virtual env reference
19 August 2022 (Updated 19 August 2022)
What is a virtual environment?
In Python, a virtual environment lets you install packages in an isolated location – usually for a specific application – instead of globally. This lets each application have its own isolated environment with only the packages it needs.
Managing virtual environments with a requirements.txt file
It seems like Pipenv is the best approach for python package management. But lots of projects still manage virtual environments using a workflow like this:
- Activate a virtual env when working on a project.
- Install packages using
pip install <package>
. - Update requirements.txt using
pipreqs
orpip freeze
. - Commit updates to
requirements.txt
. - Activate different virtual env when working on a different project.
Recipes
Make sure you’ve installed virtualenv
globally with pip install virtualenv
.
Create virtual env
python -m virtualenv <name>
Activate venv
source <name>bin/activate
Deactivate venv
If you’ve already activated a venv, you’ll have access to the deactivate
command:
deactivate
Other notes
- When you activate a virtual env, it seems like the virtual env’s
bin
directory gets added to your path. This is why you have access to commands likedeactivate
(which lives at<venv-path>/bin/deactivate
.
Sources
Tagged:
Python
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment