简体   繁体   English

使用下载缓存将PIP包安装到Virtualenv

[英]Installing PIP packages to a Virtualenv using a download cache

What's the proper way to install pip packages to a virtualenv using cached packages? 使用缓存包将pip包安装到virtualenv的正确方法是什么? I've tried setting --timeout=360 and --use-mirrors , but pypi performance is so flakey, it routinely hangs or disconnects, making deployments a crapshoot. 我已经尝试过设置--timeout=360--use-mirrors ,但pypi性能非常好,它经常挂起或断开连接,使部署成为一个废话。

So, my plan was to use the --download-cache option to pre-download all package archives (but not install them), eg: 所以,我的计划是使用--download-cache选项预先下载所有包档案(但不安装它们),例如:

pip install --upgrade --timeout=120 --use-mirrors --no-install --download-cache /usr/local/pip/cache -r pip-requirements.txt

and then specify this cache location during the install into the virtualenv, eg: 然后在安装到virtualenv期间指定此缓存位置,例如:

virtualenv /usr/local/myapp/.env
. /usr/local/myapp/.env/bin/activate; pip install --no-download --download-cache /usr/local/pip/cache -r pip-requirements.txt
deactivate

Creating the cache was very frustrating, because pip seems to nondeterministically save the downloaded package to the cache dir. 创建缓存非常令人沮丧,因为pip似乎不确定地将下载的包保存到缓存目录。 Specifically, it refused to cache Django==1.4.0 , saying "that requirement has already been met", so I had to use the --force-reinstall . 具体来说,它拒绝缓存Django==1.4.0 ,说“已满足要求”,所以我不得不使用--force-reinstall

Installing using the cache is currently where I'm stuck. 目前使用缓存进行安装是我遇到的问题。 Running the above command gives me the error: 运行上面的命令给我错误:

Could not install requirement Django==1.4.0 (from -r pip-requirements.txt (line 1)) because source folder /usr/local/myapp/.env/build/Django does not exist (perhaps --no-download was used without first running an equivalent install with --no-install?)
Storing complete log in /home/chris/.pip/pip.log

What does this mean? 这是什么意思?

The files: 文件:

http%3A%2F%2Fpypi.python.org%2Fpackages%2Fsource%2FD%2FDjango%2FDjango-1.4.tar.gz
http%3A%2F%2Fpypi.python.org%2Fpackages%2Fsource%2FD%2FDjango%2FDjango-1.4.tar.gz.content-type

definitely exist in the cache directory, and I used the --no-install option. 肯定存在于缓存目录中,我使用了--no-install选项。

What am I doing wrong? 我究竟做错了什么?

The problem seems to be that --download-cache can only be used to specify the cache for downloading, not for installing. 问题似乎是--download-cache只能用于指定要下载的缓存,而不能用于安装。 Therefore pip is still looking at /usr/local/myapp/.env/build/Django instead of /usr/local/pip/cache . 因此pip仍在查看/usr/local/myapp/.env/build/Django而不是/usr/local/pip/cache Have you tried moving 你有没有尝试过移动

pip install --upgrade --timeout=120 --use-mirrors --no-install --download-cache /usr/local/pip/cache -r pip-requirements.txt

to after the creation of virtualenv ? virtualenv创建之后? I wonder if that would help. 我想知道这是否会有所帮助。

You may also want to try to export PIP_DOWNLOAD_CACHE and see if it works without using --download-cache . 您可能还想尝试导出PIP_DOWNLOAD_CACHE并查看它是否可以在不使用--download-cache

Here is what I find that works: 以下是我发现的有效方法:

pip install --no-install --use-mirrors --download=/DIRNAME PKGNAME
pip install --find-links=file:///DIRNAME --no-index --index-url=file:///dev/null PKGNAME

Now, actually , here is the tool for I would use instead of all the above and it solves all of the problems much more elegantly and reliably: pip2pi by David Wolever. 现在,实际上 ,这里是我会改用上述所有的工具,它解决了所有的问题,更优雅和可靠: pip2pi大卫Wolever。

From the docs: 来自文档:

pip2pi builds a PyPI-compatible package repository from pip requirements pip2pi根据pip要求构建与PyPI兼容的包存储库

pip2pi allows you to create your own PyPI index by using two simple commands: pip2pi允许您使用两个简单的命令创建自己的PyPI索引:

  1. To mirror a package and all of its requirements, use pip2tgz : 要镜像包及其所有要求,请使用pip2tgz

     $ cd /tmp/; mkdir package/ $ pip2tgz packages/ httpie==0.2 ... $ ls packages/ Pygments-1.5.tar.gz httpie-0.2.0.tar.gz requests-0.14.0.tar.gz 
  2. To build a package index from the previous directory: 要从以前的目录构建包索引:

     $ ls packages/ bar-0.8.tar.gz baz-0.3.tar.gz foo-1.2.tar.gz $ dir2pi packages/ $ find packages/ /httpie-0.2.0.tar.gz /Pygments-1.5.tar.gz /requests-0.14.0.tar.gz /simple /simple/httpie /simple/httpie/httpie-0.2.0.tar.gz /simple/Pygments /simple/Pygments/Pygments-1.5.tar.gz /simple/requests /simple/requests/requests-0.14.0.tar.gz 
  3. To install from the index you built in step 2., you can simply use: 要从您在步骤2中构建的索引进行安装,您只需使用:

     pip install --index-url=file:///tmp/packages/simple/ httpie==0.2 

Bonus: you can even mirror your own index to a remote host with pip2pi . 额外奖励:您甚至可以使用pip2pi将自己的索引镜像到远程主机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM