I'm following the Flask tutorial here:
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
I get to the point where I try ./run.py and I get:
Traceback (most recent call last):
File "./run.py", line 3, in <module>
from app import app
File "/Users/benjaminclayman/Desktop/microblog/app/__init__.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask
This looks similar to:
ImportError: No module named flask
But their solutions aren't helpful. For reference, I do have a folder named flask which one user mentioned may cause issues.
Try deleting the virtualenv you created. Then create a new virtualenv with:
virtualenv flask
Then:
cd flask
Now let's activate the virtualenv
source bin/activate
Now you should see (flask) on the left of the command line.
Edit: In windows there is no "source" that's a linux thing, instead execute the activate.bat file, here I do it using Powershell: PS C:\DEV\aProject> & .\Flask\Scripts\activate)
Let's install flask:
pip install flask
Then create a file named hello.py (NOTE: see UPDATE Flask 1.0.2 below):
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
and run it with:
python hello.py
UPDATE Flask 1.0.2
With the new flask release there is no need to run the app from your script. hello.py should look like this now:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
and run it with:
FLASK_APP=hello.py flask run
Make sure to be inside the folder where hello.py is when running the latest command.
All the steps before the creation of the hello.py apply for this case as well
For python 3 use
pip3 install flask
The only way I could solve was by adding my users python dir to myapp.wsgi file. As an example:
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.
After activating the virtual environment and installing Flask, I created an app.py file. I run it like this : python -m flask run. Hope this will help!
I had a similar problem with flasgger.
The reason for that was that I always use
sudo pip install flask
but for some reason that's not always the way to go.
Sometimes, you have to do just
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?
this is what worked for me,
sudo -H pip install flask
Or for pip3(python3) use :
sudo -H pip3 install flask
Sidenote
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.
Another thing - if you're using python3, make sure you are starting your server with python3 server.py, not python server.py
This is what worked for me when I got a similar error in Windows:
Install virtualenv:
pip install virtualenv
Create a virtualenv:
virtualenv flask
Navigate to Scripts and activate the virtualenv:
activate
Install Flask:
python -m pip install flask
Check if flask is installed:
python -m pip list
I was using python2 but installed this:
sudo apt-get install libapache2-mod-wsgi-py3
Instead of:
sudo apt-get install libapache2-mod-wsgi
Correcting the installation solved the no flask problem.
Edit
/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).
Even i too suggest u virtualenv,
This might also solve ur problem.
sudo apt install python-flask
If u want to deploy in productionserver then go ahead with above solution else use virtualenv.
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.
The simplest answer to this problem is specifying the correct path to flask in your environment. Make sure the script you are running knows how to get to the flask installation. Using print(sys.path) as mentioned above certainly helped to figure out the path(s) the script was using.
enter your python interactive mode
then:
import sys
sys.path
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```
my answer just for any users that use Visual Studio Flesk Web project :
Just Right Click on "Python Environment" and Click to "Add Environment"
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.
you first need to create a Python Environment
like this:
py -m venv YOUR_DIR_NAME
You can also try this workaround set FLASK_DEBUG=1 && python -m flask run
It worked for me
It worked for me after upgrading pip:
curl https://bootstrap.pypa.io/get-pip.py | python
Found that answer here: https://stackoverflow.com/a/49748494/3197202
Then I could just install flask:
pip install flask
Related
I'm on Windows 10 and I am using PyCharm to run my application. I'm pretty new to Python and just coding in general.
I typed:
pip install flask
in the cmd prompt but forgot to open cmd prompt under administrator privileges. Could this be the problem? I ended up re-opening the cmd prompt with administrator rights and typed
pip install flask
again and it showed that it was already installed. I'm pretty sure I have flask installed on my system.
When I try to run the python file below which I named test_webapp.py I get the following error:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
I get an error:
ModuleNotFoundError: No module named 'flask'
when i type in python --version into the command prompt i get version 3.6.3.
When i type the same code into the Python 3.6 (32-bit) command line, the code works! However, when i try to run it using the IDLE I get a SyntaxError and the number 6 in Python 3.6.3 gets highlighted in red.
Any ideas as to what's going on?
Try to install flask with specific version of python, do something like:
python[version] -m pip install flask
Note: replace [version] with python version (such as python3).
Sounds like the pip you are using is not installing for the same python you are using.
try which pip and which python
the they should be in the same folder. Might want to make an alias or two if you have a bunch of pythons (sys, 2, 3, conda etc) or just start with a fresh virtual env for your project. <- best imo.
When you typed pip install flask in the command prompt, it installed flask in the global environment. That is the reason when you run it in command line it works because it used global environment by default.
Pycharm has a virtual environment, therefore you need to install it in that virtual environment. Open Pycharm, then open the terminal within Pycharm and type "pip install flask".
You can read about virtual environment in python to get a clear idea.
Check version of python
python --version
And then use accordingly
if python 3.x.x then type
python3 app.py
if python 2.x.x then type
python2 app.py
I'm following the Flask Mega Tutorial from http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world/page/0
I have created the project folder as told, and created the virtualenv using "mkvirtualenv flask" and used "workon flask" to activate it.
After that the tutorial asks to run some commands to install the dependecies of the project running "flask/bin/pip install " but when I try it I get the following error: "bash: flask/bin/pip: No such file or directory". (I believe that's because the folders created for the venv, are not on the folder but on ~/.virtualenvs).
So I istalled the dependecies using "(flask)$ pip install "
then I created the files and folders for the project as told, and when I try to run the project I get: bash: ./run.py: flask/bin/python: bad interpreter: No such file or directory
If I run "python run.py" looks like the project is running, but notthing happens, and i get no message saying that the project is running on any port, just this:
Restarting with stat
Debugger is active!
Debugger pin code: 284-703-124
can anyone help me pls?
(i'm running on ubuntu 14.04lts)
Don't know if this will help but this is how I solved my config issues for The Flask Mega-Tutorial on Mac OS 10.11 El Capitan.
Start from scratch removing all files and folders you've been working with
Follow the Flash install guide on the website http://flask.pocoo.org/docs/latest/installation/
Using the steps in the link above as follows:
$ sudo easy_install virtualenv
$ mkdir myproject
$ cd myproject
$ virtualenv venv
$ . venv/bin/activate
$ pip install Flask
3. Use the $ pip install Flask format for all the items Miguel lists in the $ flask/bin/pip install flask format
$ mkdir app
4. Make the necessary files for the Hello, World! example: app/__init__.py,
app/views.py, a
run.py
Note: in run.py you will need to change the first line from #!flask/bin/python to #!venv/bin/python
5. Run it
$ chmod a+x run.py
$ ./run.py
If you're running "python run.py" and getting this output...
Restarting with stat
Debugger is active!
Debugger pin code: 284-703-124
...then the flask project actually IS running. You've got it working - it just doesn't tell you that in the output above. Open a browser and navigate to http://localhost:5000, though, and you should see the test site up and running.
The problem is that in the tutorial, it assumes you have not activated the virtual environment, and you are still in the parent directory where the virtual environment was created.
That's why the commands are all prefixed with flask/bin/pip; flask is the directory where the virtual environment was created.
Once you create a virtual environment you usually follow this up with the activate script. All this script does is changes the paths so commands like python and pip point to the virtual environment's bin folder rather then pointing to the default system location. It also adds a variable to the shell so that programs can detect they are running in a virtual environment, and it adds the name of the virtual environment to the prompt.
If you don't activate the virtual environment, you can still install packages in it by giving the full path to the pip command that is in the virtual environment. Due to the way Python works, it will install packages inside the virtual environment (since that's the first package directory it will find). This is what is happening in the tutorial.
So if you activated the virtual environment, remove flask/bin/ from the instructions and use the commands directly.
I am following these steps to learn flask http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world/page/0#comments
I ran this command to create the virtual env:
python virtualenv.py flask
When I try to start flask using the python.exe file in my project scripts directory, it says
No module named flask
My PATH is set to the python directory that virtualenv installed. Why is it not able to find flask?
I initially started with the official Flask quickstart guide and was able to get the webserver to run, but with this virtual env install it is not working.
Make sure your virtualenv is activated. Then You check on the PYTHONPATH of that virtualenv. Is there a flask package (folder) installed in that directory.
If you unsure whether you have installed flask, just run the following command to see all the packages you have installed pip list or pip show flask. Do you see flask there? If not you can run pip install flask
This error can also appear if you start your Flask python server using ./run.py or similarly use file associations to start your server. Then the python command in the association will be used instead of your virtual environment python command. Use python run.py instead. See how my run.py innocently assumes /usr/bin/python?
#!/usr/bin/python
# run.py
from app import app
app.run(debug=True,host='0.0.0.0',port=5000)
I had this same problem on three Raspberry Pi units at the same time; beat my head against the wall trying to fix it for several hours (reinstall flask via pip, apt and aptitude - no joy).
Instead of:
pip install flask
I finally tried:
pip install Flask
Worked like a charm.
Make sure you run your script after you've activated your virtualenv. On OS X you'd see (virtual_env_name) at the start of each terminal line. To do this:
cd to your virtualenv's directory and type . bin/activate
cd to the directory containing the .py file you want to run at app launch in the browser
Now enter python file_name.py, for me the file name was routes.py following this example
This problem can also arise if the port is not available.
Try running on different port.
Activate your virtual environment first with
source bin/activate envName
Then try to run your command again
If nothing else helps, check that in your code it is:
from flask import Flask
I've tried many things before I've noticed my mistake. I had this in my code:
from Flask import Flask
When I have changed capitalized letter for the module name, i.e. flask then everything worked.
On Windows even if you see (virtual_env_name) in the cmd line it is possible that the virtual environment is not completely activated. Deactivate/reactivate and try again.
I am running Python on windows 7. I had same problem No module named flask.
I tried reinstalling python, venv but it did not work.
Finally i run it like this
Install venv the usual way
go to scripts directory and activate
C:\Python34\microb>c:\Python34\microb\fla\scripts\python run.py
here microb is my project and fla is venv
In Python 3.x
pip3 install flask
Worked fine for me.
Thanks & Regards
For those of you on Windows who are running into this problem, have activated your venv and flask is installed in the right directory. For me I realized it was looking for flask but the file was called flask.exe. I renamed it and it worked perfectly.
Try below lines :
$ python3.7 -m venv env
$ source env/bin/activate
(env)$ pip install yourpackages
(env)$ python app.py
I get the following error when trying to run Django from the command line.
File manage.py, line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
Any ideas on how to solve this?
It sounds like you do not have django installed. You should check the directory produced by this command:
python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
To see if you have the django packages in there.
If there's no django folder inside of site-packages, then you do not have django installed (at least for that version of python).
It is possible you have more than one version of python installed and django is inside of another version. You can find out all the versions of python if you type python and then press TAB. Here are all the different python's I have.
$python
python python2-config python2.6 python2.7-config pythonw2.5
python-config python2.5 python2.6-config pythonw pythonw2.6
python2 python2.5-config python2.7 pythonw2 pythonw2.7
You can do the above command for each version of python and look inside the site-packages directory of each to see if any of them have django installed. For example:
python2.5 -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
python2.6 -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
If you happen to find django inside of say python2.6, try your original command with
python2.6 manage.py ...
sudo pip install django --upgrade
did the trick for me.
I got the same error and I fixed it in this manner:
I had to activate my virtual environment using the following command
source python2.7/bin/activate
Most probably in your manage.py the first line starts with !/usr/bin/python which means you are using the system global python rather than the one in your virtual environment.
so replace
/usr/bin/python
with
~/projectpath/venv/bin/python
and you should be good.
well, I faced the same error today after installing virtualenv and django. For me it was that I had used sudo (sudo pip install django) for installing django, and I was trying to run the manage.py runserver without sudo. I just added sudo and it worked. :)
Are you using a Virtual Environment with Virtual Wrapper? Are you on a Mac?
If so try this:
Enter the following into your command line to start up the virtual environment and then work on it
1.)
source virtualenvwrapper.sh
or
source /usr/local/bin/virtualenvwrapper.sh
2.)
workon [environment name]
Note (from a newbie) - do not put brackets around your environment name
I am having the same problem while running the command-
python manage.py startapp < app_name >
but problem with me is that i was running that command out of virtual environment.So just activate your virtual environment first and run the command again -
I experience the same thing and this is what I do.
First my installation of
pip install -r requirements.txt
is not on my active environment. So I did is activate my environment then run again the
pip install -r requirements.txt
The problem occurs when django isn't installed on your computer. You also get this error because the django.core.management module is missing.
To solve this issue we have to install django using pip. Open a command line prompt -> cmd(on windows) and enter the following command:
pip install django
This command will install django on your computer. So consider installing pip first. Here's how to install pip on a Windows machine
Okay so it goes like this:
You have created a virtual environment and django module belongs to that environment only.Since virtualenv isolates itself from everything else,hence you are seeing this.
go through this for further assistance:
http://www.swegler.com/becky/blog/2011/08/27/python-django-mysql-on-windows-7-part-i-getting-started/
1.You can switch to the directory where your virtual environment is stored and then run the django module.
2.Alternatively you can install django globally to your python->site-packages by either running pip or easy_install
Command using pip: pip install django
then do this:
import django print (django.get_version()) (depending on which version of python you use.This for python 3+ series)
and then you can run this: python manage.py runserver and check on your web browser by typing :localhost:8000 and you should see django powered page.
Hope this helps.
In case this is helpful to others... I had this issue because my virtualenv defaulted to python2.7 and I was calling Django using Python3 while using Ubuntu.
to check which python my virtualenv was using:
$ which python3
>> /usr/bin/python3
created new virtualenv with python3 specified (using virtualenv wrapper https://virtualenvwrapper.readthedocs.org/en/latest/):
$ mkvirtualenv --python=/usr/bin/python3 ENV_NAME
the python path should now point to the virtualenv python:
$ which python3
>> /home/user/.virtualenvs/ENV_NAME/bin/python3
This also happens if you change the directory structure of your python project (I did this, and then puzzled over the change in behavior). If you do so, you'll need to change a line in your /bin/activate file. So, say your project was at
/User/me/CodeProjects/coolApp/
and your activate file is at
/User/me/CodeProjects/coolApp/venv/bin/activate
when you set up your project, then you changed your project to
/User/me/CodeProjects/v1-coolApp/
or something. You would then need to open
/User/me/CodeProjects/v1-coolApp/venv/bin/activate
find the line where it says
VIRTUAL_ENV="/User/me/CodeProjects/coolApp"
export VIRTUAL_ENV
and change it to
VIRTUAL_ENV="/User/me/CodeProjects/v1-coolApp"
before reactivating
In my case, I am using Ubuntu. The problem can be that I don't have the permission to write to that folder as a normal user. You can simply add the sudo before your command and it should work perfectly. In my case sudo python manage.py syncdb.
I had the same issue and the reason I was getting this message was because I was doing "manage.py runserver" whereas doing "python manage.py runserver" fixed it.
I had the same problem and following worked good, you should navigate main folder in your project than type:
source bin/activate
My case I used pyCharm 5 on mac. I also had this problem and after running this command my problem was solved
sudo pip install django --upgrade
had the same problem.run command 'python manage.py migrate' as root. works fine with root access (sudo python manage.py migrate )
You can try it like so : python3 manage.py migrate (make sur to be in the src/ directory)
You can also try with pip install -r requirements.txt (make sur you see the requirements.txt file when you type ls after the migrate
If after all it still won't work try pip install django
Hope it helps
I got the same problem trying to use the python manage.py runserver. In my case I just use sudo su. Use the terminal as a root and try it again an it works partially. So I use python manage.py migrate comand and it fix it.
You must choose your Project first before running the server , type this
workon your_project_name
then
python manage.py runserver
It is because of virtual enviornment configuration. You need to work on your virtual enviornmnet of Python. You should try on your command promt with,
workon virtual_enviornment_name
File and Directory ownership conflict will cause issues here. Make sure the ownership of the directories and files under the project are to the current user. (You can change them using the chown command with the -R option.) Try rerunning the command: this solved the problem for me when running through the "First Django App" sample:
python manage.py startapp polls
I recently deleted some old development folders off my disk, and now one of my virtualenv projects doesn't work. I noticed the problem when I tried to import flask.
I am using Ubuntu and I was using python2.7 in the virtualenv instead of python2.6 which is the default python.
I will describe how I fixed it, but I was wanting to know if there was a better way.
fyi I use bash in the terminal by default...
download source from http://www.python.org/download/releases/2.7/
move source file into /project/src/dir and extract
change the working directory of the terminal to the newly extracted directory
configure and make
mkdir python2.7
./configure --prefix=/project/src/dir/python2.7
make
make install
create virtualenv and specify python to use
virtualenv --no-site-packages -p /project/src/dir/python2.7/bin/python2.7 projectname
enter the virtual environment
cd projectname
source bin/activate
get flask if you want it
pip install flask
test to see if we can import time and inspect
(bash)
python
(python shell)
import time
import inspect
test to see if we can import flask
import flask
Perhaps you were using --system-site-packages before hand and flask was available via a path external to your VirtualEnv.
Keep in mind that older versions of VirtualEnv were not very relocatable either if they moved at all.