pythonpippython-modulepython-packaging

What is the difference between installing a module with pip/setup.py vs adding it to PYTHONPATH?


I have a custom Python module I want to use in another project. This module gets deployed to production as well. I have two options:

  1. Add a setup.py and install the module locally with pip.
  2. Add the location of the module to the PYTHONPATH environment variable.

What is the difference? I know instinctively that using pip with a setup.py should be preferred, but can't point to any concrete evidence why. Does manually adding to PYTHONPATH have adverse effects (maybe by skipping some other actions pip install does in the background)?


Solution

  • The benefits of packaging are that it encapsulates the installation operation into a single, standardized, well-known command: pip install.

    If you have well-documented manual installation instructions or live in an environment where you don't need them (because you're in the same room as anyone who might ever need help, for example) you can skip or defer packaging the code; but in the vast majority of cases, the required effort is minuscule compared to the benefits.

    Notice that setup.py (or its more modern replacements) can and often does do more than just (cause pip to) copy the relevant files to a place in your sys.path. If your project requires data files or other additional infrastructure, there are more reasons to package it all up earlier.

    Finally, packaging is a stepping stone to building new tools which depend on this one. Packaging also encapsulates the automated handling of dependencies, so that pip, not your user, figures out how to pull in all packages that your package depends on, and conversely, arranges for future packages which depend on yours to access it.