A virtual environment in Python allows you to install Python packages into the virtual, isolated environment so that project "my_python_virtual_environment" can be isolated from other installations of Python.
If using version 2 of Python, the python -m virtualenv <something unique> command can be used to create a virtual environment. In this example, a virtual environment named my_python_virtual_environment is created and will be created in the present working directory.
python -m virtualenv my_python_virtual_environment
If using version 3 of Python, the python3 -m venv <something unique> command can be used to create a virtual environment.
python3 -m venv my_python_virtual_environment
Optionally, you can specify a full path.
python3 -m venv /tmp/my_python_virtual_environment
This will create a directory named my_python_virtual_environment in your present working directory, and there will be files and directories below the my_python_virtual_environment directory. These files and directories make up the virtual environment.
~]$ ls -l
drwxr-xr-x. 5 john.doe users 74 Aug 25 06:08 my_python_virtual_environment
source can then be used to activate the Python virtual environment.
[john.doe@server1 ~]$ source my_python_virtual_environment/bin/activate
(my_python_virtual_environment) [john.doe@server1 ~]$
And the pip list command should return something like this, showing the packages installed in the Python virtual environment.
(my_python_virtual_environment) [john.doe@server1 ~]$ pip list
Package Version
---------- -------
pip 21.3.1
setuptools 59.6.0
WARNING: You are using pip version 21.3.1; however, version 25.2 is available.
You should consider upgrading via the '/home/john.doe/my_python_virtual_environment/bin/python3 -m pip install --upgrade pip' command.
Almost always, you should first and foremost upgrade pip in the Python virtual environment.
(my_python_virtual_environment) [john.doe@server1 ~]$ python3 -m pip install --upgrade pip
Requirement already satisfied: pip in ./my_python_virtual_environment/lib/python3.9/site-packages (21.3.1)
Collecting pip
Downloading pip-25.2-py3-none-any.whl (1.8 MB)
|████████████████████████████████| 1.8 MB 6.3 MB/s
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 21.3.1
Uninstalling pip-21.3.1:
Successfully uninstalled pip-21.3.1
Successfully installed pip-25.2
(my_python_virtual_environment) [john.doe@server1 ~]$ pip list
Package Version
---------- -------
pip 25.2
setuptools 59.6.0
If you are using Git to version control files and directories, you should add the virtual environment to the .gitignore file, like this.
~]$ ls -l my_python_virtual_environment/
drwxr-xr-x. 2 john.doe users 186 Mar 21 04:58 bin
drwxr-xr-x. 2 john.doe users 6 Mar 21 04:54 include
drwxr-xr-x. 3 john.doe users 23 Mar 21 04:54 lib
lrwxrwxrwx. 1 john.doe users 3 Mar 21 04:54 lib64 -> lib
-rw-r--r--. 1 john.doe users 61 Mar 21 04:58 pip-selfcheck.json
-rw-r--r--. 1 john.doe users 69 Mar 21 04:54 pyvenv.cfg
And here is an example of how you could create a virtual environment in a Python script.
#!/usr/bin/python3
import sys
import subprocess
base_virtual_environment_directory = "/tmp/my_python_virtual_environment"
def subprocess_return_code(command):
result = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
streamdata = result.communicate()[0]
return result.returncode
# --------------------------------------------
# remove the /tmp/my_python_virtual_environment directory if it exists
# --------------------------------------------
return_code = subprocess_return_code("rm -rf "+base_virtual_environment_directory)
if return_code != 0:
print(fFailed to remove the {base_virtual_environment_directory} virtual environment directory")
sys.exit()
else:
print(f"Successfully removed the {base_virtual_environment_directory} directory")
# --------------------------------------------
# create the "my_python_virtual_environment" python3 virtual environment
# --------------------------------------------
return_code = subprocess_return_code(f"python3 -m venv {base_virtual_environment_directory}")
if return_code != 0:
print(f"Failed to create the python3 {base_virtual_environment_directory} virtual environment")
sys.exit()
else:
print(f"Successfully created the python3 {base_virtual_environment_directory} virtual environment")
# -----------------------------------------------------
# upgrade pip in the "my_python_virtual_environment" python3 virtual environment
# -----------------------------------------------------
return_code = subprocess_return_code(base_virtual_environment_directory+"/bin/pip install --upgrade pip 2>/dev/null")
if return_code != 0:
print("Failed to upgrade pip in the python3 winrm virtual environment")
sys.exit()
else:
print("Successfully updated pip in the python3 winrm virtual environment")
Did you find this article helpful?
If so, consider buying me a coffee over at 