Python is my favorite language by far, but here’s one thing it’s absolutely terrible at: package management. Some decent tools (easy_install, pip) exist, but what if you’ve created your own module or package? Ideally, you could start Python from anywhere and simply run:
>>> import mypackage
If you’re running Python from the same directory where the package is stored, this will be fine. But if you run from anywhere outside that directory, Python won’t be able to find the package.
So, here are three options for importing your own Python packages, in order of increasing hack-iness.
Method 1: Distutils
The professional way of creating a distributable package would be to create a setup.py file and install the package from the command-line:
$ python setup.py install
This industry-standard method uses distutils, a package available in every Python version since 2.6. After running the setup, you can import your module from anywhere.
Method 2: Environment Variables
Environment variables contain system information, so why not use them to store where your package is located? From command-line, run:
$ export package=""
is your package’s directory. Thus, the
package environment variable will be the location of your package. Typically, you’d have users add the line above to their
.bashrc, or put it in a setup script that the user has to run in each session before using your package.
Then, to import your module in Python, run:
import os path = os.environ["package"] os.sys.path.append(path) import mypackage
A bit wordy, but it gets the job done!
Method 3: Setuptools Develop
If you really get into the annals of Python, you’ll realize that “installing” a package only means copying it to a directory inside of Python called “site-packages”. This directory contains every package you’ve installed into Python; you can determine its location by running:
$ python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
So, when you run “import package” in Python, this is what it actually does:
- look for the package in fundamental Python libraries (for example, the
- look for a local file/folder in the current directory with the same name as the package
- look in
site-packagesfor a file/folder with the same name as the package
So, assuming you’re on a Unix machine, you could make a symbolic link from the site-packages directory to your package. However, setuptools (a relative of distutils) allows you to do this easily by running the ‘develop’ target:
$ python setup.py develop
Then you can import your package the way you always wanted:
>>> import mypackage
Of these three methods, I find the ‘develop’ method ideal for development environments and distutils great for production. But every situation is different, so use the one that makes the most sense for your environment. Happy coding!