How to do it...

  1. To create the new environment in the myenv directory, use the following command in Linux and macOS:
python3 -m venv myenv
  1. In Windows, the command to be used is as follows:
python -m venv myenv

This creates an environment in the myenv subdirectory of the current directory. The -m switch is used because venv is a Python module that is being executed.

  1. To switch to the new environment on Linux and macOS, run the following in the command line:
source myenv/bin/activate
  1. On Windows, we use the following command:
myenvinactivate
  1. If we need to install a package in the new environment, we use pip or pip3 as usual. For example, to install a molecule package using pip3, we execute the following command:
pip3 install molecule
  1. Optionally, run the following command to create a requirements file:
pip3 freeze > requirements.txt

This will generate a report listing the packages required by the current environment and store the information in the requirements.txt file. This file can be distributed with the package being developed to facilitate installation by users that use pip.

  1. When done working in the environment, deactivate it by running the following command:
deactivate
  1. If we no longer need an environment, we can delete it as follows. First, activate the environment using the following commands:
    On Linux and macOS, use the following:
source myenv/bin/activate

On Windows, use the following:

myenvinactivate
  1. Create a list of all packages in the environment with the following command:
pip3 freeze > requirements.txt
  1. Remove all packages installed in the environment with the following command:
pip3 uninstall -y -r requirements.txt
  1. The -y switch prevents pip from asking for confirmation to uninstall each individual package. Next, deactivate the environment by running the following command:
deactivate
  1. Finally, remove the environment directory with the following commands:

On Linux and macOS use the following:

rm -rf myenv

On Windows use the following:

rmdir /s /q myenv
It is important to uninstall all packages using pip3 because some packages will copy files in directories outside of the environment directory. Typically, these directories are:
On Linux and Windows: The .local folder located in the user's home folder.
On macOS: /Users/username/Library/Python/x.x/bin, where username is the current user name and x.x denotes the version of Python the environment is based on.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset