Where shall I put my self-written Python packages?

python

Solution

Thanks to the two additional links, I found not only the intended answer to my question, but also a solution that I like even more and that - ironically - was also explained in my first search result, but obfuscated by all the version-(in)dependent site-package lingo.

Answer to original question: default folder

I wanted to know if there was a canonical (as in "default") location for my self-written packages. And that exists:

>>> import site
>>> site.USER_SITE
'C:\\Users\\ojdo\\AppData\\Roaming\\Python\\Python27\\site-packages'

And for a Linux and Python 3 example:

ojdo@ubuntu:~$ python3
>>> import site
>>> site.USER_SITE
'/home/ojdo/.local/lib/python3.6/site-packages'

The docs on user scheme package installation state that folder `USER_SITE` - if it exists - will be automatically added to your Python's `sys.path` upon interpreter startup, no manual steps needed.

Bonus: custom directory for own packages

- Create a directory anywhere, e.g. `C:\Users\ojdo\Documents\Python\Libs`.

- Add the file `sitecustomize.py` to the site-packages folder of the Python installation, i.e. in `C:\Python27\Lib\site-packages` (for all users) or `site.USER_SITE` (for a single user).

This file then is filled with the following code:

import site
site.addsitedir(r'C:\Users\ojdo\Documents\Python\Libs')

- Voilà, the new directory now is automatically added to `sys.path` in every (I)Python session.

How it works: Package site, that is automatically imported during every start of Python, also tries to import the package `sitecustomize` for custom package path modifications. In this case, this dummy package consists of a script that adds the personal package folder to the Python path.

Problem

Is there a canonical location where to put self-written packages? My own search only yielded a blog post about where to put version-independent pure Python packages and a SO question for the canonical location under Linux, while I am working on Windows. My use case is that I would like to be able to import my own packages during a IPython session just like any site-package, no matter in which working directory I started the session. In Matlab, the corresponding folder for example is simply `C:/Users/ojdo/Documents/MATLAB`. ``` import mypackage as mp mp.awesomefunction() ... ```

Original source

Related problems