Python Environment

venv (for Python 3) and virtualenv (for Python 2) allow you to manage separate package installations for different projects. They essentially allow you to create a “virtual” isolated Python installation and install packages into that virtual installation. When you switch projects, you can simply create a new virtual environment and not have to worry about breaking the packages installed in the other environments. It is always recommended to use a virtual environment while developing Python applications.”

References:

Installing virtualenv

On macOS and Linux:

python3 -m pip install --user virtualenv

On Windows:

py -m pip install --user virtualenv

TODO: Add images

Creating a virtual environment

“To create a virtual environment, go to your project’s directory and run venv. If you are using Python 2, replace venv with virtualenv in the below commands.”

On macOS and Linux:

python3 -m venv env

On Windows:

py -m venv env

Note: you can just run virtualenv env or venv env since virtualenv and venv should be recognized.

“The second argument is the location to create the virtual environment. Generally, you can just create this in your project and call it env.”

“Note: You should exclude your virtual environment directory from your version control system using .gitignore or similar.”

TODO: Add images

Reference: https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/

Activating a virtual environment

Before you can start installing or using packages in your virtual environment you’ll need to activate it. Activating a virtual environment will put the virtual environment-specific python and pip executables into your shell’s PATH.

On macOS and Linux:

$ source env/bin/activate

On Windows:

$  .\env\Scripts\activate

TODO: Add images

Reference: https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/

Leaving the virtual environment

If you want to switch projects or otherwise leave your virtual environment, simply run:

$ deactivate

Reference: https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/

Journal