Python Virtual Environments
If you are working on multiple project, and these projects have conflicting package requirements,
using global packages will cause issues.
For example:
Project A requires version 2.13 of the requests package.
Project B requires version 2.20 of the requests package.
You will have a problem...
Python Virtual Environments to the rescue.
To create a Python Virtual Environment for a project, you:
- Create a new folder to hold this project
- Change into this folder
- Run the
python -m venv
this will create a new set of sub-folders, including a site-packages
folder for this project
- There will also be a scripts folder, run the
source activate
script to activate this Virtual Environment
- Now the pip install <package> command will install this package into this
Virtual Environment only
Please note, a Virtual Environment is not used to create a deployable project, but rather used
to create a sandbox for development.
To create a deployable project, you will list your external packages in a requirements.txt file,
and the installation of your package, will trigger the installation of your dependencies.
Here is the requirements.txt file for the pandas project:
numpy>=1.16.5
python-dateutil>=2.7.3
pytz
The command pip show pandas will show this:
pip show pandas
Name: pandas
Version: 1.1.2
Summary: Powerful data structures for data analysis, time series, and statistics
Home-page: https://pandas.pydata.org
Author: None
Author-email: None
License: BSD
Location: /home/jonathan/.local/lib/python3.6/site-packages
Requires: python-dateutil, pytz, numpy