Skip to content

Latest commit

 

History

History
129 lines (88 loc) · 3 KB

File metadata and controls

129 lines (88 loc) · 3 KB

virtualenv

virtualenv is a tool to create isolated Python environments.

Since Python 3.3, a subset of virtualenv has been integrated into the standard library under the venv module. However, virtualenv contains more features that enables virtualenv management to be more faster, extendable, and flexible.

How to install?

Here is using pip to install the virtualenv package,

$ pip install --user virtualenv

There are several more methods to install virtualenv, like using pipx, wheel, sdist, etc., please refer to the official documentation for more information.There are also details about the compatibility of virtualenv with differnet Python interpreters and OS environments.

How to use it?

Use the --help command,

$ virtualenv --help

you can find out these rich flag options of virtualenv.

1. Create a virtual environment

Run this command to create a virtual environment,

$ virtualenv testenv

What's inside after the creation?

testenv
├── bin
│   ├── activate
│   ├── activate.csh
│   ├── activate.fish
│   ├── activate.nu
│   ├── activate.ps1
│   ├── activate_this.py
│   ├── deactivate.nu
│   ├── pip
│   ├── pip3
│   ├── pip-3.12
│   ├── pip3.12
│   ├── python -> /usr/bin/python3.12
│   ├── python3 -> python
│   ├── python3.12 -> python
│   ├── wheel
│   ├── wheel3
│   ├── wheel-3.12
│   └── wheel3.12
├── lib
│   └── python3.12
└── pyvenv.cfg

3 directories, 19 files

2. Activate the virtual environment

Source to activate the virtual environment,

$ source testenv/bin/activate

You may need to run different activation file based on your system.

3. Manage Python packages

To install or uninstall a single package, run

$ pip install <package-name>

To install a list of Python packages in requirements.txt, run

$ pip install -r requirements.txt

To uninstall the Python package,

$ pip uninstall <package-name>

4. Deactivate the virtual environment

Run the following command to deactivate the virtual environment,

$ deactivate

5. Remove the virtual environment

Removing the virtual environment is simple, just to remove the virtual environment directory.

$ rm -r testenv

Different Python versions?

If you have multiple Python versions installed, then you can use --python/-p option to specify the Python version when creating the virtual environment. For example,

$ virtualenv -p python3.10 testenv10
$ virtualenv -p python3.11 testenv11
$ virtualenv -p python3.12 testenv12

Happy coding!