I am trying to understand where and how are Python packages installed and stored on Linux and find the best location to store them. My repo is Debian Wheezy.

I would like to store all my custom Python modules in one folder.

I am confused, because

1. I can see certain packages (installed with apt-get), like pygame in all the following places:

  • /usr/share/pyshared/pygame
  • /usr/lib/pyshared/python2.7/pygame
  • /usr/lib/pyshared/python2.6/pygame
  • /usr/lib/python2.7/dist-packages/pygame
  • /usr/lib/python2.6/dist-packages/pygame

With some files being linked from pyshared -> to lib/python2.7/dist-packages

2. Some other packages are installed to /usr/share/pysharedand then linked to /usr/lib/python2.6 and 2.7.

3. Finally, when I install something with easy_install it gets into /usr/local/lib/python2.7/dist-packages/ into something .egg.

In terms of all this chaos, what do you believe is the right way to keep custom modules and what is the best file format for them? Should single file modules should just be called something.py while multiple file modules should be in a folder with a __init__.py in them? Is that right? Is there any reason to keep single-file modules in folders as well?

And most importantly where should I keep them? /usr/local/lib/python2.7/my-packages/, or inside dist-packages?

  • I've often had this question or a variation of it. It seems important to me especially (thinking as a sysadmin) in terms of keeping one's systems predictable and understandable for others who may have to manage them as well. Essentially the question is "what is the accepted convention for locating custom packages?". Unfortunately I've yet to get a good answer, and experienced Python people seem to consider it a dumb question. But I think this is part of the cause of the 'chaos' you describe. The question is not 'what will work?' but 'what is least surprising?'. – robo Jan 24 '15 at 17:21

You can keep them wherever you want provided you change $PYTHONPATH accordingly. I wouldn't put them to /usr/lib/ because it's reserved for files that belong to .deb packages and can be easily uninstalled by dpkg/apt-get. If you want to keep your system clean of course.

You're right about single file modules.

If I were you, I'd put them in /opt/my-python-packages or ~/my-python-packages - to distinguish between third-party packages installed by easy_install and my own.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.