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()
...
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.
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.
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.
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.
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.
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.
C:\Users\ojdo\Documents\Python\Libs
.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')
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With