TL;DR Don't user sudo python setup.py intall use pip install -e ./ --user
I regularly Go through the following process:
- Python packages are confusing
- I should read the docs
pipis cool- I have no idea what I am doing
But this time I actually learned something useful.
Background 1: easy_install and pip
If you have spent any reasonable amount of time you have probably learned about easy_install and pip. You have probably also generally learned that easy_install is not good and should be replaced with pip in most cases.
Background 2: Working with Package Source
So pip install works fabulously with packages in PyPI, but that does not help you if you work with actual package source. If you deal with the source for python package source there are three common ways to deal with developing and testing on the package source itself.
- Work in the same directory as the package source
- Modify
sys.pathto add your working directory - Install the package using
sudo python ./setup.py install
One and Two are just fragile, crazy, and if you have ever tried it you know somewhere in the back of your head this is bad. Three is theoretically the right way to install and test python Packages, but it has a number of downsides.
- Requires
sudo - Leaves a
builddirectory owned byroot - Needs to be re-run every time the source changes
- Can't easily uninstall packages installed this way
Useful Thing 1: Using pip for local source installation
sudo pip install ./ can be used instead of setup.py. The solves the second and fourth problem on our list. No more build directory is left behind after the installation and sudo pip uninstall <Package> will uninstall the package in one command
Useful Thing 2: Installing in non-System paths
Adding a --user flag to pip install puts the installed package at ~/Library instead of /Library. Because ~/Library is owned by the user and not root there is no need for sudo.
Useful Thing 3: Installing Package symlinks*
By adding the -e flag to pip a symlink to the source source is installed instead of the byte-code compiled source. It is not really a symlink, but it is a close enough analogy to understand what is happening.
Summary
Developing and Testing Packages locally can be greatly improved by using pip install -e ./ --user. This keeps the "installed" package up to date with the source and can be easy uninstalled with pip uninstall <Package>