Basics: Installing Distributions¶
The central online directory of distributions is called the “Python Package Index” (aka, the PyPI) located at http://pypi.python.org/pypi. Almost everyone who creates Python packages and modules who wants them used by the Python community gets them listed at the PyPI. Search there first for whatever you need.
You can install any of the distributions listed at the PyPI by running a simple command (“pip”—described below) which downloads and installs the distribution along with any other distributions that the one you want depends upon.
For this system to work its best, we recommend you first set up a
separate environment for your installed modules by using Virtual Environments.
This only takes a moment, and works well because it keeps you from having to
install various and sundry modules all over your filesystem—instead they
go into a nice roped-off area where they can make all the noise
they want. :) First install the tool that lets you create that
environment:
- download the most-recent release of
virtualenv
(perhaps to your own
~/tempdirectory) - unpack
cd virtualenvpython setup.py install
Now create an environment named, say, “test-env”:
mkdir ~/opt/py-envs
cd ~/opt/py-envs
virtualenv --distribute test-env
If you like, you can now bring up the Python prompt for your new
environment by running ~/opt/py-envs/test-env/bin/python. Exit it
when you’re ready to continue.
You are now ready to install any distributions you like into the isolated Python environment you just created. The command syntax goes like so:
~/opt/py-envs/test-env/bin/pip install DistName
The distribution name is shown in the main heading of its corresponding PyPI page and should be written in CamelCase.
To make use of your newly-installed distributions, just make sure your script refers to the environment where the distributions are installed:
#!/home/<you>/opt/py-envs/test-env/bin/python
import <newly_installed_package>
print "Hello World!"
If you want your shell to always find your own Python environment’s
python first, you might add its bin to your PATH. [1]
For more information on using pip, see the pip chapter of this Guide.
Footnotes
| [1] | On Ubuntu GNU/Linux, you can do this by adding the following
to your ~/.profile: export PATH=/home/<you>/opt/py-envs/test-env/bin:$PATH |