TrickLogo

HOWTO Setup a Python Virtual Environment

Using the Built-in venv Module in Python 3

Creating a Virtual Environment

The following command creates a virtual Python environment:

% python3 -m venv <path-of-virtual-environment>

This command runs the venv module, to create a virtual environment. The directory specified by <path-of-virtual-environment> is created to store the resources of the environment. It contains scripts to activate, deactivate, and otherwise configure the environment. It also provides a place to install Python modules for that particular environment. One can create multiple virtual environments, each with different resources.

For example, the following will create a Python virtual environment called myVenv in your home directory.

% python3 -m venv ~/myVenv

Activating the Virtual Environment

To activate the virtual environment, execute the following:

% source myVenv/bin/activate

Note that the name of virtual environment is added to the command prompt.

Installing Python Modules Into Your Virtual Environment

Use the following command to install Python modules into the virtual environment:

(myVenv) % python3 -m pip install <package-name>

For example, the Trick test suite, which uses TrickOps which requires PyYAML. This Python module would be installed as follows:

(myVenv) % python3 -m pip install PyYAML

Every time myVenv is activated, the PyYAML module will be available.

Deactivating the venv Shell

To deactivate the venv shell, execute the following:

(myVenv) % deactivate

The above should get you going. If you need more details, the following tutorial is pretty good. RealPython Tutorial.

Using Conda

Conda is a powerful package manager and environment manager that you use with command line commands at the Anaconda Prompt for Windows, or in a terminal window for macOS or Linux.

You can obtain conda by installing Miniconda or Anaconda.

Miniconda a small bootstrap version of Anaconda that includes only conda, Python, the packages they both depend on, and a small number of other useful packages (like pip, zlib, and a few others).

Anaconda is a downloadable, free, open-source, high-performance, and optimized Python and R distribution. It includes conda, conda-build, Python, and 250+ automatically installed, open-source scientific packages and their dependencies that have been tested to work well together, including SciPy, NumPy, and many others.

Creating a Conda Environment with Commands

Create a conda virtial environment with Python by running one of following conda commands from a terminal:

# A specific version of Python
% conda create --name trick python=3.9.18
or
% conda create -n trick python=3.9.18

# The latest version of Python 3.9
% conda create -n trick python=3.9

# The lastest version of Python
% conda create -n trick python

# The latest version of Python 3.9 and packages
% conda create -n trick python=3.9 pyyaml scipy

Creating a Conda Environment From a YAML File

Create the file myenv.yml with following contents:

name: trick
channels:
    - conda-forge
    - defaults
dependencies:
    - python = 3.9
    - pyyaml

In this example, the environment is named trick and includes two packages: python and pyyaml.

Run conda command to create the new environment:

Once you have your YAML file ready, you can create your conda environment using the following command in your terminal:

% conda env create -f myenv.yml

Activating the Conda Environment

After creating the environment, you can activate it using the following command:

% conda activate trick

Installing Packages Into a Conda Environment

If you're in your conda environment, you can install package(s) using the following command:

(trick) % conda install numpy scipy

If you're NOT in your conda environment, you can install package(s) into a specified environment using the following command:

% conda install -n trick numpy scipy

Deactivating an Active Conda Environment

If you're in your conda environment, you can deactivate it using the following command:

(trick) % conda deactivate

Removing a Conda Environment

You can remove a conda environment from your terminal using the following command:

% conda remove -n trick --all

or

% conda env remove -n trick

To verify that the environment was removed, run following from your terminal:

% conda info --envs

The removed environment should not be shown.

References