简体   繁体   English

如何从 CMD 运行 Pip 命令

[英]How to run Pip commands from CMD

As I understand, Python 2.7.9 comes with Pip installed, however when I try to execute a Pip command from CMD (Windows) I get the following error:据我了解,Python 2.7.9 安装了 Pip,但是当我尝试从 CMD (Windows) 执行 Pip 命令时,出现以下错误:

'pip' is not recognized as an internal or external command, operable program or batch file.

When I type python I do get the following, which suggests it has been installed correctly:当我键入python时,我得到以下信息,这表明它已正确安装:

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

I did however need to add some environmental variables to get the python part working on CMD:但是,我确实需要添加一些环境变量才能使 python 部分在 CMD 上运行:

  • Add to the environment variable PATH : "C:\Python27\"添加到环境变量PATH : "C:\Python27\"

  • Define the system variable PYTHONPATH : "C:\Python27\"定义系统变量PYTHONPATH : "C:\Python27\"

I cannot find a Pip folder within the Python directory, however there is a folder called "ensurepip" in C:\Python27\Lib\ .我在 Python 目录中找不到 Pip 文件夹,但是在C:\Python27\Lib\中有一个名为“ensurepip”的文件夹。

Does anybody know how can I get Pip commands to start working in CMD?有人知道如何获得 Pip 命令以在 CMD 中开始工作吗?

Little side note for anyone new to Python who didn't figure it out by theirself: this should be automatic when installing Python, but just in case, note that to run Python using the python command in Windows' CMD you must first add it to the PATH environment variable, as explained here .对于没有自己弄明白的 Python 新手,请注意一点:在安装 Python 时应该是自动的,但为了以防万一,请注意,要在 Windows 的 CMD 中使用python命令运行 Python,您必须首先将其添加到在PATH环境变量,解释在这里


To execute Pip, first of all make sure you have it installed, so type in your CMD:要执行 Pip,首先要确保你已经安装了它,所以输入你的 CMD:

> python
>>> import pip
>>>

And it should proceed with no error.它应该没有错误地继续。 Otherwise, if this fails, you can look here to see how to install it.否则,如果此操作失败,您可以在此处查看如何安装它。 Now that you are sure you've got Pip, you can run it from CMD with Python using the -m (module) parameter, like this:现在您确定您已经获得了 Pip,您可以使用-m (模块)参数从 CMD 使用 Python 运行它,如下所示:

> python -m pip <command> <args>

Where <command> is any Pip command you want to run, and <args> are its relative arguments, separated by spaces.其中<command>是您要运行的任何 Pip 命令,而<args>是其相对参数,以空格分隔。

For example, to install a package:例如,要安装一个包:

> python -m pip install <package-name>

Newer versions of Python come with py , the Python Launcher , which is always in the PATH .较新版本的 Python 带有py ,即Python 启动器,它始终位于PATH

Here is how to invoke pip via py :以下是通过py调用pip

py -m pip install <packagename>

py allows having several versions of Python on the same machine. py允许在同一台机器上有多个版本的 Python。

As an example, here is how to invoke the pip from Python 2.7:例如,以下是如何从 Python 2.7 调用pip

py -2.7 -m pip install <packagename>

Make sure to also add "C:\\Python27\\Scripts" to your path.确保还将“C:\\Python27\\Scripts”添加到您的路径中。 pip.exe should be in that folder. pip.exe 应该在该文件夹中。 Then you can just run:然后你可以运行:

C:\> pip install modulename

Go to the folder where Python is installed .. and go to Scripts folder .转到安装 Python 的文件夹 .. 并转到 Scripts 文件夹。

Do all this in CMD and then type :在 CMD 中执行所有这些操作,然后键入:

pip

to check whether its there or not .检查它是否存在。

As soon as it shows some list it means that it is there .只要它显示一些列表,就意味着它就在那里。

Then type然后输入

pip install <package name you want to install>

Firstly make sure that you have installed python 2.7 or higher首先确保你已经安装了 python 2.7 或更高版本

Open Command Prompt as administrator and change directory to python and then change directory to Scripts by typing cd Scripts then type pip.exe and now you can install modules Step by Step:以管理员身份打开命令提示符并将目录更改为 python,然后通过键入 cd Scripts 将目录更改为 Scripts,然后键入 pip.exe,现在您可以逐步安装模块:

  • Open Cmd打开命令

  • type in "cd \\" and then enter输入“cd \\”,然后输入

  • type in "cd python2.7" and then enter输入“cd python2.7”然后输入

Note that my python version is 2.7 so my directory is that so use your python folder here...请注意,我的 python 版本是 2.7,所以我的目录是这样的,所以在这里使用你的 python 文件夹......

  • type in "cd Scripts" and enter输入“cd Scripts”并输入

  • Now enter this "pip.exe"现在输入这个“pip.exe”

  • Now it prompts you to install modules现在它会提示你安装模块

Simple solution that worked for me is, set the path of python in environment variables,it is done as follows对我有用的简单解决方案是,在环境变量中设置python的路径,如下完成

  1. Go to My Computer转到我的电脑
  2. Open properties打开属性
  3. Open Advanced Settings打开高级设置
  4. Open Environment Variables开放环境变量
  5. Select path选择路径
  6. Edit it编辑它

In the edit option click add and add following two paths to it one by one:在编辑选项中单击添加并一一添加以下两个路径:

C:\Python27

C:\Python27\Scripts

and now close cmd and run it as administrator, by that pip will start working.现在关闭 cmd 并以管理员身份运行它,通过该 pip 将开始工作。

In my case I was trying to install Flask.就我而言,我试图安装 Flask。 I wanted to run pip install Flask command.我想运行pip install Flask命令。 But when I open command prompt it I goes to C:\\Users[user]>.但是当我打开命令提示符时,我会转到 C:\\Users[user]>。 If you give here it will say pip is not recognized.如果你在这里给出,它会说 pip 不被识别。 I did below steps我做了以下步骤

On your desktop right click Computer and select Properties在您的桌面上右键单击计算机并选择属性

Select Advanced Systems Settings选择高级系统设置

In popup which you see select Advanced tab and then click Environment Variables在您看到的弹出窗口中,选择“高级”选项卡,然后单击“环境变量”

In popup double click PATH and from popup copy variable value for variable name PATH and paste the variable value in notepad or so and look for an entry for Python.在弹出窗口中双击 PATH 并从弹出的变量名称 PATH 复制变量值并将变量值粘贴到记事本等中,然后查找 Python 条目。

In my case it was C:\\Users\\[user]\\AppData\\Local\\Programs\\Python\\Python36-32就我而言,它是C:\\Users\\[user]\\AppData\\Local\\Programs\\Python\\Python36-32

Now in my command prompt i moved to above location and gave pip install Flask现在在我的命令提示符中,我移到了上面的位置并给了pip install Flask

在此处输入图片说明

This is the simplest way.这是最简单的方法。

Within Command Prompt:在命令提示符内:

py -m pip install <ModuleNameGoesHere>

This will run anywhere as long as you have python installed.只要您安装了 python,它就可以在任何地方运行。

Breakdown:分解:

  • py is a universal python command for most versions of Python. Which is nice because you don't have to find and navigate to a specific or complex path. py是适用于 Python 的大多数版本的通用 python 命令。这很好,因为您不必查找并导航到特定或复杂的路径。
  • -m stands for module -m代表模块
  • pip is the package management system used to install and manage software packages written in Python pip是package管理系统,用于安装和管理Python编写的软件包
  • install is the Pip command for installation install是Pip安装命令
  • ModuleName... is the placeholder for the module/package object you wish to download into python. ModuleName...是您希望下载到 python 的模块/包 object 的占位符。

Example:例子:

py -m pip install requests or py -m pip install pandas py -m pip install requestspy -m pip install pandas

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM