sys.path.append('/home/deployer/anaconda3/lib/python3.5/site-packages')
I guess that if you install the packages in the global enviroment, you should have no problem, but I had my python packages installed as user.
–
–
pip install flask
Another gotcha is that sometimes people type pip install Flask
with the cap F
Posting this here in case somebody gets stuck.
Let me know if it helped.
Useful Link:
What is the difference between pip install and sudo pip install?
If you're using virtualenv it's a good idea to
pip freeze >> requirements.txt
to allow for the installed packages to be listed in one place.
The sudo
command and -H
flag. For more on sudo
's -H
flag, look at Paul's answer.
Hope this helps you.
–
/etc/apache2/sites-available/FlaskApp.conf
Add the following two lines before the "WSGIScriptAlias" line:
WSGIDaemonProcess FlaskApp python-home=/var/www/FlaskApp/FlaskApp/venv/FlaskApp
WSGIProcessGroup FlaskApp
Restart Apache:service apache2 restart
I'm following the Flask tutorial too.And I met the same problem.I found this way to fix it.
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
In my case the solution was as simple as starting up my virtual environment like so:
$ venv/scripts/activate
It turns out I am still fresh to Python :)
Go to the flask file in microblog, then activate the virtual environment with source bin/activate
, then go to flask/bin and install flask, and the rest of the packages, pip install flask
. You will see flask listed inside bin directory. Try to run ./run.py
again from microblog (or from wherever you have the file).
If you are using Pycharm then this is the virtual environment issue.
So, at the time of creating your Python project you will have to select "Existing interpreter" option -> click "system Interpreter" -> select the correct option for example "*\AppData\Local\Programs\Python\Python3.6\python.exe".
You can use 'New Virtual Env' as well, but I have just given the quick fix that should work for Pycharm users.
it will print your path. Check wether flask is installed in the sys.path.
For MacOS, python path is under
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
But pip'll install python package by default under
/Library/Python/2.7/site-packages
That's why it doesn't work for MacOS.
The flask script is nice to start a local development server, but you would have to
restart it manually after each change to your code. That is not very nice and Flask can
do better. If you enable debug support the server will reload itself on code changes,
and it will also provide you with a helpful debugger if things go wrong.
To enable debug mode you can export the FLASK_DEBUG environment variable before
running the server:
forexample your file is hello.py
$ export FLASK_APP=hello.py
$ export FLASK_DEBUG=1
$ flask run
in my case using Docker, my .env file was not copied, so the following env vars were not set:
.env.local:
FLASK_APP=src/app.py
so in my Dockerfile
i had to include:
FROM deploy as dev
COPY env ./env
which was referenced in docker-compose.yml
env_file: ./env/.env.local
another thing i had to pay attention to is the path variable to ensure my environment is used
ENV PATH $CONDA_DIR/envs/:my_environment_name_from_yml_file:/bin:$CONDA_DIR/bin:$PATH```
I had a similar problem using WSL and PyCharm / VSCode, the problem only occurred in the development environment and not in production. I realized that IDE's with WSL can have problems when exporting PYTHONPATH from their work directory and I started to do it manually.
Before running your program and inside a VENV, try running the following command:
export PYTHONPATH="path/my_user/code"
–
–
In your case , the solution is :
First:
Open the terminal and navigate to the directory/folder where that python file is located ( in ur case the python file you created is run.py) and make sure you have installed flask in that same directory/folder.
Second :
Now , type the following command :
$python3 "pythonfilename" (in ur case, its $python3 "run.py")
I had the same error where it said flask module not found. this is what I did:
used venv to create a virtual environment $ python3.6 -m venv --without-pip virtual
activated the virtual environment using source virtual/bin/activate
downloaded pip into the virtual environment curl https://bootstrap.pypa.io/get-pip.py | python
installed flask using pip install flask
after running my application, all was good.
–
Found that answer here: https://stackoverflow.com/a/49748494/3197202
Then I could just install flask:
pip install flask