[英]Checking whether pip is installed?
I am using Python 2.7.12
and I want to check whether the pip is installed or not.我正在使用
Python 2.7.12
,我想检查是否安装了 pip。 For this, in command line of Python application I wrote pip list and pressed enter.为此,在 Python 应用程序的命令行中,我编写了 pip list 并按了 Enter。 However, I get an error like:
但是,我收到如下错误:
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谢谢
Use command line and not python.使用命令行而不是 python。
TLDR; TLDR; On Windows, do:
在 Windows 上,请执行以下操作:
python -m pip --version
OR或
py -m pip --version
Details:详情:
On Windows, ~> (open windows terminal)在 Windows 上,~>(打开 Windows 终端)
Start (or Windows Key) > type "cmd" Press Enter
You should see a screen that looks like this你应该看到一个看起来像这样的屏幕
To check to see if pip is installed.检查是否安装了pip。
python -m pip --version
if pip is installed, go ahead and use it.如果安装了 pip,请继续使用它。 for example:
例如:
Z:\>python -m pip install selenium
if not installed, install pip, and you may need to如果没有安装,安装pip,你可能需要
add its path to the environment variables . 将其路径添加到环境变量中。 (basic - windows)
(基本 - 窗口)
add path to environment variables (basic+advanced) 添加环境变量的路径(基本+高级)
if python is NOT installed you will get a result similar to the one below如果没有安装 python,你会得到类似于下面的结果
Install python.安装蟒蛇。 add its path to environment variables.
将其路径添加到环境变量中。
UPDATE: for newer versions of python replace "python" with py - see @gimmegimme's comment and link https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/更新:对于较新版本的 python,将“python”替换为 py - 请参阅@gimmegimme 的评论和链接https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/
$ which pip
or或
$ pip -V
execute this command into your terminal.在终端中执行此命令。 It should display the location of executable file eg.
它应该显示可执行文件的位置,例如。 /usr/local/bin/pip and the second command will display the version if the pip is installed correctly.
/usr/local/bin/pip如果pip安装正确,第二个命令将显示版本。
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/
UPDATE I would like to mention a few things.更新我想提几点。 When working with Django I learned that my Linux install requires me to use python 2.7, so switching my default python version for the
python
and pip
command alias's to python 3 with alias python=python3
is not recommended.在使用 Django 时,我了解到我的 Linux 安装要求我使用 python 2.7,因此不建议将
python
和pip
命令别名的默认 python 版本切换到 python 3, alias python=python3
。 Therefore I use the python3
and pip3
commands when installing software like Django 3.0, which works better with Python 3. And I keep their alias's pointed towards whatever Python 3 version I want like so alias python3=python3.8
.因此,我在安装像 Django 3.0 这样的软件时使用
python3
和pip3
命令,这在 Python 3 中效果更好。我将它们的别名指向我想要的任何 Python 3 版本,例如alias python3=python3.8
。
Keep In Mind When you are going to use your package in the future you will want to use the pip
or pip3
command depending on which one you used to initially install the package.请记住当您将来要使用您的软件包时,您将需要使用
pip
或pip3
命令,具体取决于您最初安装软件包时使用的命令。 So for example if I wanted to change my change my Django package version I would use the pip3
command and not pip
like so, pip3 install Django==3.0.11
.因此,例如,如果我想更改我的 Django 包版本,我将使用
pip3
命令而不是pip
像这样pip3 install Django==3.0.11
。
Notice When running checking the packages version for python: $ python -m django --version
and python3: $ python3 -m django --version
, two different versions of django will show because I installed django v3.0.11 with pip3
and django v1.11.29 with pip
.注意当运行检查 python 的包版本:
$ python -m django --version
和 python3: $ python3 -m django --version
,会显示两个不同版本的 django,因为我安装了 django v3.0.11 和pip3
和 django v1。 11.29 与pip
。
pip list
is a shell command. pip list
是一个 shell 命令。 You should run it in your shell (bash/cmd), rather than invoke it from python interpreter.您应该在 shell (bash/cmd) 中运行它,而不是从 python 解释器中调用它。
pip
does not provide a stable API. pip
不提供稳定的 API。 The only supported way of calling it is via subprocess
, see docs and the code at the end of this answer.唯一支持的调用方式是通过
subprocess
,请参阅文档和本答案末尾的代码。
However, if you want to just check if pip
exists locally, without running it, and you are running Linux, I would suggest that you use bash's which
command:但是,如果您只想检查本地是否存在
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 running pip
is not an issue, you could just do:如果运行
pip
不是问题,你可以这样做:
python -m pip --version
If you really need to do it from a python script, you can always put the import statement into a try...except
block:如果您确实需要从 python 脚本执行此操作,您始终可以将 import 语句放入
try...except
块中:
try:
import pip
except ImportError:
print("Pip not present.")
Or check what's the output of a pip --version
using subprocess
module:或者使用
subprocess
模块检查pip --version
的输出是什么:
subprocess.check_call([sys.executable, '-m', 'pip', '--version'])
You need to run pip list
in bash not in python.您需要在 bash 中而不是在 python 中运行
pip list
。
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
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.