I have Python wheel file: psutil-5.4.5-cp26-none-linux_x86_64.whl
How can I list the dependencies this wheel has?
I have Python wheel file: psutil-5.4.5-cp26-none-linux_x86_64.whl
How can I list the dependencies this wheel has?
As previously mentioned, .whl
files are just ZIP archives. You can just open them and poke around in the METADATA
file.
There is a tool, however, that can make this manual process a bit easier. You can use pkginfo, which can be installed with pip.
CLI usage:
$ pip install pkginfo
$ pkginfo -f requires_dist psutil-5.4.5-cp27-none-win32.whl
requires_dist: ["enum34; extra == 'enum'"]
API usage:
>>> import pkginfo
>>> wheel_fname = "psutil-5.4.5-cp27-none-win32.whl"
>>> metadata = pkginfo.get_metadata(wheel_fname)
>>> metadata.requires_dist
[u"enum34 ; extra == 'enum'"]
I just tried to unzip (not gunzip) a wheel package I had lying around. The packagename-version.dist-info/METADATA
file contains a list of Requires-Dist:
entries that contain the compiled requirements from setup.py
.
You can install the wheel file in a separate virtual environment and then look which all other packages are installed.
Use pip freeze
command to see all installed packages.
From the directory where you have unzipped your wheel file (change .whl to .zip and unzip) run the following in a shell at the command line:
grep --include=METADATA -rnw '.' -e "Requires-Dist"
Here's a minimal snippet that doesn't require you to have any external tool (unzip, gzip or similars), so it should work in both *nix/windows:
wheeldeps.py:
import argparse
from zipfile import ZipFile
parser = argparse.ArgumentParser()
parser.add_argument('filename')
args = parser.parse_args()
archive = ZipFile(args.filename)
for f in archive.namelist():
if f.endswith("METADATA"):
for l in archive.open(f).read().decode("utf-8").split("\n"):
if 'requires-dist' in l.lower():
print(l)
Example:
> python wheeldeps.py psutil-5.4.5-cp27-cp27m-win_amd64.whl
Requires-Dist: enum34; extra == 'enum'
I use to install my virtual envs with pipenv
that installs pew
as a requirement. pew
lets you install temporary virual environments that are deleted as you exit
those special virtual environments. So...
Make a new empty virtual environment and activate it on the fly:
pew mktmpenv -p /usr/bin/python3.6
Install your package:
pip install somedistro
See what are the requirements of your distro (as well as requirements of requirements...):
pip list
Deactivate and delete the temporary environment.
exit
In addition, temporary virtual environments are very useful at packaging tests.
This is an post I found somewhere. I copied everything and emailed myself so I don't have source for this answer but I think this might help. If anyone know the source, I will delete this post and link it to that post.
Installation
$ pip install --upgrade pip # pip-tools needs pip==6.1 or higher (!)
$ pip install pip-tools
Example usage for pip-compile
Suppose you have a Flask project, and want to pin it for production. Write the following line to a file:
# requirements.in
Flask
Now, run pip-compile requirements.in:
$ pip-compile requirements.in
#
# This file is autogenerated by pip-compile
# Make changes in requirements.in, then run this to update:
#
# pip-compile requirements.in
#
flask==0.10.1
itsdangerous==0.24 # via flask
jinja2==2.7.3 # via flask
markupsafe==0.23 # via jinja2
werkzeug==0.10.4 # via flask
And it will produce your requirements.txt, with all the Flask dependencies (and all underlying dependencies) pinned. Put this file under version control as well and periodically re-run pip-compile to update the packages.
Example usage for pip-sync
Now that you have a requirements.txt, you can use pip-sync to update your virtual env to reflect exactly what's in there. Note: this will install/upgrade/uninstall everything necessary to match the requirements.txt contents.
$ pip-sync
Uninstalling flake8-2.4.1:
Successfully uninstalled flake8-2.4.1
Collecting click==4.1
Downloading click-4.1-py2.py3-none-any.whl (62kB)
100% |████████████████████████████████| 65kB 1.8MB/s
Found existing installation: click 4.0
Uninstalling click-4.0:
Successfully uninstalled click-4.0
Successfully installed click-4.1
The requirement.txt will have all the requirement needed for the package.