如果不进行安装,我想快速查看pip install
将安装的所有软件包。
已接受的答案不再适用于更新的pip版本,并且在不仔细阅读多条评论的情况下不会给出即时答案,因此我提供了更新的答案。
这是使用pip版本8.1.2,9.0.1,10.0.1和18.1进行测试的。
要在Linux上使用,而不会使当前目录混乱
pip download [package] -d /tmp --no-binary :all:
-d
告诉pip下载应放入文件的目录。
更好的是,只需使用此脚本作为包名称参数,仅将依赖项作为输出:
#!/bin/sh
PACKAGE=$1
pip download $PACKAGE -d /tmp --no-binary :all: \
| grep Collecting \
| cut -d' ' -f2 \
| grep -Ev "$PACKAGE(~|=|\!|>|<|$)"
也可用here。
看看我的项目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
注意:此答案中使用的功能是deprecated in 2014和removed in 2015。请参阅适用于现代
pip
的其他答案。
你可以直接用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
目录一样简单。
当且仅当包安装时,您可以使用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
应该使用命令pip install <package> --download <path>
,如@radtek的评论中所述,自7.0.0(2015-05-21)起, - no-install是来自removed的pip
。这将下载<path>
所需的依赖项。
@Jmills的答案很棒。它在负匹配中有一个错误,导致错过某些依赖项。为了确保包没有被标记为自身的依赖,他包括行grep -v $PACKAGE
,它也与原始包名称作为子字符串的任何依赖项负面匹配,因此jupyter_core
未被列为jupyter
的依赖项,例如。
对于我的用例,我发现在python代码而不是shell脚本中实现它是有用的。我没有包含原始的bug,但是如果他们愿意,任何人都可以自由添加它。我借用了一个stdout捕获上下文管理器,希望能够使依赖性收集更加直观。
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()
另一个选择是使用类似于this one的帮助程序脚本,它使用pip.req.parse_requirements
API来解析requirements.txt
文件和distutils.core.setup
替换来解析setup.py
文件。
如果您已经安装了软件包,则此脚本可以通过运行@Sardathrion提到的命令pip show
从需求文件中获取所有依赖项。
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