Python 2 has been EOL'd and is no longer receiving security updates. You can read more about this here:
Python 2 is still available on DreamHost servers, however, it's recommended you upgrade to Python 3 to ensure your applications are secure going forward.
This article uses Python version 2.7. If you want to set up virtualenv using Python 3, view the following article instead:
Virtualenv is a tool used to create an isolated Python environment. This environment has its own installation directories that do not share libraries with other virtualenv environments or the globally installed libraries on the server.
Virtualenv is the easiest and recommended way to configure a custom Python environment.
Virtualenv is installed by default on DreamHost servers for Python 2, so you do not need to install it to run it. However, it's recommended you install a custom version of Python as doing so also installs pip.
Creating a virtual environment using a custom Python version
When working with virtual environments in Python, it's recommended to use a custom version of Python rather than the server's version. View the following article to install any version of Python you like.
To create a new virtual environment using this custom Python version, follow these steps:
- Make a note of the full file path to the custom version of Python you just installed. If you've followed the instructions in the installation article, the full path is:
- Navigate to your site's directory. This is where you'll create the new virtual environment:
[server]$ cd ~/example.com
- Create the virtual environment at the same time you specify the version of Python you wish to use. Make sure to change username to your Shell user. In this example, the project is titled venv, but you can name this anything you like.
Make sure you specify the path to your custom installation of Python 2. If you do not, you'll end up using the system version of Python.
[server]$ virtualenv -p /home/username/opt/python-2.7.15/bin/python venv
- This creates a local copy of your environment specific to this website. While working on this website, you should activate the local environment in order to make sure you're working with the right versions of your tools and packages.
- Run the following to activate this new virtual environment:
[server]$ source venv/bin/activate
- Finally, check the version of Python being used:
(venv) [server]$ python -V Python 2.7.15
From now on, any package that you install using pip is placed in the virtual environments project folder, isolated from the global Python installation.
It's a good idea to upgrade the version of pip to ensure you can install current modules.
(venv) [server]$ python -m pip install --upgrade pip
You can then check the version.
(venv) [server]$ pip --version
Deactivating your virtualenv
If you are finished working in the virtual environment for the moment, you can deactivate it by running the following:
(venv) [server]$ deactivate
- This puts you back into your Shell user's default settings.
Deleting your virtual environment
To delete a virtual environment, just delete project folder. In this case, it would be:
[server]$ rm -rf venv
Installing custom modules
View the following article for information on how to use pip to install Python modules.