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.

1

Log in to a SSH session on your Opalstack server.

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:

python3 -m venv envname

Python 2 Virtual Environments

Python 2 virtual environments require the use of the virtualenv package.

1

Log in to a SSH session on your Opalstack server.

2

Execute the following commands to install the virtualenv package:

pip2 install --user zipp==1.1
pip2 install --user  virtualenv==16.7.10
3

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.