如何使用PIP编程(从我的代码)安装PIP模块?
我需要从
是否有某些模块或 distutils
( distribute
, pip
等)功能,使我可以执行诸如 pypi之类的东西。 install('请求')
和请求将安装到我的Virtualenv中?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
对于安装多个软件包,我使用
setup.py.py
文件,其中包括以下代码:For installing multiple packages, I am using a
setup.py
file with the following code:使用:
我尝试了以上用于临时解决方案的方法,而不是更改 docker file docker file 。
Use:
I tried the above for a temporary solution instead of changing a Docker file.
如果您想要一个更有效的答案,以扩展
subprocess.check_call
。您可以首先使用pkg_resources
检查是否已经满足了需求。这适用于不同的需求specifiers 很好。例如,
> =
,==
:示例用法:
相关信息:堆栈溢出问题:58612272
If you want a more efficient answer that expands on
subprocess.check_call
. You can first check if the requirement has already been met usingpkg_resources
.This works for different requirement specifiers which is nice. E.g.,
>=
,==
:Example usage:
Relevant information: Stack Overflow question: 58612272
您可以使用“ install_requires”选项定义您自己软件包的依赖模块。
如果您的软件包需要生成一些控制台脚本,则可以使用“ console_scripts”输入点,以生成包装脚本
在“ bin”文件夹中(例如,虚拟环境的环境)。
You define the dependent module inside the setup.py of your own package with the "install_requires" option.
If your package needs to have some console script generated then you can use the "console_scripts" entry point in order to generate a wrapper script that will be placed
within the 'bin' folder (e.g. of your virtualenv environment).
到有条件地安装带有精确版本的多个软件包,我一直在@tanmay Shrivastava的答案上使用这种模式。
To conditionally install multiple packages with exact version, I've been using this pattern basing on @Tanmay Shrivastava's answer:
我不喜欢所有建议的选项,因此我写了 library 为此。
安装它:
使用上下文管理器:
要了解有关库的更多信息,请按照我上面给出的链接。它允许您在一个程序中使用两个不同版本的同一库,例如,彼此之间不兼容的库。
I didn't like all the suggested options and I wrote my library for this.
Install it:
And use a context manager:
To learn more about the library, follow the link I gave above. It allows you to use two different versions of the same library in one program, or, for example, libraries that are incompatible with each other.
尝试以下。到目前为止,这是对我有用的最好的。
首先安装4个,然后提及“必需” 列表中的新:
Try the below. So far, it was the best that worked for me.
Install the 4 ones first and then mention the new ones in the "REQUIRED" list:
从脚本安装软件包的正式建议方法是通过子过程调用PIP的命令行接口。 此处提供的大多数其他答案不受PIP 。此外,自PIP V10以来,所有代码已移至
pip._internal
,以便向用户清楚地表明,不允许使用PIP的程序使用。使用
sys.executable
确保您将调用与当前运行时关联的相同pip
。The officially recommended way to install packages from a script is by calling pip's command-line interface via a subprocess. Most other answers presented here are not supported by pip. Furthermore since pip v10, all code has been moved to
pip._internal
precisely in order to make it clear to users that programmatic use of pip is not allowed.Use
sys.executable
to ensure that you will call the samepip
associated with the current runtime.您也可以使用类似的东西:
You can also use something like:
如果要使用
pip
安装所需的软件包并在安装后导入它,则可以使用此代码:如果您以用户的身份安装了软件包,则可以遇到无法仅导入软件包的问题。请参阅如何刷新sys.path?以获取更多信息。
If you want to use
pip
to install required package and import it after installation, you can use this code:If you installed a package as a user you can encounter the problem that you cannot just import the package. See How to refresh sys.path? for additional information.
这应该有效:
This should work:
我在。
I added some exception handling to Aaron's answer.