I have created a virtual environment specifying python3
$ virtualenv -p /usr/bin/python3 flask_tut
$ source flask_tut/bin/activate
created a file named hello.py containing:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello, World!'
and, while in my working directory containing hello.py, enter:
(flask_tut) $ export FLASK_APP=hello.py
(flask_tut) $ python -m flask run
However, this yields errors that I am struggling with:
Traceback (most recent call last):
File "/home/matt/Envs/flask_tut/bin/flask", line 11, in <module>
sys.exit(main())
File "/home/matt/Envs/flask_tut/local/lib/python3.5/site-packages/flask/cli.py", line 478, in main
...
ImportError: cannot import name 'flask'
This is weird to me, I have installed flask as pip list indicates I have Flask (0.11.1). Others have solved similar issues by using python 3 which I am using as python --version shows I am using Python 3.5.1+
If anyone can provide help that would be excellent.
I am using Linux Mint if that makes a difference.
Edit:
After trying 100 different things, I just deleted my virtual environment, and hello.py, created a new virtual environment, working directory and script, and there are no problems. I will post if I get down to the root issue as I suspect this may happen to others running through the Flask tutorial.
Related
I am trying to start learning python bottle framework, I've installed python 2.7.11, and installed pip too, I've installed bottle using
pip install bottle
Collecting bottle
Using cached bottle-0.12.9.tar.gz
Installing collected packages: bottle
Running setup.py install for bottle ... done
Successfully installed bottle-0.12.9
Now I tried to run the example code from bottle website
from bottle import route, run, template
#route('/hello/<name>')
def index(name):
return template('<b>Hello {{name}}</b>!', name=name)
run(host='localhost', port=8080)
when I run this code it throws the following error
Traceback (most recent call last):
File "C:/Users/SID/Desktop/bottle.py", line 1, in <module>
from bottle import route, run, template
File "C:/Users/SID/Desktop\bottle.py", line 1, in <module>
from bottle import route, run, template
ImportError: cannot import name route
I don't know what went wrong could some one guide me, Is it the error with the code? or in bottle installation?
Note: I Tried python 3.4.3 too still facing same error while running program and I'm using windows 8.1 in virtual box
You have bottle.py file within your project folder. Problem occurred because python module C:/Users/SID/Desktop/bottle.py shadowed bottle.py module which installed by pip. Rename file which shadow real bottle.py module to fix import problem.
Location of file which shall be renamed is C:/Users/SID/Desktop/bottle.py.
When a module named bottle is imported, the interpreter first searches for a built-in module with that name. If not found, as in this case it then searches for a file named bottle.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:
the directory containing the input script (or the current directory).
PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
the installation-dependent default.
Make sure bottle is in PYTHONPATH. More info on PYTHONPATH here
I'm working on my very very first python program/assignment, I'm new to not just the language but it's also my first time outside of languages like Java, C#, and .Net, so I'm having a difficult time picking it up.
I was given a bunch of example files and am supposed to run them to get a feel for what I need to do, except I get an error:
ModuleNotFoundError: No module named 'flask_bootstrap'
I ran my bin/activate successfully and here's a bit of command line:
(p3) C:\Users\ritol\OneDrive\School\Project Files\Project Files\Flask\2f>python weather.py
Traceback (most recent call last):
File "weather.py", line 2, in
from flask_bootstrap import Bootstrap
ModuleNotFoundError: No module named 'flask_bootstrap'
(p3) C:\Users\ritol\OneDrive\School\Project Files\Project Files\Flask\2f>bin/activate
'bin' is not recognized as an internal or external command,
operable program or batch file.
Here's a list of pip installs I was given to do, and it did them:
Flask==1.1.1
Flask-Bootstrap==3.3.7.1
Flask-Login==0.4.1
flask-marshmallow==0.10.1
Flask-Migrate==2.5.2
Flask-RESTful==0.3.7
Flask-Script==2.0.6
Flask-SQLAlchemy==2.4.0
Flask-Testing==0.7.1
Flask-WTF==0.14.2
itsdangerous==1.1.0
Jinja2==2.10.1
2f/weather.py:
from flask import Flask, render_template
from flask_bootstrap import Bootstrap
app = Flask(__name__)
bootstrap = Bootstrap(app)
#app.route('/')
def index():
etc...
I expect to get it to show something like a previous example did:
* Serving Flask app "FlaskApp" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: 173-453-384
* Running on http://127.0.0.1:5001/ (Press CTRL+C to quit)
instead I get
Traceback (most recent call last):
File "weather.py", line 2, in <module>
from flask_bootstrap import Bootstrap
ModuleNotFoundError: No module named 'flask_bootstrap'
I had the same issue.
deactivate and activate the virtual environment solved it
deactivate
. venv/bin/activate
While I waited for help, I was trying to figure out if I could get the virtual environment to be 'hooked up' to pycharm, and I found that pycharm made it's own virtual environment without the pip installs I was given, so I got the command line open into that, ran the pip installs, and now I can get it to run through pycharm.
So it works in the virtual environment that I discovered and added the installs to. (did not work from pycharm at all due to pycharm didn't know flask was installed and would error sooner) But not in the one that I was instructed on making. I don't know why.
You have to install flask_bootstrap using the following command:
pip install flask_bootstrap
In your case, you should not use a hyphen(-) In place of hyphen use underscore( _) then it might work.
I followed this guide: guide to create a startupfile which excecutes a python file on startup.
in step 2 it says I have to test the startupfile I just created and suddently my script says:
Traceback (most recent call last):
File "Display.py", line 1, in <module>
import pyowm
ImportError: No module named pyowm
the python file works perfect if I run it directly.
what I allready tried: run pip again to see if the lib was okay
check the /usr/local/lib/python3.4/dist-packages folder to see if it was there and it is.
I think this is a python issue and not a RaspberryPi issue thats why I uploaded it here.
runned by:
sh launcher.sh
inside is:
#!/bin/sh
# launcher.sh
# navigate to home directory, then to this directory, then execute python script, then back home
cd /
cd /home/pi/arduino/Python/Main/Master
sudo python Display.py
cd /
Simple fix: define the version of python which will be used. It used python 2.7. Yet the lib was for 3.4.
I'm following a Flask tutorial and am getting an import error. I have a file called run.py which contains:
from app import app
app.run(debug = True)
When I run ./run.py, I get:
Traceback (most recent call last):
File "./run.py", line 2, in <module>
from app import app
File "/Users/myName/Desktop/SquashScraper/app/__init__.py", line 1, in <module>
from flask import Flask
ImportError: cannot import name Flask
This seems similar to this issue: http://stackoverflow.com/questions/26960235/python3-cannot-import-name-flask
So I attempted the checked solution by running:
virtualenv -p /usr/bin/python3 my_py3_env
Unfortunately, I get:
The executable /usr/bin/python3 (from --python=/usr/bin/python3) does not exist
Any ideas what may be happening here?
Thanks for the help,
bclayman
If you want your virtual environment to be Python 3 but don't know the installation directory, use which python3. Use that directory in your virtualenv -p [directory] my_py3_env command to set up the Python 3 virtual environment.
I sounds like your pip is installing to your Python 2.X directory. If you're okay with that, you'll need to run the app either with python2 run.py, python2.X run.py where x is your installed version, or change the symlink of python in /usr/bin/python to your installation of Python 2.
This question has some more information.
Regardless of the version of Python that you wish to use, you will need to install Flask to that version of Python. See this question for that.
I just recreated all my python environment, reinstalled python and setuptools, and installed virtualenv.
I started a test enviroment with virtualenv --no-site-packages test, activated it with Scripts\activate.bat and then easy_install web.py.
Then I create a code.py file:
import web
urls = (
'/.*', 'index',
)
app = web.application(urls, globals())
class index:
def GET(self):
return 'ok'
if __name__ == "__main__": app.run()
And I get the following error:
File "...\code.py", line 1, in <module>
import web
ImportError: No module named web
But if I use the interactive shell it works:
>>> import web
>>>
Everything done in the same cmd with the enviroment activated.
Does anyone know what is going on?
Edit:
It happens for every package installed within the environment. First it was web.py, now BeautifulSoup (same issue, cant find module, but import works in python shell)
Edit2:
The activate script is not setting the new python executable and the pythonpath print sys.executable gives C:\Python27\python.exe.
Solved.
Windows was configured to open .py files with C:\Python27\python.exe. I can even remember setting this mannualy some time ago so I wouldn't have to use python to run files (oh lazyness, what have you done to me?).
That's why it was working with the interactive shell, but not by executing the code.py file.
Running the file using python code.py works perfectly.