在 python idle 中运行 pip

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34989626/
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-19 15:50:37  来源:igfitidea点击:

Run pip in python idle

pythonpippython-idle

提问by Hyun-geun Kim

I am curious about running pip. Everytime I ran pip in command shell in windows like that

我对运行 pip 很好奇。每次我在 Windows 中的命令 shell 中运行 pip 时

c:\python27\script>pip install numpy

But, I wondered if I can run it in python idle.

但是,我想知道是否可以在 python idle 中运行它。

import pip
pip.install("numpy")

Unfortunately, it is not working.

不幸的是,它不起作用。

回答by fn.

At moment there are no official way to do it, you could use pip.main but you current idle session will not 'see' this installed package.

目前没有官方的方法可以做到这一点,您可以使用 pip.main 但您当前的空闲会话不会“看到”这个已安装的包。

There been a lot a discussion over how to add a "high level" programmatic API for pip, it's seems promising.

关于如何为 pip 添加“高级”编程 API有很多讨论,看起来很有希望。

回答by L_Pav

Actually, I think, you can use subprocess.Popen(apt-get numpy), not sure how to do it with PIP though.

实际上,我认为,您可以使用subprocess.Popen(apt-get numpy),但不确定如何使用 PIP。

回答by Terry Jan Reedy

This question is, or should be, about how to run pip from a python program. IDLE is not directly relevant to this version of the quesiton.

这个问题是,或者应该是,关于如何从 python 程序运行 pip。IDLE 与此版本的问题没有直接关系。

To expand on J. J. Hakala's comment: a command-line such as pip install pillowis split on spaces to become sys.argv. When pip is run as a main module, it calls pip.main(sys.argv[1:]). If one imports pip, one may call pip.main(arg_line.split()), where arg_lineis the part of the command line after pip.

扩展 JJ Hakala 的评论:诸如pip install pillow在空格上拆分的命令行变为sys.argv. 当 pip 作为主模块运行时,它会调用pip.main(sys.argv[1:]). 如果一个进口点子,一个可以调用pip.main(arg_line.split()),这里arg_line是后命令行的一部分pip

Last September (2015) I experimented with using this unintended API from another python program and reported the initial results on tracker issue 23551. Discussion and further results followed.

去年九月(2015 年),我尝试从另一个 python 程序中使用这个意外的 API,并报告了跟踪器问题23551的初步结果。讨论和进一步的结果随之而来。

The problem with executing multiple commands in one process is that some pip commands cache not only sys.path, which normally stays constant, but also the list of installed packages, which normally changes. Since pip is designed to run one command per process, and then exit, it never updates the cache. When pip.main is used to run multiple commands in one process, commands given after the caching may use a stale and no-longer-correct cache. For example, listafter installshows how things were before the install.

在一个进程中执行多个命令的问题在于,一些 pip 命令不仅缓存了通常保持不变的 sys.path,而且缓存了通常会更改的已安装包列表。由于 pip 旨在为每个进程运行一个命令,然后退出,因此它永远不会更新缓存。当 pip.main 用于在一个进程中运行多个命令时,缓存后给出的命令可能会使用陈旧且不再正确的缓存。例如,listafterinstall显示了 install 之前的情况

A second problem for a program that wants to examine the output from pip is that it goes to stdout and stderr. I posted a programthat captures these streams into program variables as part of running pip.

想要检查 pip 输出的程序的第二个问题是它转到 stdout 和 stderr。我发布了一个程序,将这些流捕获到程序变量中,作为运行 pip 的一部分。

Using a subprocess call for each pip command, as suggested by L_Pav, though less efficient, solves both problems. The communicatemethod makes the output streams available. See the subprocess doc.

根据 L_Pav 的建议,对每个 pip 命令使用子进程调用,虽然效率较低,但可以解决这两个问题。该communicate方法使输出流可用。请参阅子流程文档。

回答by Grifin

If your on a Mac you should be able to do it like this:

如果您在 Mac 上,您应该可以这样做:

  1. Go to your IDLE.
  2. Run help('modules').
  3. Find the HTML module.
  4. Run help('HTML')
  5. There should pop up a file map, for example this/file/map/example/.
  6. Go to the finder and do command+shift+g and paste the file map there. Please delete the last file, because then your gonna go to the modules files.
  7. There are all the modules. If you want to add modules, download the files of the module and put them there.
  1. 去你的空闲。
  2. 运行 help('modules')。
  3. 找到 HTML 模块。
  4. 运行帮助('HTML')
  5. 应该会弹出一个文件映射,例如 this/file/map/example/。
  6. 转到查找器并执行 command+shift+g 并将文件映射粘贴到那里。请删除最后一个文件,因为那样你会去模块文件。
  7. 有所有的模块。如果要添加模块,请下载模块的文件并将它们放在那里。

I hope this helps you.

我希望这可以帮助你。