How To Install Python Libraries Using Wheels

Most of the time, for your Python project, you need libraries which call in your code, after the installation of the required add-on packages. You install add-on packages nowadays by just running pip install <name_of_package> and it works fine. In most cases, this is because, among other things, the package has releases published (by its author) on the Python Package Index (the PyPI).

But there are situations where you are forced to do your own builds of some packages for deployment on the Python environment of your project.

From the source code to a Wheel

In those cases, you need the standard setup script (setup.py). This is a script added to the source code of a package (at the root of its directory structure), to declare how its setup will be done. Very important since the package's author wants others to use his library, ideally without any hassle.

Now, for our particular need, what we want is to generate a distribution of a given package so that it can be installed on the target environment locally (or remotely, if it is then uploaded on a package index such as PyPI, but that is out of scope for this article), using the pip command.

Without going in too much detail, the modern suitable Python package distribution format that you use for such cases is called "Wheel". The distribution is encapsulated in a special archive file with the .whl extension.

Once the package's wheel exists, we can install it...

  • locally, using a command such as pip install /path/to/dir/mypackage.whl,
  • or, remotely, using pip install mypackage.whl (the "wheel" has been uploaded to a package repository where pip can find it).

What is interesting now is to see how you create the "wheel".

You need the wheel package

You need to install a special package that brings the possibility of building wheels, called, you might have guessed: wheel.

So you would add that package to your Python environment:

pip install wheel

Build your wheels using the bdist_wheel option

Once that package is installed, we can create our own wheel for a given package called mypackage, containing its setup.py file as required, by doing (in the simple case):

cd mypackage
python setup.py bdist_wheel

If all goes well, we would get a folder called dist/ created in the package folder, and inside it, the mypackage-1.0.0-py2-none-any.whl archive file created, if you are using Python 2.7 and if the version of our package is 1.0.0. You would get a similar result on Python 3.6.

You can understand more about these setup / installation tools by visiting:

comments powered by Disqus

Need help for your project?

Our team can contribute to your project, working on a specific task, or doing all the coding based on your specifications, using Python, a web framework such as Django or a CMS.