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.
Related
I'm working in a flask REST API using python3, supervisor and gunicorn. I have installed all the pip modules. when i run the supervisor's config file it shows following error. Any help would be appreciated.
Using virtual environment and install python 3.x to it. After that it works fine.
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.
I am trying to run a python script with Azure functions.
I had success updating the python version and installing modules on Azure functions under the App Services plan but I need to use it under the Consumption plan as my script will only execute once everyday and for only a few minutes, so I want to pay only for the time of execution. See: https://azure.microsoft.com/en-au/services/functions/
Now I'm still new to this but from my understanding the consumption plan spins up the vm and terminates it after your script has been executed unlike the App Service plan which is always on.
I am not sure why this would mean that I can't have install anything on it. I thought that would just mean I have to install it every time I spin it up.
I have tried installing modules through the python script itself and the kudu command line with no success.
While under the app service plan it was simple, following this tutorial: https://prmadi.com/running-python-code-on-azure-functions-app/
On Functions Comsumption plan, Kudu extensions are not available. However, you can update pip to be able to install all your dependencies correctly:
Create your Python script on Functions (let's say NameOfMyFunction/run.py)
Open a Kudu console
Go to the folder of your script (should be d:/home/site/wwwroot/NameOfMyFunction)
Create a virtualenv in this folder (python -m virtualenv myvenv)
Load this venv (cd myenv/Scripts and call activate.bat)
Your shell should be now prefixed by (myvenv)
Update pip (python -m pip install -U pip)
Install what you need (python -m pip install flask)
Now in the Azure Portal, in your script, update the sys.path to add this venv:
import sys, os.path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname( __file__ ), 'myvenv/Lib/site-packages')))
You should be able to start what you want now.
(Reference: https://github.com/Azure/azure-sdk-for-python/issues/1044)
Edit: reading previous comment, it seems you need numpy. I just tested right now and I was able to install 1.12.1 with no issues.
You may upload the modules for the Python version of your choice in Consumption Plan. Kindly refer to the instructions at this link: https://github.com/Azure/azure-webjobs-sdk-script/wiki/Using-a-custom-version-of-Python
This is what worked for me:
Dislaimer: I use C# Function that includes Python script execution, using command line with System.Diagnostics.Process class.
Add relevant Python extension for the Azure Function from Azure Portal:
Platform Features -> Development Tools -> Extensions
It installed python to D:\home\python364x86 (as seen from Kudu console)
Add an application setting called WEBSITE_USE_PLACEHOLDER and set its value to 0. This is necessary to work around an Azure Functions issue that causes the Python extension to stop working after the function app is unloaded.
See: Using Python 3 in Azure Functions question.
Install the packages from Kudu CMD line console using pip install ...
(in my case it was pip install pandas)
I am building an app on Google Compute Engine using Django 1.7 and Python 2.7 within a Debian environment.
To get the mpl_toolkit library to work I have had to install Anaconda and its various modules within that environment, thereby over-riding the default "out-the-box" Bitnami setup.
I am currently unable to get the live server to run python from the Anaconda directory (/home/beastflow/anaconda2/bin/python) as it needs to.
Indeed, when I load the page I get the following line as part of the debug message:
Python Executable: /opt/bitnami/python/bin/python
What do I have to change in order to get Django to point to /home/beastflow/anaconda2/bin/python ?
Ok I found an answer (which avoids having to rebuild mod_wsgi). If you're using Bitnami, you can get python to load from a different path by changing /opt/bitnami/apache2/bin/envvars by adding a PATH line as follows (for example):
PATH=/home/beastflow/anaconda2/envs/django17/bin/:$PATH
export PATH
Then restart the apache server:
sudo /opt/bitnami/ctlscript.sh restart
I am working on a Django project that was created by another developer on a different machine. I see that in the root of the application, there is a .virtualenv directory. Is it possible to simply setup this project locally on my Windows machine using the project settings and Python version (the app uses 2.7), so that I can run it like a local Django application so debugging is feasible?
I have access to the development web server and have copied the full source of the app down to my Win7 machine but cannot seem to get things setup correctly to run the app locally so I can debug.
I currently have Python 2.7, 2.7.5 and 3.3.2 installed on my local dev machine. I would call myself pretty new to Django and Virtualenv.
If anyone has any guidance on how I can get my environment straitened out so I can run the app with debugging, I would be very thankful.
Thank you in advance.
Using a virtualenv environment created on a different machine is not recommended. There are things hard-wired for the particular system it was created on, and some apps may have components compiled for that particular system.
You should create a new virtualenv environment on your machine, install dependencies and move the Django project there.
Note on installing dependencies - there might be a file named requirements.txt somewhere. If it's there and it's kept up to date you can install all the dependencies by running a single command while in your virtualenv:
pip -r requirements.txt install
If you can't find it ask the other developer to create it. He just need to do this inside his own environment:
pip freeze > requirements.txt
I once faced the same problem and it took me so much time to configure another environment that I eventually had to create a VM with the same version of OS and libraries. I then made a raw copy of the project and it worked fine.