有没有办法列出PIP依赖/需求?有没有办法列出PIP依赖/需求?(Is there a way to

2019-06-02 17:02发布

如果没有与安装经历,我想很快看到所有包pip install将安装。

Answer 1:

接受的答案是不再与PIP的更新的版本,并没有仔细阅读多份意见,所以我提供了一个更新的回答没有给出直接的答案。

这与PIP版本8.1.2,9.0.1,10.0.118.1进行测试。

为了让输出,而不在Linux上使用塞满您的当前目录

pip download [package] -d /tmp --no-binary :all:

-d告诉点子是下载应该把目录中的文件。

好,只要使用这个脚本的说法是包名,仅保留的依赖作为输出:

#!/bin/sh

PACKAGE=$1
pip download $PACKAGE -d /tmp --no-binary :all: \
| grep Collecting \
| cut -d' ' -f2 \
| grep -Ev "$PACKAGE(~|=|\!|>|<|$)"

也可在这里 。



Answer 2:

看看我的项目johnnydep !

安装:

pip install johnnydep

用法示例:

$ johnnydep requests
name                       summary
-------------------------  ----------------------------------------------------------------------
requests                   Python HTTP for Humans.
├── certifi>=2017.4.17     Python package for providing Mozilla's CA Bundle.
├── chardet<3.1.0,>=3.0.2  Universal encoding detector for Python 2 and 3
├── idna<2.7,>=2.5         Internationalized Domain Names in Applications (IDNA)
└── urllib3<1.23,>=1.21.1  HTTP library with thread-safe connection pooling, file post, and more.

一个更复杂的树:

$ johnnydep ipython 
name                              summary
--------------------------------  -----------------------------------------------------------------------------
ipython                           IPython: Productive Interactive Computing
├── appnope                       Disable App Nap on OS X 10.9
├── decorator                     Better living through Python with decorators
├── jedi>=0.10                    An autocompletion tool for Python that can be used for text editors.
│   └── parso==0.1.1              A Python Parser
├── pexpect                       Pexpect allows easy control of interactive console applications.
│   └── ptyprocess>=0.5           Run a subprocess in a pseudo terminal
├── pickleshare                   Tiny 'shelve'-like database with concurrency support
├── prompt-toolkit<2.0.0,>=1.0.4  Library for building powerful interactive command lines in Python
│   ├── six>=1.9.0                Python 2 and 3 compatibility utilities
│   └── wcwidth                   Measures number of Terminal column cells of wide-character codes
├── pygments                      Pygments is a syntax highlighting package written in Python.
├── setuptools>=18.5              Easily download, build, install, upgrade, and uninstall Python packages
├── simplegeneric>0.8             Simple generic functions (similar to Python's own len(), pickle.dump(), etc.)
└── traitlets>=4.2                Traitlets Python config system
    ├── decorator                 Better living through Python with decorators
    ├── ipython-genutils          Vestigial utilities from IPython
    └── six                       Python 2 and 3 compatibility utilities


Answer 3:

注意:这个答案使用的功能被弃用在2014年和2015年删除 。 请参阅适用于现代的其他答案pip

您可以直接点子得到最接近的是通过使用--no-install参数:

pip install --no-install <package>

例如,这是在安装芹菜当输出:

Downloading/unpacking celery                                                                                   
  Downloading celery-2.5.5.tar.gz (945Kb): 945Kb downloaded
  Running setup.py egg_info for package celery

    no previously-included directories found matching 'tests/*.pyc'
    no previously-included directories found matching 'docs/*.pyc'
    no previously-included directories found matching 'contrib/*.pyc'
    no previously-included directories found matching 'celery/*.pyc'
    no previously-included directories found matching 'examples/*.pyc'
    no previously-included directories found matching 'bin/*.pyc'
    no previously-included directories found matching 'docs/.build'
    no previously-included directories found matching 'docs/graffles'
    no previously-included directories found matching '.tox/*'
Downloading/unpacking anyjson>=0.3.1 (from celery)
  Downloading anyjson-0.3.3.tar.gz
  Running setup.py egg_info for package anyjson

Downloading/unpacking kombu>=2.1.8,<2.2.0 (from celery)
  Downloading kombu-2.1.8.tar.gz (273Kb): 273Kb downloaded
  Running setup.py egg_info for package kombu

Downloading/unpacking python-dateutil>=1.5,<2.0 (from celery)
  Downloading python-dateutil-1.5.tar.gz (233Kb): 233Kb downloaded
  Running setup.py egg_info for package python-dateutil

Downloading/unpacking amqplib>=1.0 (from kombu>=2.1.8,<2.2.0->celery)
  Downloading amqplib-1.0.2.tgz (58Kb): 58Kb downloaded
  Running setup.py egg_info for package amqplib

Successfully downloaded celery anyjson kombu python-dateutil amqplib

不可否认,这确实留下一些克鲁夫特各地的临时文件的形式,但它确实实现目标。 如果你这样做与的virtualenv(你应该是),清理是去掉了容易<virtualenv root>/build目录。



Answer 4:

当且仅当该包安装,你可以使用pip show <package> 。 认准Requires:在输出的末尾提出。 显然,这伤了你的要求,但可能是有用的不过。

例如:

$ pip --version
pip 7.1.0 [...]
$ pip show pytest
---
Metadata-Version: 2.0
Name: pytest
Version: 2.7.2
Summary: pytest: simple powerful testing with Python
Home-page: http://pytest.org
Author: Holger Krekel, Benjamin Peterson, Ronny Pfannschmidt, Floris Bruynooghe and others
Author-email: holger at merlinux.eu
License: MIT license
Location: /home/usr/.tox/develop/lib/python2.7/site-packages
Requires: py


Answer 5:

命令pip install <package> --download <path>应使用,如通过在@radtek评论提到的,由于作为7.0.0(2015年5月21日)中,--no安装除去从pip 。 这将下载到所需的依赖<path>



Answer 6:

另一种选择是使用类似于一个脚本这一次它使用pip.req.parse_requirements API解析requirements.txt文件和distutils.core.setup更换解析setup.py文件。



Answer 7:

通过@Jmills答案是恒星。 它在导致一些依赖错过的负匹配的错误。 为了确保包没有标记为自身的相关性,包括他行grep -v $PACKAGE ,这也与原来的包名称作为子字符串的任何依赖性负匹配,所以jupyter_core不列为的依赖性jupyter ,例如。

对于我的使用情况下,我认为有必要在Python代码,而不是一个shell脚本的实现。 我没有将原来的错误,但任何人都可以自由地添加它,如果他们想。 我借来的标准输出捕获上下文管理,希望能够使依赖采集更直观。

from cStringIO import StringIO
import sys
import pip

class Capturing(list):
    def __enter__(self):
        self._stdout = sys.stdout
        sys.stdout = self._stringio = StringIO()
        return self
    def __exit__(self, *args):
        self.extend(self._stringio.getvalue().splitlines())
        del self._stringio    # free up some memory
        sys.stdout = self._stdout

def get_dependencies(module_name):
    with Capturing() as out:
        pip.main(['download', module_name, '-d', '/tmp', '--no-binary', ':all:'])
    return [line.split(' ')[1] for line in out if 'Collecting' == line[:10]][1:]

如果你不需要的版本号,这些都是很容易过滤掉。

import re

def module_name(module_name_with_version):
    return re.match('[^!<>=]*', module_name_with_version).group()


Answer 8:

如果你已经安装的软件包,该脚本可以通过运行以下命令获取所有从需求文件的依赖pip show由@Sardathrion提及。

import commands

fil = open("requirements.txt")
for package_line in fil.readlines():
    if "==" in package_line:
        package = package_line.split("==")[0]
    elif "[" in package_line:
        package = package_line.split("[")[0]
    else:
        package = package_line

    output = commands.getoutput('pip show %s' % package)
    try:
        required = output.split("\n")[-1].split(":")[1]
    except Exception as e:
        required = ""
        print "error {} in package {}".format(e, package)

    if len(required) > 1:
        print package, "-- ****%s***" % required


文章来源: Is there a way to list pip dependencies/requirements?
标签: python pip