Installing Python Packages
Now that you have you system and project environment all setup you probably want to start developing. But you likely don’t want to start writing an entire project fully from scratch, as you dive in you’ll quickly realize theres many tools helping you build projects and sites faster. For example making a request to a website there’s Requests, for handling processing images there’s Python Imaging Library, or for a full framework to help you in building a site there’s Django. With all of these there’s one simple and common way to install them. But first a little more on how it all works.
All major Python packages are hosted on PyPi (Pronounced Pi-P or Cheeseshop). When you use a common python installer it will:
- Search for the package you specify
- If you specify a version will use it, otherwise will use the latest
- Will download the source for that package
- Install it into your Python environment
Now for actually installing… Lets get started with installing the three packages below. At this point you should at least have a fresh Python environment, however you don’t have an immediate way to install packages. The defacto Python package installer is pip.
Earlier we setup virtualenv to help isolate our python packages we were working with. First lets go ahead and create a folder for our project then setup a new environment for the project we’ll work on:
1 2 3
If we list the contents of the directory you’ll now see a folder venv. Within this folder you’ll find all the parts of the environment that virtualenv just created:
1 2 3 4
Now you’ve got a sandboxed environment that exists but you haven’t loaded it. You can now activate and deactivate this any time you like. Once you do this it customizes your path to use the packages you’ve installed for this environment. To load your environment when in the myapp directory:
To deactivate this simple:
Now that we’ve got your environment loaded installing your packages should be pretty simple. Ensure that you have your virtualenv loaded and then run:
1 2 3
Now that you’ve installed your packages you want to be able to share this with others to make it easy to get setup. You could provide a list of everything your application needs to run manually, or because its Python you can expect it to make it easy for you. Pip has a wonderful command freeze that will show all of your packages and their versions that are installed. Simply run:
However, this only outputs the information. Along with this pip has a canonical form for listing requirements and installing them from a file. The filename is commonly a requirements.txt. To create this we simply pipe the results of pip freeze to this file.
Next we’ll talk about a few more advanced items in dependency management, then finally we’ll get started on building an application.