Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where shall I put my self-written Python packages?

Tags:

python

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()
...
like image 659
ojdo Avatar asked Jul 23 '13 09:07

ojdo


People also ask

Where should my Python packages be installed?

When a package is installed globally, it's made available to all users that log into the system. Typically, that means Python and all packages will get installed to a directory under /usr/local/bin/ for a Unix-based system, or \Program Files\ for Windows.

Where can I publish Python packages?

PyPi is the official third-party software repository for python packages. Whenever you run the pip install command, the pip tool searches the package in this repository and then downloads and installs it to your machine or virtual environment.

Where should I install pip packages?

By default, packages are installed to the running Python installation's site-packages directory. site-packages is by default part of the python search path and is the target directory of manually built python packages. Modules installed here can be imported easily afterwards.

How do I set up Python packages?

To install a package that includes a setup.py file, open a command or terminal window and: cd into the root directory where setup.py is located. Enter: python setup.py install.


1 Answers

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.

like image 168
ojdo Avatar answered Oct 21 '22 01:10

ojdo