This guide helps to set up a python virtual environment on Ubuntu.
First, I will check the current version of python on my ubuntu machine
$ python3 -V
Now install the python3-venv package that provides the venv module
$ sudo apt update
$ sudo apt install python3-venv
Creating virtual environment
once, you install the python3-venv package. Your system is ready to create a virtual environment
$ python3 -m venv env
the above command creates a directory called env, which is a copy of Python.
Activating Virtual Environment on Ubuntu
In order to activate the virtual environment, I will run the activate script
$ source env/bin/activate
(env) $
or
$ . env/bin/activate
Deactivate
To deactivate just type the “deactivate” command
$ deactivate
Installing Module in Virtual Environment
use pip command to install packages in your virtual environment
(env) $ pip install requests
Running python in Virtual Environment
using python command you can run your python script
(env) $ python <scriptname>