Python 检查pip是否安装?

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

Checking whether the pip is installed?

pythonpython-2.7pipsyntax-error

提问by Merto

I am using Python 2.7.12and I want to check whether the pip is installed or not. For this, in command line of Python application I wrote pip list and pressed enter. However, I get an error like:

我正在使用Python 2.7.12,我想检查是否安装了 pip。为此,在 Python 应用程序的命令行中,我编写了 pip list 并按了 Enter。但是,我收到如下错误:

File"stdin",line 1

pip list

Syntax Error: invalid syntax

So, how can I solve this issue and get the list of modules as an output?

那么,如何解决这个问题并获得模块列表作为输出?

Thanks

谢谢

回答by TimeTrax

Use command lineand not python.
TLDR; On Windows, do: python -m pip --version

Details:

On Windows, ~> (open windows terminal)
Start (or Windows Key) > type "cmd" Press Enter
You should see a screen that looks like this enter image description here
To check to see if pip is installed.

使用命令行而不是 python。
TLDR;在 Windows 上,执行: python -m pip --version

详细信息:

在 Windows 上,~>(打开 windows 终端)
Start (or Windows Key) > type "cmd" Press Enter
您应该会看到如下所示的屏幕 检查是否安装了 pip。在此处输入图片说明

python -m pip --version

if pip is installed, go ahead and use it. for example:

如果安装了 pip,请继续使用它。例如:

Z:\>python -m pip install selenium

if not installed, install pip, and you may need to
add its path to the environment variables.
(basic - windows)
add path to environment variables(basic+advanced)

如果没有安装,请安装pip,您可能需要
将其路径添加到环境变量中
(basic - windows)
添加环境变量的路径(basic+advanced)

if python is NOT installed you will get a result similar to the one below

enter image description here

如果没有安装 python,你会得到类似于下面的结果

在此处输入图片说明

Install python. add its path to environment variables.

安装蟒蛇。将其路径添加到环境变量中。

回答by Tushar Niras

$ which pip

or

或者

 $ pip -V 

execute this command into your terminal. It should display the location of executable file eg. /usr/local/bin/pipand the second command will display the version if the pip is installed correctly.

在终端中执行此命令。它应该显示可执行文件的位置,例如。/usr/local/bin/pip如果pip安装正确,第二个命令将显示版本。

回答by philantrovert

You need to run pip listin bash not in python.

您需要pip list在 bash 中运行而不是在 python 中运行。

pip list
DEPRECATION: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. A future version of pip will drop support for Python 2.6
argparse (1.4.0)
Beaker (1.3.1)
cas (0.15)
cups (1.0)
cupshelpers (1.0)
decorator (3.0.1)
distribute (0.6.10)
---and other modules

回答by B?a?ej Michalik

pip listis a shell command. You should run it in your shell (bash/cmd), rather than invoke it from python interpreter.

pip list是一个shell命令。您应该在 shell (bash/cmd) 中运行它,而不是从 python 解释器中调用它。

If you want to do it from a python script, you would need to import pipmodule first:

如果要从 python 脚本执行此操作,则需要先导入pip模块:

import pip
pip.get_installed_distributions()

However, if you want to just check if pipexists locally, and you are running Linux, I would suggest that you use bash's whichcommand:

但是,如果您只想检查pip本地是否存在,并且您正在运行 Linux,我建议您使用 bash 的which命令:

which pip

It should show you whether the command can be found in bash's PATH/aliases, and if it does, what does it actually execute.

它应该向您显示该命令是否可以在 bash 的PATH/aliases 中找到,如果可以,它实际执行了什么。

If you really need to do it from a python script, you can always put the import statement into a try...exceptblock:

如果您确实需要从 python 脚本执行此操作,您始终可以将 import 语句放入一个try...except块中:

try:
    import pip
except ImportError:
    print("Pip not present.")

回答by w3Develops

If you are on a linux machine running Python 2 you can run this commands:

如果您在运行 Python 2 的 linux 机器上,您可以运行以下命令:

1st make sure python 2 is installed:

1st确保安装了python 2:

python2 --version

2nd check to see if pip is installed:

第二次检查是否安装了pip:

pip --version

If you are running Python 3 you can run this command:

如果您运行的是 Python 3,则可以运行以下命令:

1st make sure python 3 is installed:

1st确保安装了python 3:

python3 --version

2nd check to see if pip3 is installed:

第二次检查是否安装了pip3:

pip3 --version

If you do not have pip installed you can run these commands to install pip (it is recommended you install pip for Python 2 and Python 3):

如果您没有安装 pip,您可以运行这些命令来安装 pip(建议您为 Python 2 和 Python 3 安装 pip):

Install pip for Python 2:

为 Python 2 安装 pip:

sudo apt install python-pip

Then verify if it is installed correctly:

然后验证是否安装正确:

pip --version

Install pip for Python 3:

为 Python 3 安装 pip:

sudo apt install python3-pip

Then verify if it is installed correctly:

然后验证是否安装正确:

pip3 --version

For more info see: https://itsfoss.com/install-pip-ubuntu/

有关更多信息,请参阅:https: //itsfoss.com/install-pip-ubuntu/

回答by Inconnu

In CMD, type:

在 CMD 中,输入:

pip freeze

And it will show you a list of all the modules installed including the version number.

它会向您显示已安装的所有模块的列表,包括版本号。

Output:

输出:

aiohttp==1.1.4
async-timeout==1.1.0
cx-Freeze==4.3.4
Django==1.9.2
django-allauth==0.24.1
django-cors-headers==1.2.2
django-crispy-forms==1.6.0
django-robots==2.0
djangorestframework==3.3.2
easygui==0.98.0
future==0.16.0
httpie==0.9.6
matplotlib==1.5.3
multidict==2.1.2
numpy==1.11.2
oauthlib==1.0.3
pandas==0.19.1
pefile==2016.3.28
pygame==1.9.2b1
Pygments==2.1.3
PyInstaller==3.2
pyparsing==2.1.10
pypiwin32==219
PyQt5==5.7
pytz==2016.7
requests==2.9.1
requests-oauthlib==0.6
six==1.10.0
sympy==1.0
virtualenv==15.0.3
xlrd==1.0.0
yarl==0.7.0