How to Set Up a Python Virtual Environment

1304
python virtual environment
Python

Trying to install packages and modules for use with python on a system that you do not have administrator rights on is a challenge. Not having complete control over your python environment means that you will often run into trouble when trying to run your scripts. The best solution to this common issue is to use a python virtual environment. Here, we take you through the steps on how to create a python virtual environment.

Why Do You Need to Use Python Virtual Environments?

Well, in all honesty, you don’t need to use Python virtual environments, but they certainly do help make your workflows more efficient. Virtual environments are isolated workspaces that contain all of the dependencies you need for that specific project without experiencing any of the conflicts that you might regularly see in a global set up.

Third party Python scripts are available for practically everything you need to do, so rather than write a new script yourself, you may opt to download an existing one. However, the script might have been written on a different version of Python that your system has installed by default, or it may require different versions of dependences that you are running or might install.

By setting up a Python virtual environment for your project, you can keep all of these factors separate so that you never run into conflicts with your other work. While it might take some time to set up, your virtual environments will definitely make your life easier.

How Many Python Virtual Environments Can You Have?

The amount of Python virtual environments you have depends on what you need to do. There are no limited to how many you have installed on your system, but a common configuration is for those who need to regularly use both Python 2 and Python 3 environments.

There is no limit to the number of virtual environments you run. You will only invoke one at a time, so there is no downside to having a separate environment for every project you work on.

How to Install a Python Virtual Environment

The first step to installing a Python virtual environment is to ensure that you have Python and pip installed. Check that you have Python installed by typing the following into your command line:

python --version

On most Linux and OSX systems, you will likely see an output like this, which tells you which version you are running:

Python 3.7.3

If you don’t, then you can find instructions for installing Python for a range of different system types from the official Python website.

Pip is an installer package for Python. If you are running a Python version higher than 2.7.9 (Python 2) or 3.4 (Python 3) then you likely already have pip installed. Check that pip is installed by typing:

pip --version

You will either receive the version and location of your pip install, or your system will tell you that pip is not installed. If you need to install pip, then you can find information on the official pip page.

Once pip is all ready to go, the next step is to install the virtualenv package. This is performed through the following command:

pip install virtualenv

A successful installation will look like this:

Collecting virtualenv
  Downloading https://files.pythonhosted.org/packages/62/77/6a86ef945ad39aae34aed4cc1ae4a2f941b9870917a974ed7c5b6f137188/virtualenv-16.7.8-py2.py3-none-any.whl (3.4MB)
    100% |████████████████████████████████| 3.4MB 3.7MB/s 
Installing collected packages: virtualenv
Successfully installed virtualenv-16.7.8

Now you’re ready to go!

Setting Up Your First Python Virtual Environment

Create the Virtual Environment

Create a folder for your project, navigate into the folder, and then create the virtual environment for the project using the following commands. Note, that venv is the name of the environment in this example, but you can call it whatever you want.

mkdir project_folder
cd project_folder
virtualenv venv

Activate the Virtual Environment

Once you have created the environment, you need to activate it before you can work within it. Use the following command to activate the environment:

source venv/bin/activate

You will notice that there’s an addition to your command line prompt: (venv) appears at the beginning of the line. This means that you are now working within your virtual environment!

Deactivate the Virtual Environment

If you want to deactivate the environment, you simply need to use the deactivate command:

deactivate

Installing a Virtual Environment for a Specific Python Version

If you have Python 3 installed by default on your system, but you need to use Python 2 for your environment, then you can specify the version when you run the virtualenv command with the -p flag:

virtualenv -p /usr/bin/python2.7 venv

Conclusion

Now that you have the ability to install, create and use Python virtual environments, you have the power to make your work processes more efficient. Avoid conflicts and keep your projects neat and tidy with just a few simple commands! Stay tuned for more of our Python tutorials!