Where can I put my proprietary Python packages?

Is there a canonical place to put self-made packages? My own search only led to a blog post about where to put version-independent pure Python packages and SO the question of the canonical location under Linux while I was working on Windows.

In my case, I would like to be able to import my own packages during an IPython session, like any site package, no matter in which working directory I started the session. In Matlab, for example, the corresponding folder is simply C:/Users/ojdo/Documents/MATLAB .

 import mypackage as mp mp.awesomefunction() ... 
+10
python
source share
4 answers

Thanks to two additional links, I found not only the intended answer to my question, but also a solution that I like even more, and which, oddly enough, was also explained in my first search result, but was confused by all version- (non) dependent languages โ€‹โ€‹of site packages.

Answer to the original question: default folder

I wanted to know if there was a canonical (as in the "default") location for my generic packages. And it exists:

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

And for an example with Linux and Python 3:

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

The user schema package installation documentation states that the USER_SITE folder - if it exists - will be automatically added to your Python sys.path when the interpreter starts, no manual actions are required.


Bonus: custom catalog for your own packages

  1. Create a directory anywhere, for example C:\Users\ojdo\Documents\Python\Libs .
  2. Add the sitecustomize.py file 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 one user).
  3. Then this file is populated with the following code:

     import site site.addsitedir(r'C:\Users\ojdo\Documents\Python\Libs') 
  4. Voila, the new directory is now automatically added to sys.path with every (I) Python session.

How it works: The site package, which is automatically imported every time Python starts up, also tries to import the sitecustomize package for custom changes to the package path. In this case, this dummy package consists of a script that adds the personal package folder to the Python path.

+8
source share

Place the source of your package wherever you want, but at least provide your package with a minimal setup.py file, directly outside the package:

 import setuptools setuptools.setup(name='mypackage') 

Then fake your package into your python site-packages installation by doing:

 python setup.py develop 

This is very similar to running python setup.py install , except that egg just points to the source tree, so you don't need to install after changing each source code.

Finally, you should be able to import your package:

 python -c "import mypackage as mp; print mp.awesomefunction()" 
+11
source share
+3
source share

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

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

This is a minimal example of how to pack your own code and correctly install it locally (I think this is what you really want), or distribute it to PyPI. Doing things in python way.

+2
source share

All Articles