PYTHONPATH vs. sys.path

Question:

Another developer and I disagree about whether PYTHONPATH or sys.path should be used to allow Python to find a Python package in a user (e.g., development) directory.

We have a Python project with a typical directory structure:

Project
    setup.py
    package
        __init__.py
        lib.py
        script.py

In script.py, we need to do import package.lib. When the package is installed in site-packages, script.py can find package.lib.

When working from a user directory, however, something else needs to be done. My solution is to set my PYTHONPATH to include "~/Project". Another developer wants to put this line of code in the beginning of script.py:

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

So that Python can find the local copy of package.lib.

I think this is a bad idea, as this line is only useful for developers or people running from a local copy, but I can’t give a good reason why it is a bad idea.

Should we use PYTOHNPATH, sys.path, or is either fine?

Asked By: google cloud sucks

||

Answers:

If the only reason to modify the path is for developers working from their working tree, then you should use an installation tool to set up your environment for you. virtualenv is very popular, and if you are using setuptools, you can simply run setup.py develop to semi-install the working tree in your current Python installation.

Answered By: Ned Batchelder

I think, that in this case using PYTHONPATH is a better thing, mostly because it doesn’t introduce (questionable) unneccessary code.

After all, if you think of it, your user doesn’t need that sys.path thing, because your package will get installed into site-packages, because you will be using a packaging system.

If the user chooses to run from a “local copy”, as you call it, then I’ve observed, that the usual practice is to state, that the package needs to be added to PYTHONPATH manually, if used outside the site-packages.

Answered By: shylent

I hate PYTHONPATH. I find it brittle and annoying to set on a per-user basis (especially for daemon users) and keep track of as project folders move around. I would much rather set sys.path in the invoke scripts for standalone projects.

However sys.path.append isn’t the way to do it. You can easily get duplicates, and it doesn’t sort out .pth files. Better (and more readable): site.addsitedir.

And script.py wouldn’t normally be the more appropriate place to do it, as it’s inside the package you want to make available on the path. Library modules should certainly not be touching sys.path themselves. Instead, you’d normally have a hashbanged-script outside the package that you use to instantiate and run the app, and it’s in this trivial wrapper script you’d put deployment details like sys.path-frobbing.

Answered By: bobince

Along with the many other reasons mentioned already, you could also point outh that hard-coding

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

is brittle because it presumes the location of script.py — it will only work if script.py is located in Project/package. It will break if a user decides to move/copy/symlink script.py (almost) anywhere else.

Answered By: unutbu

In general I would consider setting up of an environment variable (like PYTHONPATH)
to be a bad practice. While this might be fine for a one off debugging but using this as
a regular practice might not be a good idea.

Usage of environment variable leads to situations like “it works for me” when some one
else reports problems in the code base. Also one might carry the same practice with the
test environment as well, leading to situations like the tests running fine for a
particular developer but probably failing when some one launches the tests.

Answered By: sateesh

Neither hacking PYTHONPATH nor sys.path is a good idea due to the before mentioned reasons. And for linking the current project into the site-packages folder there is actually a better way than python setup.py develop, as explained here:

pip install --editable path/to/project

If you don’t already have a setup.py in your project’s root folder, this one is good enough to start with:

from setuptools import setup
setup('project')
Answered By: Peter