How to install, download and build Python wheels
Python Install Wheel
Open source Python packages can be installed from Source Distributions (sdist) or Wheels (whl). According to the Python Packaging Authority (PyPA), wheels are the preferred way that pip installs Python modules from the Python Package Index (PyPI) because they’re smaller, faster to install, and more efficient than building the package from the source code contained in an sdist. Additionally, most developers simply don’t have the right tooling and experience to build everything from source code themselves. Wheels provide a great shortcut.
Pip will install all “flavors” of wheels, including:
- Universal wheels (any.whl), which support both Python2 and Python3.
- Python wheels that contain just pure Python code. These are typically distributed in a zip-style archive that contains all the files necessary for a typical package installation.
- Platform-specific wheels, which contain C extensions, and therefore must be precompiled as binary distributions for a specific Python version and operating system. Pip will install the appropriate wheel for Windows, Linux or macOS.
How to Pip Install Wheels
There’s no need to specify that you want to install a wheel. By default, pip will always attempt to install a wheel unless there is no whl file for your operating system, at which point pip will attempt to build the wheel from the sdist (note that this can fail if you don’t have the appropriate resources and requirements on your system).
Assuming you don’t first need to install Python from python.org, you can install a wheel by running the following command:
pip install <packagename>
To install a specific version of a package, run the following command:
pip install <packagename>==v.v
pip install django==3.1.13
To install a package from a repository other than PyPI, such as Github:
pip install -e git+<https://github.com/myrepo.git#egg=packagename>
To upgrade a package that is already installed:
pip install --upgrade <packagename>
To uninstall a package:
pip uninstall <packagename>
How to Download Wheels
Most packages provide multiple wheels – one for each version of Python and operating system the author supports. If you’re pre-populating a private repository behind the firewall in your organization, you may want to download multiple wheels since your developers may be working on different operating systems.
To download Python wheels to a specific location (–dest, which is typically a dir), run the following command:
python -m pip download --only-binary :all: --dest . --no-cache <package_name>
Rather than pre-populating a repository with the wheels you want to restrict your developers to using, you can save considerable time and effort by using a pre-populated repository like ActiveState’s Artifact Repository (AAR). AAR can be pre-populated with Windows and/or Linux wheels for Python 3.9 or later.
How to Build Python Wheels
When it comes to Python packaging, if your package consists purely of Python code, you can do the following:
- Make sure Wheel and the latest version of setuptools is installed on your system by running:
python -m pip install -U wheel setuptools
- Then run:
python setup.py sdist bdist_wheel
This will create both a source distribution (sdist) and a wheel file (bdist_wheel) , along with all of its dependencies. You can now upload your built distributions to PyPI. For more information, see Sharing Your Labor of Love: PyPI Quick and Dirty.
If your package has linked C libraries, you’ll need to create specific build environments, and then compile your package separately for each target operating system you want to support.
Wheels have a specific syntax for their filename that consists of multiple parts separated by a hyphen:
Each part provides a clue as to what the wheel contains and where it can be deployed. For example:
“PyYAML-5.3.1-cp27-cp27m-win32.whl” for deployment with Python 2.7 on 32 bit Windows
“PyYAML-5.3.1-cp35-cp35m-win_amd64.whl” for deployment with Python 3.5 on Windows with 64-bit AMD architecture
for deployment with Python 3.7 on multiple Linux versions, such as Ubuntu, Debian, CentOS, Fedora, etc.
Note: “manylinux” is essentially a Docker image that comes bundled with a compiler suite, multiple versions of Python and pip, and an allowed set of shared libraries.
Building Python from source code? Try ActiveState’s Platform
The ActiveState Platform gives you access to a distributed, parallel build system so that even complex development environments can be automatically built and packaged for your OS in record time—straight from source code—giving you all the security assurances and flexibility that comes with it.
Dependency resolution is at the core of the ActiveState Platform. When you create a project and start adding requirements, the Platforms tell you what dependencies those requirements have.
The ActiveState Platform is a cloud-based build tool for Python. It provides build automation and vulnerability remediation for:
- Python language cores, including Python 2.7 and Python 3.5+
- Python packages and their dependencies, including:
- Transitive dependencies (ie., dependencies of dependencies)
- Linked C and Fortran libraries, so you can build data science packages
- Operating system-level dependencies for Windows, Linux, and macOS
- Shared dependencies (ie., OpenSSL)
- Find, fix and automatically rebuild a secure version of Python packages like Django and environments in minutes
The ActiveState Platform aims to handle every dependency for every language. That means handling libraries down to the C/C++ level, external tools, and all the conditional dependencies that exist. To take things even further, our ultimate goal is to support multi-language projects. That means that you can create a project using both Python and Perl packages, and we’ll make sure that both languages are using the same (up to date) OpenSSL version.
Get a hands-on appreciation for how the ActiveState Platform can help you manage your dependencies for Python environments. Just run the following command to install Python 3.9 and our package manager, the State Tool:
powershell -Command "& $([scriptblock]::Create((New-Object Net.WebClient).DownloadString('https://platform.activestate.com/dl/cli/install.ps1'))) -activate-default ActiveState-Labs/Python-3.9Beta"
sh <(curl -q https://platform.activestate.com/dl/cli/install.sh) --activate-default ActiveState-Labs/Python-3.9Beta
Now you can run state install <packagename>. Learn more about how to use the State Tool to manage your Python environment.
Let us know your experience in the ActiveState Community forum.