Learning Python: Managing Packages and Dependencies
When Python is installed, it create a number of folders and settings. The Standard Library is
downloaded into one of these folders.
This code will display the Python package location(s), please note the site-package
for entry in sys.path:
Running the pip install <package> will install this package in the global site-package
Python Virtual Environments
If you are working on multiple project, and these projects have conflicting package requirements,
using global packages will cause issues.
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:
Please note, a Virtual Environment is not used to create a deployable project, but rather used
to create a sandbox for development.
- 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
script to activate this Virtual Environment
- Now the pip install <package> command will install this package into this
Virtual Environment only
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:
The command pip show pandas will show this:
pip show pandas
Summary: Powerful data structures for data analysis, time series, and statistics
Requires: python-dateutil, pytz, numpy
PyCharm and Virtual Environments
PyCharm defaults to a Virtual Environment for each project.
You can see this on the New Project dialog. You will also see a Lib (to hold site-packages)
and Scripts (to hold the activate script) folders.
Managing package dependencies in PyCharm is easy, and you will not need to run the pip command
To add a package to the current PyCharm project (Virtual Environment), do this:
- Open the File menu
- Select Settings...
- Select Your Project
- Select Python Interpreter
- You will see a list of package in the current Virtual Environment
- Click the + (plus sign) on the right side of the list
- Enter the package name to search for
- Select the package
- Click the Install Package button, the package will be downloaded and installed
in the project (Virtual Environment)