Learning Python: Managing Packages and Dependencies

Global Python

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 entry:

    import sys
    for entry in sys.path:

Running the pip install <package> will install this package in the global site-package location.

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:
  1. Create a new folder to hold this project
  2. Change into this folder
  3. Run the python -m venv this will create a new set of sub-folders, including a site-packages folder for this project
  4. There will also be a scripts folder, run the source activate script to activate this Virtual Environment
  5. 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:
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

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 manually.

To add a package to the current PyCharm project (Virtual Environment), do this:
  1. Open the File menu
  2. Select Settings...
  3. Select Your Project
  4. Select Python Interpreter
  5. You will see a list of package in the current Virtual Environment
  6. Click the + (plus sign) on the right side of the list
  7. Enter the package name to search for
  8. Select the package
  9. Click the Install Package button, the package will be downloaded and installed in the project (Virtual Environment)