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.
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/python3 on top of your python file (see here)
Or you can start your script with python3 script.py
As mentioned in the comments above you can also start your script inside a Docker container that hosts a python3 installation
What I did was:
which flask, to find flask binary executable path (in my case /home/myuser/.local/bin/flask
edit /home/myuser/.local/bin/flask, changing the first line from #!/usr/bin/python to #!/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.
Related
How should git bash be set up to run an non pip installed python library?
I am trying to run a Python library that I have git cloned (not pip installed) on Windows using git bash. I keep getting an error that the module cannot be found even though I have set the $PYTHONPATH to point to this folder.
This occurs both when using a venv and the base environment.
I've activated the venv using the activate command in the venv folder, and then added the folder to the PYTHONPATH as is shown here (excuse the blanking):
The first line shows the error I get when running the script which calls in other modules. The second shows my PYTHONPATH, the third my PATH
Adding the local folder to the path is how this functionality works on mac.
This the particular file that is meant to run, datagen.py:
#!/usr/bin/env python
from msq_data_gen.cli import cli
if __name__ == '__main__':
cli.datagen()
The code fails on the first import of msq_data_gen.
The structure of the program is as so:
Curiously, the integrated running of Pycharm works correctly, but as the code is based off a CLI, this isn't the intended usecase. (I could also use the unit testing mode for click, but I think an answer for this could be useful for other people!)
I want to build an application, say it's called helloworld, with a command line interface in python. My application as multiple nested modules and a top level module main.py.
Say the layout is :
helloworld/
helloworld/
__init__.py
module1/
module2/
module3/
setup.py
main.py
At this point, once my project is installed I can run it using
#> python path/to/helloworld/main.py arg1 arg2 arg3
I want to be able to interact with the program using commands such as :
#> helloworld arg1 arg2 arg3, in a similar manner as for example, the Flask framework comes with a command line application I can use with #> flask run.
I looked around and found questions suggesting using a shebang. As far as I can tell, this is not what I want.
I want a self contained application I can install using pip and then launch directly without any reference to the python interpreter.
How should I go about this ?
EDIT
It is an important requirement to me that the package can be installed using pip and the command line interface is immediately available. Shebangs/fiddling manually with the path/creating an additional shell script do not solve my problem.
You should add main.py to your PATH. What happens when you are running flask run is that your teminal looks up the command flask in PATH and runs the program that it is pointing to. You could see it as a kind of shortcut to the program Flask.
By adding your program to your PATH, you can tell the computer that if you type helloworld in your terminal, the terminal should run /my/path/to/helloworld.py.
I don't know what OS you are on, so here are links for most common OS on how to add a PATH variable.
Windows
Linux
Mac OSX
Edit: After you gave more explanation, setuptools is what you are looking for. Please look at this reference to see how to use it.
There's an explanation here of how to tell setuptools to add a script to the Python Scripts-folder during the pip installation. You can then make that script be for example a bat-file that calls your Python-script. In that way you achieve both that you do not need to write .py for your script to run, and that it happens automatically.
I know this has already been answered, but just in case someone comes across this in the future..
Here's what worked for a new package I'm developing after reading up on this from many different sources.
tldr; use setuptools and add your command as a console_scripts option under [options.entry_points] in setup.cfg.
You will also need some other fields in setup.cfg or setup.py for package discovery to work. Check out the setuptools docs for that.
Example entry point:
[options.entry_points]
console_scripts =
mycmd = mypackage.mymodule:myfunc
For local development, you can install the package in editable (development) mode with pip install -e . which will allow you to test the command as if you pip intalled it.
Alternatively, you could create a __main__.py that initializes your module when you run python -m mypackage from the root.
from mypackage import app
if __name__ == '__main__':
app.main()
I personally opted for both. Usually your main.py (or app.py in my example) belongs in your package not project level dir. Then you would create __main__.py there as well.
So I'm running python 3.7 on Windows and I have installed python-dotenv in my virtual environment as well as flask. I have a .flaskenv file in the root of my project where I execute flask run, it looks as if python/flask is not recognizing python-dotenv .flaskenv file it ignores my parameters and states that I do not have a FLASK_APP value assigned. This same setup works fine on a Linux workstation. Is there something different to configure on windows to get this to work?
Thanks ahead of time.
I ran into the same issue and resolved it by executing python -m flask run instead of flask run. The -m allows modules to be located and executed.
.flaskenv seems to be working fine on Windows 10. Have you tried creating a new file named .flaskenv, without specifying any extension, in Visual Studio Code? A .flaskenv file of FLASKENV FILE type gets automatically created. I have in there a single line FLASK_APP=yourFileName.py.
Hope this helps.
I am trying to get a feel for the Flask microframework by launching a test application to local server. When trying to run my code, app.py, I keep getting the error message:
-bash: ./app.py: /flask/bin/python: bad interpreter: No such file or directory
Here is the basic code (taken from here) for app.py, which lives in my todo-api directory:
#!/flask/bin/python
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return "Hello, World!"
if __name__ == '__main__':
app.run(debug=True)
I've checked the file path to the python interpreter, and it should exist:
:bin $ pwd python
Users/me/Documents/Python/todo-api/flask/bin
I have followed the tutorial to the T; I've tried changing the shebang line to:
#!/flask/bin/python2.x
#!flask/bin/python
#!/flask/bin/env python
But to no avail. I am not that knowledgeable about bash, and have tried looking up what is going on, but the solutions to folks with similar problems have not worked for me; is there something going on behind the scenes that I am not understanding?
Bash shebangs expect an absolute path to the interpreter. So in your case you need to specify the full path to your Python interpreter i.e.:
#!/Users/me/Documents/Python/todo-api/flask/bin
You might want to investigate the use of /usr/bin/env python to be able to use the interpreter that is available in your user's $PATH environment variable. See https://unix.stackexchange.com/questions/12736/how-does-usr-bin-env-know-which-program-to-use/12751#12751
pwd tells you the current directory. It doesn't tell you where a command is located. The output from that command is a red herring.
You may be looking for which python. Put that path into your shebang line. Note that this will give you the Python interpreter from your $PATH, which may or may not be the right one.
The standard shebang line for Python scripts is
#!/usr/bin/env python
or
#!/usr/bin/python
I was having a similar issue with trying to setup a python script as an executable for testing some things and realized that bash was getting in the way more than it was helping. I ended up setting up pyinstaller (which is incredibly easy) and then making my script a stand alone executable without bash being in the mix.
Here's what I did (only takes a couple of minutes and no config):
First; pyinstaller needs: build-essential & python-dev
apt-get install build-essential python-dev
(or yum, etc... depending on your OS)
Then use the built in python package manager to setup pyinstaller:
pip install pyinstaller
That's it. Run: pyinstaller --onefile myapp.py (or pyinstaller.exe if your OS needs exe)
If it's successful (and it usually is), your new executable will be in a folder "Dist" in the same area you ran pysinstaller.
I'm trying to deploy an example dart server using Google AppEngine. However when I run this python script (with the latest python version installed 3.5)
dev_appserver.py
I've also tried
dev_appserver.py --custom_entrypoint "dart bin/server.dart {port}" app.yaml
I get this error:
Traceback (most recent call last):
File "C:\Users\jkrie\AppData\Local\Google\Cloud SDK\google-cloud- sdk\bin\dev_appserver.py", line 11, in <module>
import bootstrapping.bootstrapping as bootstrapping
File "C:\Users\jkrie\AppData\Local\Google\Cloud SDK\google-cloud-sdk\bin\bootstrapping\bootstrapping.py", line 9, in <module>
import setup
ImportError: No module named 'setup'
I've also installed setuptools. I have to assume there is something wrong with my Google Cloud SDK install, but I really don't know what. Is python 3.5 too new and I need to try an older version?
GAE standard environment only supports Python 2.7 at this time, see Google App Engine Documentation.
Python 3.4 is supported only in the flexible environment, which has a different development flow.
Related: Google cloud dev_appserver.py unable to host laravel project locally
Indeed you have to use Python 2 for the standard App Engine environment for the time being.
If you have Python 3 installed, you can create a virtualenv using Python 2 using mkvirtualenv google --python=$(which python2) and run dev_appserver.py . in that environment.
This saves you the hassle of having to downgrade or symlink python to python2
Google could prepend the file with
#!/usr/bin/env python2
instead of
#!/usr/bin/env python
It would make their tools compatible with OSs that use python3 as default.
As #dan-cornilescu said GAE Standard Environment support only Python2.7
If you are in an environment with multiversion of Python, you could easly use Pipenv to run your dev_appserver.py with Python 2.7 version.
After you have installed pipenv globaly you could create a pipenv environment inside your project folder with Python 2.7
# pipenv install --twoo
Every time you need to run dev_appserver.py you should use this command
# pipenv run dev_appserver.py app.yaml
pipenv will use Python 2.7 to run your code. ;-)
I had this issue since I install both python2.9 and python3.6.
Quick way without uninstall python3 is just delete python3 path in environment variables while you are using GAE. Add them back when done with GAE.
I had the same problem with a very simple python35 app (now, a year later!)
I did create a python27 virtual environment which does work, but needed more workarounds.
The easiest thing is to just run python applicationmodule.py on the shell command line, ensuring you have this at the bottom:
if __name__ == '__main__':
# This is used when running locally. Gunicorn is used to run the
# application on Google App Engine. See entrypoint in app.yaml.
app.run(host='127.0.0.1', port=8080, debug=True)
If you want to run using dev_appserver.py then, I found I needed to run the following to make this work on the Google Cloud Shell:dev_appserver.py app.yaml --custom_entrypoint ./applicationmodule.py
In that case, make sure the applicationmodule.py doesn't have the if __name__ == '__main__': code. If you do have this, then it starts the same task again and complains about contention on port 8080.
This is different from other answers which have the --custom_entrypoint parameter looking more like the app.yaml entrypoint: entry.
At one point executing dev_appserver.py complained about executing applicationmodule.py (I forget exactly), so I did both chmod 777 and I added a #! pointing to my local python executable - it worked after having done both, but don't know if it was the chmod or the #! that did it.
App Engine now supports Python 3 from version 3.7.
If you still have Python 2 as the default, one way to specify version 3 is to add this to your ~/.zshrc or other startup scripts:
CLOUDSDK_PYTHON=python3
The environment variable can also take the full path to the preferred Python executable if it's not aliased to python3.
Here's a quick start guide and some differences between the support for versions 2.7 and 3.7.