Virtualenv is a tool used to create an isolated Python environment. This environment has its own installation directories that doesn't share libraries with other virtualenv environments (and optionally doesn't access the globally installed libraries either).
Virtualenv is the easiest and recommended way to configure a custom Python environment.
Installing Virtualenv using pip3
Virtualenv is installed by default on all DreamHost servers for Python 2 versions. If you're working with Python 3, you should install virtualenv using pip3.
These instructions assume you've already installed a custom version of Python 3. After it's installed and your shell is using this version, run pip3 to install virtualenv:
[server]$ pip3 install virtualenv
Collecting virtualenv Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB) 100% |████████████████████████████████| 1.8MB 367kB/s Installing collected packages: virtualenv Successfully installed virtualenv-15.1.0
You'll need the full path to the Python 3 version of virtualenv, so run the following to view it:
[server]$ which virtualenv /home/username/opt/python-3.6.2/bin/virtualenv
Creating a virtual environment using a custom Python version
When working with virtual environments in Python, it's common to use a custom version of Python rather than the server's version. To create a new virtual environment using your custom installed version of Python, follow these steps:
The following steps use Python version 3.6.2. Make sure to use the version you installed.
- 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:
[server]$ which python3 /home/username/opt/python-3.6.2/bin/python
- Navigate to your site's directory, where you'll create the new virtual environment:
[server]$ cd ~/example.com
- Create the virtual environment while you specify the version of Python you wish to use. The following command creates 'my_project' in the first path, and uses the -p flag to specify the full path to the Python3 version you just installed:
[server]$ virtualenv ~/example.com/my_project -p /home/example_username/opt/python-3.6.2/bin/python3 Running virtualenv with interpreter /home/example_username/opt/python-3.6.2/bin/python3
Using base prefix '/home/example_username/opt/python-3.6.2'
New python executable in /home/example_username/example.com/env/bin/python3
Also creating executable in /home/example_username/example.com/env/bin/python
Installing setuptools, pip, wheel...done.
- This command 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.
- To activate the new virtual environment, run the following:
[server]$ source my_project/bin/activate
- To verify the correct Python version, run the following:
[server]$ python -V Python 3.6.2
Any package that you install using pip is now placed in the virtual environments project folder, isolated from the global Python installation.
Deactivating your virtualenv
When finished working in the virtual environment, you can deactivate it by running the following:
- This puts you back into your Shell user's default settings.
Deleting your virtual environment
To delete a virtual environment, simply delete the project folder. Using the previous example, run the following command:
[server]$ rm -rf /home/username/example.com/my_project
Installing custom modules
View the following article for information on how to use pip to install Python modules.