Using Virtual Environment
What is a Virtual Environment?
A Virtual Environment is a self contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages. To learn more about Virtual Environments see here.
Why should I use a Virtual Environment?
A Virtual Environment keeps all dependencies for the Python project separate from dependencies of other projects. This has a few advantages:
- It makes dependency management for the project easy.
- It enables using and testing of different library versions by quickly spinning up a new environment and verifying the compatibility of the code with the different version.
Python Version Requirement (Required)
This guide has been tested with Python 3.10.12. Newer versions might not have support for the dependent libraries, so are not recommended.
Use Conda (or Mamba)
While there are many options for setting up virtual environments for python, by far the most common and simpler approach is by using Anaconda (aka Conda). You can read the documentation on how to get started with Conda here.
Installing Pip (Required)
- Download the
get-pip.py
file using the commandcurl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
- Run the following
python3 get-pip.py
- Check pip version using
pip3 -V
Note (for Ubuntu users): If the
ModuleNotFoundError: No module named 'distutils.util'
error is encountered,
then python3-distutils needs to be installed. Install python3-distutils using
sudo apt-get install python3-distutils
Mac OS X Setup
- Create a folder where the virtual environments will reside
$ mkdir ~/python-envs
- To create a new environment named
sample-env
execute$ python3 -m venv ~/python-envs/sample-env
- To activate the environment execute
$ source ~/python-envs/sample-env/bin/activate
- Upgrade to the latest pip version using
$ pip3 install --upgrade pip
- Upgrade to the latest setuptools version using
$ pip3 install --upgrade setuptools
- To deactivate the environment execute
$ deactivate
(you can reactivate the environment using the sameactivate
command listed above)
Ubuntu Setup
- Install the python3-venv package using
$ sudo apt-get install python3-venv
- Follow the steps in the Mac OS X installation.
Windows Setup
- Create a folder where the virtual environments will reside
md python-envs
- To create a new environment named
sample-env
executepython -m venv python-envs\sample-env
- To activate the environment execute
python-envs\sample-env\Scripts\activate
- Upgrade to the latest pip version using
pip install --upgrade pip
- To deactivate the environment execute
deactivate
(you can reactivate the environment using the sameactivate
command listed above)
Note:
- Verify that you are using Python version 3.10.12. Launch a
command prompt using cmd
and execute python --version
to verify the version.
- Python3 installation may require admin privileges on Windows.
- This guide is for Windows 10 using a 64-bit architecture only.