防止pip安装某些依赖项 [英] Prevent pip from installing some dependencies
问题描述
我们正在为Python中的Alexa技能开发 AWS Lambda 函数,并使用pip
将ask-sdk
软件包安装到我们的dist/
目录中:
We're developing an AWS Lambda function for Alexa skill in Python and using pip
to install the ask-sdk
package to our dist/
directory:
pip install -t dist/ ask-sdk
-t dist/
的麻烦在于,因为pip
希望在其中具有所有依赖项,即使它们在系统范围内安装也是如此.
The trouble is with the -t dist/
because pip
wants to have all the dependencies there, even if they are installed system-wide.
现在,ask-sdk
对boto3
有依赖性,该依赖性引入了许多其他软件包.但是, AWS Lambda 运行时环境提供了boto3
,并且无需将其及其依赖项与我们的代码打包在一起.我确实在系统中安装了boto3
并且import boto3
可以工作,所以我认为pip
应该很高兴,但是由于-t dist/
,它总是会安装它.
Now, ask-sdk
has a dependency on boto3
which pulls in a whole lot of other packages. However the AWS Lambda runtime environment provides boto3
and there is no need to package that and its dependencies with our code. I do have boto3
installed in the system and import boto3
works, so I thought pip
should be happy, but because of -t dist/
it always installs it.
我可以以某种方式仅安装ask-sdk
及其系统中不存在的依赖项吗,例如ask-sdk-core
,但不是已经安装的那些?
Can I somehow install just ask-sdk
and its dependencies that don't exist in the system, e.g. ask-sdk-core
, but not those that are already installed?
一种方法是列出所有模块并使用pip --no-deps
,但这意味着要不断手动跟踪依赖项,我们希望避免这种情况.
One way is to list all the modules and use pip --no-deps
but that means a constantly keeping track of the dependencies manually, we would like to avoid that.
我想以某种方式告诉pip
:如果该软件包已经安装,即使没有安装在-t dist/
中,也请不要将副本放在dist/
中.
Somehow I would like to tell pip
: if the package is already installed, even if not in -t dist/
don't put a copy in dist/
.
有可能吗?
谢谢!
推荐答案
尽管您不能告诉pip安装除boto3所需的所有依赖项之外的所有依赖项",但是您可以通过计算<pip freeze
输出中的c6>和ask-sdk
(使用Python 3.6.6测试):
Although you can't tell pip to "install all dependencies except those required by boto3", you can generate the needed requirements.txt
by computing the difference between boto3
and ask-sdk
from pip freeze
output (tested with Python 3.6.6):
# get boto3 requirements
pip install boto3 -t py_lib.boto3
PYTHONPATH=py_lib.boto3 pip freeze > requirements-boto3.txt
# get ask-sdk requirements
pip install ask-sdk -t py_lib.ask-sdk
PYTHONPATH=py_lib.ask-sdk pip freeze > requirements-ask-sdk.txt
# compute their difference
grep -v -x -f requirements-boto3.txt requirements-ask-sdk.txt > requirements-final.txt
# patch to add one missing dep from boto3
# aws don't have this for some reason
grep urllib3 requirements-boto3.txt >> requirements-final.txt
requirements-final.txt
包含以下内容:
ask-sdk==1.5.0
ask-sdk-core==1.5.0
ask-sdk-dynamodb-persistence-adapter==1.5.0
ask-sdk-model==1.6.2
ask-sdk-runtime==1.5.0
certifi==2018.11.29
chardet==3.0.4
idna==2.8
requests==2.21.0
urllib3==1.24.1
要将最后一组依赖项安装到文件夹:
To install the final set of dependencies to a folder:
pip install --no-deps -r requirements-final.txt -t py_lib
通过跳过boto3
依赖项,您可以从python依赖项中保存大约45M的数据. ask-sdk
依赖项仅约为7.5M(压缩后为2.1M),如果需要,您可以使用内置的lambda代码编辑器.
By skipping the boto3
dependencies, you can save about 45M of data from your python dependencies. The ask-sdk
dependencies are only about 7.5M (2.1M compressed), allow you to use the build-in lambda code editor if you need to.
这篇关于防止pip安装某些依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!