How to Create Python Virtual Environments on Ubuntu 18.04

Updated on

3 min read

Create Python Virtual Environments on Ubuntu 18.04

Python virtual environment is a self-contained directory tree that includes a Python installation and number of additional packages.

The main purpose of Python virtual environments is to create an isolated environment for different Python projects. This way you can install a specific version of a module on a per project basis without worrying that it will affect your other Python projects.

In this tutorial, we’ll provide a step by step instructions about how to create Python virtual environments on Ubuntu 18.04.

Create Virtual Environment for Python 3

Ubuntu 18.04 ships with Python 3.6 by default. You can verify that Python 3 is installed on your system by running:

python3 -V

The output should look like this:

Python 3.6.5
If you want to install the latest major release of the Python language, Python 3.8 follow this instructions .

Starting from Python 3.6, the recommended way to create a virtual environment is to use the venv module.

Let’s start by installing the python3-venv package that provides the venv module.

sudo apt install python3-venv

Once the module is installed we are ready to create virtual environments for Python 3.

Switch to the directory where you would like to store your Python 3 virtual environments. Within the directory run the following command to create your new virtual environment:

python3 -m venv my-project-env

The command above creates a directory called my-project-env, which contains a copy of the Python binary, the Pip package manager, the standard Python library and other supporting files.

To start using this virtual environment, you need to activate it by running the activate script:

source my-project-env/bin/activate

Once activated, the virtual environment’s bin directory will be added at the beginning of the $PATH variable. Also your shell’s prompt will change and it will show the name of the virtual environment you’re currently using. In our case that is my-project-env:

$ source my-project-env/bin/activate
(my-project-env) $

Now that the virtual environment is activated, we can start installing, upgrading, and removing packages using pip.

Let’s create a simple Python script utilizing the Requests module.

Within the virtual environment, you can use the command pip instead of pip3 and python instead of python3.

The first step is to install the module, using the Python package manager, pip:

pip install requests

To verify the installation you can try to import the module:

python -c "import requests"

If there are no errors importing the module, then the installation was successful.

In this example we are going to use the httpbin.org site that provides a simple HTTP Request & Response service to print all the header entries.

Open your text editor and create a new file:

nano testing.py

Paste the following content to the file:

import requests

r = requests.get('http://httpbin.org/get')  
print(r.headers)  

Close and save the file.

We can now run the script by typing:

python testing.py

The script will print a dictionary of all the header entries as shown below:

{'Connection': 'keep-alive', 'Server': 'gunicorn/19.9.0', 'Date': 'Tue, 18 Sep 2018 16:50:03 GMT', 'Content-Type': 'application/json', 'Content-Length': '266', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true', 'Via': '1.1 vegur'}

Once you are done with your work to deactivate the environment, simply type deactivate and you will return to your normal shell.

deactivate

Conclusion

You have learned how to create and use Python virtual environments. You can repeat the steps we outlined above and create additional virtual environments for your Python projects.

If you are facing any problems, feel free to leave a comment.