在 python 脚本中使用“pip install/uninstall”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12937533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:13:26  来源:igfitidea点击:

use "pip install/uninstall" inside a python script

pythonpip

提问by BernardoBarreto

how, inside a python script can I install packages using pip? I don't use the os.system, I want to import pip and use it.

如何在 python 脚本中使用 pip 安装软件包?我不使用 os.system,我想导入 pip 并使用它。

采纳答案by tehmisvh

It's not a good idea to install packages inside the python script because it requires root rights. You should ship additional modules alongside with the script you created or check if the module is installed:

在 python 脚本中安装包不是一个好主意,因为它需要 root 权限。您应该将附加模块与您创建的脚本一起提供,或者检查是否安装了该模块:

try:
   import ModuleName
except ImportError:
   print 'Error, Module ModuleName is required'

If you insist in installing the package using pip inside your script you'll have to look into callfrom the subprocessmodule ("os.system()" is deprecated).

如果您坚持在脚本中使用 pip 安装软件包,则必须callsubprocess模块中查看(os.system()不推荐使用“ ”)。

There is no pip module but you could easily create one using the method above.

没有 pip 模块,但您可以使用上述方法轻松创建一个。

回答by Ioannis Filippidis

This is a comment to this postthat didn't fit in the space allotted to comments.

这是对该帖子的评论,不适合分配给评论的空间。

Note that the use case of installing a package can arise inside setup.pyitself. For example, generating plyparser tables and storing them to disk. These tables must be generated beforesetuptools.setupruns, because they have to be copied to site_packages, together with the package that is being installed.

请注意,安装包的用例可能会出现在setup.py其内部。例如,生成ply解析器表并将它们存储到磁盘。这些表必须setuptools.setup运行之前生成,因为它们必须site_packages与正在安装的包一起复制到。

There does exist the setup_requiresoptionof setuptools.setup, however that does notinstall the packages.

确实存在的setup_requires选项setuptools.setup,但是这并没有安装的软件包。

So a dependency that is required both for the installation process andfor the installed package will not be installed this way.

因此,不会以这种方式安装安装过程已安装包所需的依赖项。

Placing such a dependency inside install_requiresdoes notalways work as expected. Even if it worked, one would have to pass some function to setuptools.setup, to be run between installation of dependencies in setup_requiresand installation of the package itself. This approach is nested, and thus against PEP 20.

放置这样的依赖内部install_requires如预期总是工作。即使它有效,也必须将一些函数传递给setuptools.setup, 以在安装依赖项setup_requires和安装包本身之间运行。这种方法是嵌套的,因此反对PEP 20

So the two flat approaches that remain, are:

所以剩下的两种扁平化方法是:

  1. run setup.pytwice, either automatically (preferred), or manually (by notifying the user that the tables failed to build prior to setuptools.setup.

  2. first call pip(or some other equivalent solution), in order to install the required dependencies. Then proceed with building the tables (or whatever pre-installation task is necessary), and call setuptools.setuplast.

  1. 运行setup.py两次,自动(首选)或手动(通过通知用户在setuptools.setup.

  2. 首先调用pip(或其他一些等效的解决方案),以安装所需的依赖项。然后继续构建表(或任何必要的预安装任务),setuptools.setup最后调用。

Personally, I prefer No.2, because No.1 can be confusing to a user observing the console output during installation, unless they already know the intent of calling setuptools.setuptwice.

就我个人而言,我更喜欢 No.2,因为 No.1 可能会让用户在安装过程中观察控制台输出时感到困惑,除非他们已经知道调用setuptools.setup两次的意图。

Besides, whatever rights are needed for installation (e.g., root, if so desired), are certainly present when setup.pyis run (and exactly then). So setup.pycould be considered as the "canonical" use case for this type of action.

此外,安装所需的任何权限(例如,root,如果需要),在setup.py运行时肯定存在(并且恰好在那时)。因此,setup.py可以将其视为此类操作的“规范”用例。

回答by Jon

I think those answers are outdated. In fact you can do:

我认为这些答案已经过时了。事实上,你可以这样做:

import pip
failed = pip.main(["install", nameOfPackage])

and insert any additional args in the list that you pass to main(). It returns 0 (failed) or 1 (success)

并在传递给 main() 的列表中插入任何其他参数。它返回 0(失败)或 1(成功)

Jon

乔恩

回答by jesusperaltac

If you are behind a proxy, you can installa module within code as follow...

如果你在代理后面,你可以在代码中安装一个模块,如下所示......

import pip
pip.main(['install', '--proxy=user:password@proxy:port', 'packagename'])

回答by X0FloH

I used the os.system to emulate the terminal installing a pip module, (I know os.system is deprecated, but it still works and it is also the easiest way to do it), E.G I am making a Game Engine which has multiple python scripts that all use Pygame, in the startup file I use this code to install pygame onto the user's system if they don't have it:

我使用 os.system 来模拟安装 pip 模块的终端,(我知道 os.system 已被弃用,但它仍然有效,这也是最简单的方法),例如我正在制作一个具有多个的游戏引擎python 脚本都使用 Pygame,在启动文件中,如果用户没有它,我使用此代码将 pygame 安装到用户系统上:

import os
os.system('pip install pygame')

Unfortunately, I don't know how to install pip if they don't have it so this script is dependent on pip.

不幸的是,如果他们没有 pip,我不知道如何安装它,所以这个脚本依赖于 pip。

回答by Damian Russak

pip.main() no longer works in pip version 10 and above. You need to use:

pip.main() 不再适用于 pip 版本 10 及更高版本。您需要使用:

from pip._internal import main as pipmain

pipmain(['install', 'package-name'])

For backwards compatibility you can use:

为了向后兼容,您可以使用:

try:
    from pip import main as pipmain
except ImportError:
    from pip._internal import main as pipmain