Where shall I put my self-written Python packages?
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. inC:\Python27\Lib\site-packages
(for all users) orsite.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.
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.
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()"
I'd use the home scheme for this:
http://docs.python.org/2/install/#alternate-installation-the-home-scheme