有没有办法使用pip一次安装所有python模块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38962947/
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
Is there a way to install all python modules at once using pip?
提问by rsnaveen
I would like to install all available modules for Python 2.7.12 using a single pip command. Is there a way to do this without having to specify every single package name?
我想使用单个 pip 命令为 Python 2.7.12 安装所有可用模块。有没有办法在不必指定每个包名称的情况下做到这一点?
回答by enderland
I highly recommend against doing this - the overwhelmingly supported best practice is to use a requirements.txt file, listing the packages you want to install specifically.
我强烈建议不要这样做 - 绝大多数支持的最佳实践是使用 requirements.txt 文件,列出您要专门安装的包。
You then install it with pip install -r requirements.txt
and it installs all the packages for your project.
然后你安装它,pip install -r requirements.txt
它会为你的项目安装所有的包。
This has several benefits:
这有几个好处:
- Repeatability by installing only the required packages
- Conciseness
- 通过仅安装所需的包来实现可重复性
- 简明
However, if you really dowant to install ALL python packages (note that there are thousands), you can do so via the following:
但是,如果你真的不想要安装所有的Python包(请注意,有数以千计的),您可以通过以下操作:
pip search * | grep ")[[:space:]]" | cut -f1 -d" "
点子搜索 * | grep ")[[:space:]]" | 剪切 -f1 -d" "
I strongly recommend against this as it's likely to do horrible things to your system, as it will attempt to install every python package(and why it's in spoiler tags).
我强烈建议不要这样做,因为它可能会对您的系统造成可怕的后果,因为它会尝试安装每个 python 包(以及为什么它在剧透标签中)。
回答by Wayne Werner
This is a terrible idea, but you could always use autoinstaller, which will automatically download packages via pip
if you don't have them installed.
这是一个糟糕的主意,但您始终可以使用autoinstaller,pip
如果您没有安装它们,它将自动下载软件包。
回答by Cool Charac
UPDATE:The other answers wereright when they said that this was a terrible idea: Some of the packages I have installed using this method will not work because their fixed-old required dependencies have been updated by other packages. Next time, I'm using virtualenv. Some day, I'm going to produce a list of packages from packages.pypy.org with their dependencies, so that someone can make many environments with those packages quickly.
更新:当他们说这是一个糟糕的主意时,其他答案是正确的:我使用此方法安装的某些软件包将无法正常工作,因为它们的固定旧必需依赖项已被其他软件包更新。下一次,我使用 virtualenv。总有一天,我会从packages.pypy.org 生成一个包含它们的依赖项的包列表,以便有人可以快速地使用这些包创建许多环境。
Well, I did find a way to do this, but only for the 1000 most popular packages at http://packages.pypy.org. But other pages can be processed too, it just needs some creativity. For that page you will need a spreadsheet program to process tha page. I used LibreOffice Calc. And I did do this on windows using the Command Prompt.
嗯,我确实找到了一种方法来做到这一点,但仅限于http://packages.pypy.org 上的 1000 个最流行的包。但是其他页面也可以处理,只是需要一些创意。对于该页面,您将需要一个电子表格程序来处理该页面。我使用了 LibreOffice Calc。我确实使用命令提示符在 Windows 上执行此操作。
First, open the link I just posted and select/copy all the text (and do not expand any of the package names on the webpage).
首先,打开我刚刚发布的链接并选择/复制所有文本(不要展开网页上的任何包名称)。
Then, paste it on any cell in Calc. Open the Find & Replace dialog. If you use another spreadsheet program, make sure it's Find & Replace supports regexps. Select the checkbox next to Regular Expressions
(or something like that), type downloads:\s+\d+
in the Search for
field, make sure there is no text in the Replace with
field, and leave all the other options at their defaults, click Replace all
(assuming the program has this button). After that, you get rid of the trailing spaces by doing another replace with a space in the Search for
field (Python package names do not have spaces). Once you do these two replaces, you have a package name in each cell.
然后,将其粘贴到 Calc 中的任何单元格上。打开查找和替换对话框。如果您使用其他电子表格程序,请确保它的查找和替换支持正则表达式。选择旁边的复选框Regular Expressions
(或类似的东西),downloads:\s+\d+
在Search for
字段中键入,确保字段中没有文本Replace with
,并将所有其他选项保留为默认值,单击Replace all
(假设程序具有此按钮)。之后,您可以通过在Search for
字段中再次用空格替换来去除尾随空格(Python 包名称没有空格)。完成这两个替换后,每个单元格中都有一个包名称。
Then you copy the entire spreadsheet into a text editor, any editor should work (I used Notepad). The text editor will put the contents of each cell (i.e. the package names) onto it's own line. There won't be any blank lines in the file (but it doesn't hurt to check). At this point you can close the spreadsheet, you don't have to save it's data.
然后将整个电子表格复制到文本编辑器中,任何编辑器都可以使用(我使用的是记事本)。文本编辑器将把每个单元格的内容(即包名)放到它自己的行上。文件中不会有任何空行(但检查也无妨)。此时您可以关闭电子表格,而不必保存其数据。
Now save the text file. Open a command prompt and change to the folder where you saved the text file. Now type:
现在保存文本文件。打开命令提示符并切换到保存文本文件的文件夹。现在输入:
for /F %p in ('type TEXT_FILE_NAME.txt') DO pip.exe install %p
[If Command Prompt can't find the right pip to use, then you need to type the full path to pip e.g. C:\Python27\Scripts\pip.exe]
[如果命令提示符找不到要使用的正确 pip,那么您需要输入 pip 的完整路径,例如 C:\Python27\Scripts\pip.exe]
Voila, pip is now installing all of the packages listed on the link I gave.
瞧,pip 现在正在安装我提供的链接中列出的所有软件包。
You should redirect stdout (and maybe stderr too) to a file because command prompts don't have a large history buffer. For that, you put something like this at the end of the command:
您应该将标准输出(也可能是标准错误)重定向到文件,因为命令提示符没有大的历史缓冲区。为此,您可以在命令的末尾添加如下内容:
>>PIP_STDOUT_FILE.txt 2>>PIP_STDERR_FILE.txt
Be careful about abruptly interrupting pip if you use this, because output no longer goes to the terminal. You might kill it in the middle of a package installation. In particular, if you use a text editor to view the output, reload the file frequently. It's safer to append --log LOG_FILE.txt
instead of the above, but the log file becomes more verbose and also writes its messages to the command prompt as usual.
如果您使用它,请小心突然中断 pip,因为输出不再发送到终端。您可能会在软件包安装过程中终止它。特别是,如果您使用文本编辑器查看输出,请经常重新加载文件。附加--log LOG_FILE.txt
而不是上面的更安全,但日志文件变得更加冗长,并且像往常一样将其消息写入命令提示符。
There are quite a number of
有相当多的
- Linux-only packages
- Packages built with a Microsoft C++ compiler
- 仅限 Linux 的软件包
- 使用 Microsoft C++ 编译器构建的包
and using pip to install these will fail unless you do some additional work. But this should work for the majority of packages.
除非你做一些额外的工作,否则使用 pip 安装这些将失败。但这应该适用于大多数软件包。