我正在Ubuntu 12.04中使用以下requirements.txt文件安装几个Python包:

numpy>=1.8.2,<2.0.0
matplotlib>=1.3.1,<2.0.0
scipy>=0.14.0,<1.0.0
astroML>=0.2,<1.0
scikit-learn>=0.14.1,<1.0.0
rpy2>=2.4.3,<3.0.0

下面是两条命令:

$ pip install --download=/tmp -r requirements.txt
$ pip install --user --no-index --find-links=/tmp -r requirements.txt

(第一个程序下载包,第二个程序安装包)。

进程经常被错误停止:

  Could not find a version that satisfies the requirement <package> (from matplotlib<2.0.0,>=1.3.1->-r requirements.txt (line 2)) (from versions: )
No matching distribution found for <package> (from matplotlib<2.0.0,>=1.3.1->-r requirements.txt (line 2))

我手动修复:

pip install --user <package>

然后再次运行第二个PIP安装命令。

但这只适用于特定的包装。当我再次运行第二个pip install命令时,进程停止了,现在抱怨有另一个所需的包,我需要再次重复该过程,即:手动安装新的所需包(使用上面的命令),然后运行第二个pip install命令。

到目前为止,我不得不手动安装6,pytz, nose,现在它抱怨需要mock。

有没有办法告诉pip自动安装所有需要的依赖项,这样我就不必手动一个一个地安装了?

补充:这只发生在Ubuntu 12.04 BTW中。在Ubuntu 14.04中,应用在requirements.txt文件上的pip安装命令可以正常工作。


当前回答

这种方法(将所有依赖项放在一个目录中,而不是从索引中下载)只在目录包含所有包时有效。因此,目录应该包含所有依赖项,以及这些依赖项所依赖的所有包(例如,six, pytz等)。

因此,您应该手动将这些包包含在requirements.txt中(这样第一步就可以显式地下载它们),或者您应该使用PyPI安装所有包,然后pip freeze > requirements.txt来存储所需的所有包的列表。

其他回答

尽管它并没有真正回答这个具体的问题。其他人也收到了同样的错误信息。

对于那些像我一样最初忘记了-r的人:使用pip install -r requirements.txt, -r对于命令是必不可少的。

原来的答案是:

https://stackoverflow.com/a/42876654/10093070

Pip从pypi.org安装。

pip install -U -i  https://pypi.org/simple package

尝试使用以下命令通过powershell安装flask。

pip install --isolated Flask

这将允许安装避免环境变量和用户配置。

当我失去网络连接时,我犯了这个错误。

只是提醒谁谷歌这个错误,来这里。

假设我得到这个错误:

$ python3 example.py
Traceback (most recent call last):
  File "example.py", line 7, in <module>
    import aalib
ModuleNotFoundError: No module named 'aalib'

既然它提到了aalib,我想试试aalib:

$ python3.8 -m pip install aalib
ERROR: Could not find a version that satisfies the requirement aalib (from versions: none)
ERROR: No matching distribution found for aalib

但它实际上是错误的包名,确保pip搜索(在撰写时服务已禁用),或谷歌,或在pypi网站上搜索以获得准确的包名:

安装成功:

$ python3.8 -m pip install python-aalib
Collecting python-aalib
  Downloading python-aalib-0.3.2.tar.gz (14 kB)
...

正如pip——help所说:

$ python3.8 -m pip --help
...
  -v, --verbose               Give more output. Option is additive, and can be used up to 3 times.

为了有一个系统的方法来找出根本原因,而不是依靠运气,你可以在pip命令中附加-vvv选项来查看详细信息,例如:

$ python3.8 -u -m pip install aalib -vvv
User install by explicit request
Created temporary directory: /tmp/pip-ephem-wheel-cache-b3ghm9eb
Created temporary directory: /tmp/pip-req-tracker-ygwnj94r
Initialized build tracking at /tmp/pip-req-tracker-ygwnj94r
Created build tracker: /tmp/pip-req-tracker-ygwnj94r
Entered build tracker: /tmp/pip-req-tracker-ygwnj94r
Created temporary directory: /tmp/pip-install-jfurrdbb
1 location(s) to search for versions of aalib:
* https://pypi.org/simple/aalib/
Fetching project page and analyzing links: https://pypi.org/simple/aalib/
Getting page https://pypi.org/simple/aalib/
Found index url https://pypi.org/simple
Getting credentials from keyring for https://pypi.org/simple
Getting credentials from keyring for pypi.org
Looking up "https://pypi.org/simple/aalib/" in the cache
Request header has "max_age" as 0, cache bypassed
Starting new HTTPS connection (1): pypi.org:443
https://pypi.org:443 "GET /simple/aalib/ HTTP/1.1" 404 13
[hole] Status code 404 not in (200, 203, 300, 301)
Could not fetch URL https://pypi.org/simple/aalib/: 404 Client Error: Not Found for url: https://pypi.org/simple/aalib/ - skipping
Given no hashes to check 0 links for project 'aalib': discarding no candidates
ERROR: Could not find a version that satisfies the requirement aalib (from versions: none)
Cleaning up...
Removed build tracker: '/tmp/pip-req-tracker-ygwnj94r'
ERROR: No matching distribution found for aalib
Exception information:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pip/_internal/cli/base_command.py", line 186, in _main
    status = self.run(options, args)
  File "/usr/lib/python3/dist-packages/pip/_internal/commands/install.py", line 357, in run
    resolver.resolve(requirement_set)
  File "/usr/lib/python3/dist-packages/pip/_internal/legacy_resolve.py", line 177, in resolve
    discovered_reqs.extend(self._resolve_one(requirement_set, req))
  File "/usr/lib/python3/dist-packages/pip/_internal/legacy_resolve.py", line 333, in _resolve_one
    abstract_dist = self._get_abstract_dist_for(req_to_install)
  File "/usr/lib/python3/dist-packages/pip/_internal/legacy_resolve.py", line 281, in _get_abstract_dist_for
    req.populate_link(self.finder, upgrade_allowed, require_hashes)
  File "/usr/lib/python3/dist-packages/pip/_internal/req/req_install.py", line 249, in populate_link
    self.link = finder.find_requirement(self, upgrade)
  File "/usr/lib/python3/dist-packages/pip/_internal/index/package_finder.py", line 926, in find_requirement
    raise DistributionNotFound(
pip._internal.exceptions.DistributionNotFound: No matching distribution found for aalib

从上面的日志中,很明显没有找到URL https://pypi.org/simple/aalib/ 404。然后您可以猜测导致404的可能原因,即错误的包名。另外,我可以修改pip模块的相关python文件,用上面的日志进一步调试。要编辑.whl文件,可以使用wheel命令解压和打包。