问题
How can you run Flask app which uses a specific version of python?
On my env "python" = python2.7 and "python3" = python3.6. I have installed Flask using pip3. I start the app with FLASK_APP=app.py flask run. It uses python2.7 to execute, I would like it to use python3.6.
Also tried adding #!flask/bin/python3 to app.py, but it did not have an effect.
回答1:
There are several ways:
Virtualenv lets you create isolated python environments with different versions (this is the way I would recommend)
You can put
#!/usr/bin/python3on top of your python file (see here)Or you can start your script with
python3 script.pyAs mentioned in the comments above you can also start your script inside a Docker container that hosts a python3 installation
回答2:
What I did was:
which flask, to find flask binary executable path (in my case/home/myuser/.local/bin/flaskedit
/home/myuser/.local/bin/flask, changing the first line from#!/usr/bin/pythonto#!/usr/bin/python3
In summary, making flask use python3, regardless of which shebang was specified in other scripts, since those scripts are not the entrypoint of execution, but flask is. I didn't have to change the shebang in any of my scripts, just the flask executable.
来源:https://stackoverflow.com/questions/49255283/run-flask-using-python3-not-python