Skip to content

Instantly share code, notes, and snippets.

@jreniel
Last active February 2, 2020 14:32
Show Gist options
  • Save jreniel/c2dd4f2f68f9d8172355461b5337f236 to your computer and use it in GitHub Desktop.
Save jreniel/c2dd4f2f68f9d8172355461b5337f236 to your computer and use it in GitHub Desktop.
Quick notes on Python virtual environments.

Quick notes on Python virtual environments

There are many reason why someone would want to install certain software packages to a Python virtual environment instead of using the system package manager. In general, the use of Python virtual environments is recommended when:

  • the package you want to install is at an early stage of development, and is not yet available in a public release format through PyPi (meaning you can't simply do 'pip install mymodule' in your terminal because the package is not available in PyPi).
  • your operating system does not provide a specific version of Python that is required to run the code.
  • you are working as a developer on a project.
  • you want to test the code in multiple Python versions.
  • you want to meet certain library dependencies by installing them to an isolated system environment that does not affect the global scope of the user.

This is an incomplete list of reasons, but I hope these are enough to give the general sense of why and when to use Python virtual environments.

How to create Python virtual environments

There are several options to create and manage Python environments. I will briefly describe two of the most popular methods to create and manage Python environments.

Using conda (the "easier" method)

Using conda might be the simplest method and is also recommended when the operating system doesn't provide a particular version of Python. It is also the simplest method for begginers. Download and install Anaconda or Miniconda (recommended). Create a python environment using the target python version:

conda create -n myenv python=3.6

Now you can acces your python3.6 by activating the "myenv" environment:

conda activate myenv

You can substitute the "myenv" name for the real name of your project, so that switching to the correct environments becomes easier to remember and more intuitive for daily use. You may omit specifying a Python version and it should install the latest available. When you activate a Python environment, every package that you install using Python setuptools will be installed to the current active environment. See this link for a more complete guide on using conda environments.

Using venv (the "canonical" method)

For creating a Python virtual environment, you may use venv, provided that the system has the required Python version. For example, if you require a Python3.8 environment the you may try:

python3.8 -m venv ~/myenv

You may substitute the number for the proper Python version you are trying to use, or you may omit the number to use the system's default Python version. This will create a directory create a directory called "~/myenv". Please chose an appropriate directory name and location for your project. A recommended method is to clone the project, cd into it and then create a virtual environment. For simplicity, suppose the name of the project is "myproject".

git clone https://github.com/myproject-dev/myproject.git  # clone repo
cd myproject  # cd into it
python3.8 -m venv .myproject_venv  # create a hidden directory containing the Python environment (uses Python3.8)
source .myproject_venv/bin/activate  # activate the Python virtual environment
pip install .  # installs the package to the current active environment.

Pro-tip: create a shell alias for your environment.

You can create an alias to quickly load the Python environment with a single keyword. If using venv:

echo 'alias myproject="source ~/myproject/.myproject_env/bin/activate"' >> ~/.bashrc

or if using conda (where the environment has the same name as the project, called "myproject", following the example above):

echo 'alias myproject="conda activate myproject"' >> ~/.bashrc

Now you can activate your Python virtual environment with a single command:

myproject     # this single command will activate the python environment for your project.

References:

https://towardsdatascience.com/understanding-python-virtual-environments-7ccf690320d7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment