List dependencies of Python wheel file

Deadly 提交于 2019-11-29 06:43:19

问题


I have Python wheel file: psutil-5.4.5-cp26-none-linux_x86_64.whl

How can I list the dependencies this wheel has?


回答1:


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'"]



回答2:


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.




回答3:


You can install the wheel file in a separate virtual environment and then look which all other packages are installed.




回答4:


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'  



回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/50170588/list-dependencies-of-python-wheel-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!