Python – Virtual Environment Tutorial

By July 29, 2022 No Comments

A virtual environment in Python allows you to setup a folder for dependencies that could be used among a few projects (i.e. data science projects that need base conda libraries) or isolated per project to ensure no conflicts. This prevents conflicting having a project that needs library v1.0 and another project needing v2.0.

To create a new virtual environment, invoke -m venv ENV_NAME from a python command line.

python -m venv new-project-env

Then activate the virtual environment, invoke source ENV_NAME/bin/activate

source new-project-env/bin/activate

You should see what virtual environment you are ‘in’ by looking at your terminal prefix (ENV_NAME).

To install dependencies, pip install LIBRARY_NAME # search for libraries by navigating to

pip install schedule

To list all the dependencies that installed in the virtual env

pip list

To output a list of the installed dependencies to persist to source control or pass along to another developer

pip freeze > requirements.txt