15

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()
...
Community
  • 1
  • 1
ojdo
  • 8,280
  • 5
  • 37
  • 60
  • 1
    I prefer to stick files in `C:\PythonXX\Lib\site-packages`. This has the advantage of requiring no additional configuration - that directory is in `sys.path` by default. – bbayles Jul 23 '13 at 09:51
  • 4
    Did you look at [the documentation](http://docs.python.org/2/install/#how-installation-works)? Also, you can use the PYTHONPATH environment variable, as explained [in another section of the docs](http://docs.python.org/2/install/#modifying-python-s-search-path). – Paulo Almeida Jul 23 '13 at 09:52
  • 2
    I’m voting to close this question because the example provided is too specific for being useful to others. – Casper Round Dec 16 '20 at 14:17
  • 3
    @CasperRound can you elaborate, please? I was asking for a Python-specific, but general convention about package placement for "machine-local" packages one does not want to publish/release via PyPI. Given that there is a standard library answer to this quesiton (`site.USER_SITE`), which I did not know at the time, suggests to me that I am/was not the only one who needed that spelled out explicitely. Or is the mention of MATLAB (my langauge of "origin" at the time which fueled my expectation) the reason for the "too specific" vote? – ojdo Dec 16 '20 at 15:43

4 Answers4

14

Place the source of your package wherever you'd like, but at least give your package a minimal setup.py file, immediately outside the package:

import setuptools

setuptools.setup(name='mypackage')

Then fake-install your package into your python install's site-packages by running:

python setup.py develop

This is a lot like running python setup.py install, except the egg just points to your source tree, so you don't have to install after every source code change.

Finally, you should be able to import your package:

python -c "import mypackage as mp; print mp.awesomefunction()"
Dolph
  • 49,714
  • 13
  • 63
  • 88
12

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

  1. Create a directory anywhere, e.g. C:\Users\ojdo\Documents\Python\Libs.
  2. 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).
  3. This file then is filled with the following code:

    import site
    site.addsitedir(r'C:\Users\ojdo\Documents\Python\Libs')
    
  4. 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.

ojdo
  • 8,280
  • 5
  • 37
  • 60
  • Will do so once it is possible. I receive a *"You can accept your own answer tomorrow"* right now. – ojdo Jul 24 '13 at 07:55
  • Note - the python docs have an error - they have an incorrect format. It says the packages should be in "modules: userbase/lib/pythonX.Y/site-packages" but the directory (for 3.5) from site.USER_SITE is '~/Library/Python/3.5/lib/python/site-packages' - note the X.Y is not there. My site.USER_BASE is '~/Library/Python/3.5' – ski_squaw Apr 22 '16 at 18:17
  • So, basically you mean to say when one writes their own package, the good way it to move the package to PythonXX\Lib\site-packages ? – Travis_Dudeson May 12 '23 at 07:47
3

I'd use the home scheme for this:

http://docs.python.org/2/install/#alternate-installation-the-home-scheme

djc
  • 11,603
  • 5
  • 41
  • 54
  • Thanks for the pointer to the documentation. It made me read it more thoroughly and find the `site.USER_BASE` property. I wonder why it is not mentioned more "prominently". – ojdo Jul 23 '13 at 13:19
2

I had the same question, and your answer is very helpful. To add a little bit, I came across this example that's helpful to me:

http://python-packaging.readthedocs.io/en/latest/minimal.html

It is a minimal example of how to package your own code, and properly install it locally (I imagine this is what you actually want), or distribute on PyPI. Doing things the python way.

Alex
  • 912
  • 2
  • 7
  • 19