I'm working through Miguel Grinberg's Flask Mega-Tutorial, and am unable to run the basic app in Part I. I am using Ubuntu, and getting the following traceback:
Traceback (most recent call last):
File "./run.py", line 2, in <module>
from app import app
File "/home/makisupa43/dev/microblog/app/__init__.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask
I have double checked code and all looks correct. Not sure if I'm getting screwed up with VirtualEnv or if it is a separate issue.
Reading through the tutorial, it looks like Miguel skips the step where you actually activate the virtual environment. Run this command from the directory where you're doing all your pip installs:
. flask/bin/activate
This should put you in the correct virtual environment, which will make everything you've installed with pip available to your run.py script.
Related
I'm new to flask framework and I want to write a simple flask app that uses another python module (librosa package). I have successfully installed librosa in the same virtual environment that I have installed flask and I can easily import it in the python interpreter. Here is the python script.
# app.py
from flask import Flask
import librosa
app = Flask(__name__)
#app.route('/')
def hello():
return 'Hello'
if __name__ == '__main__':
app.run()
The problem is that when I want to run the flask app using the flask run command (after setting export FLASK_APP=app.py) I get the ModuleNotFoundError error as follows:
Error: While importing "app", an ImportError was raised:
Traceback (most recent call last):
File "/home/masoud/anaconda3/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/home/masoud/projects/my_projects/seperator_rewriten/app.py", line 2, in <module>
import librosa
ModuleNotFoundError: No module named 'librosa'
The strange thing is that there is no import error when I run the flask server using python app.py command.
if you are using an IDE such as pycharm, then you may need to install it from the terminal in the IDE itself, not cmd
This may be the solution to your problem:
python -m flask run
The reason is that flask run may use the python executable somewhere else, not the virtual environment you created for the project.
You can add these lines of code in your script:
import sys
import flask
print(sys.executable)
print(flask.__version__)
And then you can check the python executable path using python app.py and flask run.
I use codeanywhere.com try to create application with Python werkzeug and jinja2.
I install those library utilize virtualenv. Now I face the problem
Traceback (most recent call last):
File "manage.py", line 4, in
from werkzeug import Request
ImportError: No module named 'werkzeug'
virtualenv activated but nothing is works.
Does anybody has experience with codeanywhere.com?
Oh I solve this problem. In the container config file add instruction "source venv3/bin/activate" as shown on a picture add path to python as well.
enter image description here
So I have run into a funny problem when trying to use Flask, I can only run it from ~/ (home) and not from ~/Projects/projectfolder. I'm using Python 2.7.4 installed via their homepage, virtualenv and virtualenvwrapper. Every time it's the same:
$ mkvirtualenv project
New python executable in project/bin/python
Installing setuptools............done.
Installing pip...............done.
Then I install Flask:
$ pip install flask
[...]
Successfully installed flask Werkzeug Jinja2
Cleaning up...
Then I open Python from my home directory:
(project) $ python
>>> from flask import Flask
>>>
Then I quit and go to my project folder:
(project) $ cd ~/Projects/example
(project) $ python
>>> from flask import Flask
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "flask.py", line 1, in <module>
from flask import Flask
ImportError: cannot import name Flask
And I'm a bit lost as to why this is happening, anybody have any ideas?
According to you traceback, you have a module of your own called flask.py in ~/Projects/example.
The current directory is searched before the actual package installation path, so it shadows the "real" Flask.
I am running the following code
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, debug=True)
and getting the following error
Traceback (most recent call last):
File "test.py", line 1, in <module>
from flask import Flask
File "/home/pi/programs/flask.py", line 1, in <module>
from flask import Flask
ImportError: cannot import name Flask
I have tried installing flask through various methods , but still the problem persists
also, is there any alternative to flask???
I ran into this error because I named the test file as flask.py and tried to run it! It creates namespace conflict with the real flask module!
Delete the local test file that you named flask.py and the respective flask.pyc. Give some other name! This will happen with other modules like socket etc where you are likely to give the same name for the test file as the standard module :-)
The reason is your python file name is flask.
Just run apt-get install python3-flask
Edited to install the python3 version, since nobody should be using python2 now.
Just rename flask.py file also delete flask.pyc file
It's because of the name flask.py. It will import itself if the name is flask.py. Change the name and try again.
Restart virtual environment
$ virtualenv flask
Into dir flask run
$source ./bin/activate
Install python module again
$pip install "module"
I had the same issue. Apparently you can't name your file socket.py either.
exactly when we create file name as flask.py and then execute it for the first time it will execute and at the same time framework creates another file called flask.pyc .Now stop the process and start it again it will throw this error as instead of looking to actual framework flask file it is looking in to the one you created . To solve this problem
Go to the folder you created flask.py and delete flask.pyc and then rename flask.py to some test_1.py ..save it and execute it . You should see no errors .
Add PYTHONPATH to your environment:
export PYTHONPATH=/root/environments/my_env/lib/python3.6/site-packages/
I have a ubuntu lucid VPS server where I try to run my django development server.
Running python manage.py runserver gives me the following error:
Traceback (most recent call last):
File "manage.py", line 2, in <module>
from django.core.management import execute_manager
ImportError: No module named core.management
I can import django without any problem in a python shell. I looked in the django install directory and noticed there is no \__init__.py in the django/core folder. Which I beleive is the source of the problem for python to register django.core as a module.
It then looks like an installation issue. I installed django using apt-get.
FYI the django install worked perfectly on my home computer with same OS.
Any ideas?
solved.
Thank you for all the suggestions. I installed django using the tar ball as instructed in the django website.
apt-get is not a good idea to install django. I also had to manually remove the left over django folder in /usr/lib/pymodules/python2.6 after using apt-get remove.