how.wtf

Using Virtualenv with Python

· Thomas Taylor

What is Virtualenv?

Virtualenv is a tool used for creating isolated Python environments. It allows multiple Python environments/projects to coexist on the same computer with their own dependencies, python versions, and pip versions.

How to install Virtualenv

Using Python’s package manager, pip or pip3, virtualenv can be installed with a single command:

1pip install --user virtualenv

Optionally, you can install it for all users:

1pip install virtualenv

How do I create a new virtualenv environment?

Simply navigate to the project directory and use the virtualenv cli command with a name for the environment (venv in this case):

1$ cd ~/projects/myproject
2$ virtualenv venv
3created virtual environment CPython3.8.5.final.0-64 in 1993ms
4  creator CPython3Posix(dest=/home/user/projects/myproject, clear=False, no_vcs_ignore=False, global=False)
5  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/user/.local/share/virtualenv)
6    added seed packages: pip==21.0.1, setuptools==56.0.0, wheel==0.36.2
7  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

This creates a new folder named venv in the project directory.

How do I use/activate a virtualenv (venv) environment?

To activate the virtualized environment, run the following command from the project directory:

On Linux / MacOS:

1source venv/bin/activate

On Windows:

1.\venv\Scripts\activate

With the isolated environment activated, any pip install packagename will be contained within the directories of the virtualized environment.

How do I deactivate a virtualenv (venv) environment?

Simply type the following command to deactivate:

1deactivate

#python  

Reply to this post by email ↪