https://linuxize.com/post/how-to-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.
The output should look like this:
Starting from Python 3.6, the recommended way to create a virtual environment is to use the
Let’s start by installing the
Once the module in installed we are ready to create virtual environments for Python 3.
First switch to a 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:
The command above creates a directory called
To start using this virtual environment, you need to activate it by running the
Once activated, the virtual environment’s bin directory will be added at the beginning of the
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.
The first step is to install the module, using the Python package manager, pip:
To verify the installation your can try to import the module:
If there are no errors importing the module, then the installation was successful.
In our script 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:
Paste the following content to the file:
Close and save the file.
We can now run the script by typing:
The script will print a dictionary of all the header entries as shown bellow:
Once you are done with your work to deactivate the environment, simply type
If you are facing any problem, feel free to leave a comment.
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
Python 3.6.5
venv
module.Let’s start by installing the
python3-venv
package that provides the venv
module.sudo apt install python3-venv
First switch to a 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
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
$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) $
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
.(my-project-env) $ pip install requests
(my-project-env) $ import requests
In our script 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:
(my-project-env) $ nano testing.py
import requests
r = requests.get('http://httpbin.org/get')
print(r.headers)
We can now run the script by typing:
(my-project-env) $ python testing.py
{'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'}
deactivate
and you will return to your normal shell.(my-project-env) $ 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 problem, feel free to leave a comment.