Python¶
Python is a popular general-purpose scripting language.
Python/uWSGI Applications¶
Python web applications are commonly deployed using WSGI. Opalstack provides a installer for the popular uWSGI application server configured for Python 3.
Installing a Python/uWSGI Application¶
To install a Python/uWSGI application follow our general instructions for adding applications and select "Python/uWSGI" as the application type in step 5.
When the installation is complete, the following files and directories will be present in the application directory:
- start
, stop
, and kill
: scripts to control the operation of your application.
- env
: a directory containing the Python 3 environment used by your application.
- tmp
: a directory containing temporary files used by your application
- myapp
: a directory containing a simple "hello world" Python WSGI application.
- uwsgi.ini
: the configuration file for your Python/uWSGI application.
Complete the setup by adding the application to a site.
Controlling your Python/uWSGI application¶
You can control the operation of your Python/uWSGI application using the scripts provided by the installer.
- To start the application:
/home/app_user/apps/app_name/start
- To stop the application:
/home/app_user/apps/app_name/stop
- To kill the application if it has hung:
/home/app_user/apps/app_name/kill
Installing Python dependencies for your project¶
Your Python/uWSGI application includes a Python 3 environment located at /home/app_user/apps/app_name/env
into which you can install your project's Python dependencies. To do so:
-
Activate your application's Python environment:
source /home/app_user/apps/app_name/env/bin/activate
-
Install your dependences with
pip
. You can install single packages...pip install packagename
...or if you have a requirements file for your project you can install all of the dependencies at once:
pip install -r requirements.txt
Connecting your Python/uWSGI application to your project¶
Your Python/uWSGI applications are initially configured to serve a simple "hello world" application. All of the configuration is done via the uwsgi.ini
configuration file in the application directory, so you can reconfigure the application to serve your own project by editing that configuration.
For example, if you have a Flask project that you'd like to serve via uWSGI:
-
Create a new Python/uWSGI application as described above.
-
Upload your Flask project directory to your application directory. For this example, the project directory is named
flaskapp
. -
Edit your application's
uwsgi.ini
configuration file (below the "adjust the following to point to your project" comment) to point it at your Flask app. For this example, we'll comment out the existing WSGI handler and use the uWSGImodule
directive instead.# adjust the following to point to your project #wsgi-file = /home/username/apps/appname/myapp/wsgi.py #touch-reload = /home/username/apps/appname/myapp/wsgi.py module = flaskapp:app touch-reload = /home/username/apps/appname/flaskapp/__init__.py
-
Use the application's
stop
andstart
commands to restart the application to pick up the new configuration: /home/username/apps/appname/stop /home/username/apps/appname/start
At this point, the application is now serving the flaskapp
project.
Performance tuning¶
If your Python/uWSGI application's performance is poor under heavy load, you may be able to improve it by increasing the values of workers
and threads
in your application's uwsgi.ini
configuration file. Increasing those values will increase the ability of the application to handle more traffic by adding more worker processes and by increasing the number of simultaneous requests that each worker can handle.
There is no magic formula for determining the number of workers and threads your application needs. You'll need to spend some time observing your application's performance while you are adjusting the configuration until you find the settings that work best for you.
Other configuration¶
You can use any uWSGI configuration you need by setting it in your application's uwsgi.ini
file. For more information please see:
- Configuring uWSGI
- uWSGI Options
Python Virtual Environments¶
Python virtual environments are isolated installations of Python that can be used to run applications with their own individual Python package dependencies.
Python 3 Virtual Environments¶
Python 3 includes native support for virtual environments via the venv
module.
-
Log in to a SSH session on your Opalstack server.
-
Execute the following command to create the virtual environment, changing
envname
to the name or full path of the Python virtual environment that you want to create:python3 -m venv envname
Python 2 Virtual Environments¶
Python 2 virtual environments require the use of the virtualenv
package.
-
Log in to a SSH session on your Opalstack server.
-
Execute the following commands to install the
virtualenv
package:export PATH=$HOME/.local/bin:$PATH pip2.7 install --user -U pip==20.3.4 ~/.local/bin/pip2.7 install --user virtualenv==20.15.1
Note that pip 20.3.4 is the highest version of pip that will work with Python 2.
-
Execute the following command to create the virtual environment, changing
envname
to the name or full path of the Python virtual environment that you want to create:virtualenv --python=python2.7 envname
Using Virtual Environments¶
When you want to use the virtual environment interactively, you must first activate it with the following command, again changing envname
to the name or full path of your Python virtual environment.
source envname/bin/activate
When you are finished working with the environment, deactivate it by running the deactivate
command.
Installing and upgrading Python packages¶
If you need Python packages that aren't installed systemwide then you can use pip
to install those packages.
With a virtual environment¶
First create an environment if you don't have one already:
python3 -m venv ~/apps/appname/env
... then activate the environment and then run pip
, eg:
source ~/apps/myapp/env/activate
pip install packagename
Without a virtual environment¶
Sometimes you might want to install a package globally in your home directory instead of in a particular environment. To do so, use the --user
flag when running pip
eg:
pip install --user packagename
To upgrade an installed package¶
If you've installed a package and need to upgrade it, include the -U
option, eg:
pip install --user -U packagename
or activate your environment and run:
pip install -U packagename