A virtual environment in Python allows you to install Python packages into the virtual, isolated environment so that project "foo" can be isolated from project "bar", and so on.
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 foo is created and will be created in the present working directory.
python -m virtualenv foo
If using version 3 of Python, the python3 -m venv <something unique> command can be used to create a virtual environment.
python3 -m venv foo
Optionally, you can specify a full path.
python3 -m venv /tmp/foo
This will create a directory named foo in your present working directory, and there will be files and directories below the foo directory. These files and directories make up the virtual environment.
~]$ ls -l
drwxr-xr-x. 5 john.doe users 74 Aug 25 06:08 foo
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 foo/
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/foo"
def subprocess_return_code(command):
result = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
streamdata = result.communicate()[0]
return result.returncode
# --------------------------------------------
# remove the /tmp/foo 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 "foo" 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 "foo" 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