如何判断是否安装了 Python setuptools?

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

How can I tell if Python setuptools is installed?

pythonpipsetuptools

提问by Ryan Nelson

I'm writing a quick shell script to make it easier for some of our developers to run Fabric. (I'm also new to Python.) Part of installing Fabric is installing pip, and part of installing pip is installing setuptools.

我正在编写一个快速的 shell 脚本,以使我们的一些开发人员更容易运行 Fabric。(我也是 Python 新手。)安装 Fabric 的一部分是安装 pip,而安装 pip 的一部分是安装 setuptools。

Is there any easy way to detect if setuptools is already installed? I'd like to make it possible to run the script multiple times, and it skip anything it's already done. As it stands now, if you run ez_setup.py twice in a row, you'll get a failure the second time.

有没有什么简单的方法可以检测 setuptools 是否已经安装?我想让多次运行脚本成为可能,它会跳过已经完成的任何事情。就目前而言,如果您连续运行 ez_setup.py 两次,第二次就会失败。

One idea I had was to look for the easy_install scripts under the /Scripts folder. I can guess at the Python root using sys.executable, and then swap off the executable name itself. But I'm looking for something a little more elegant (and perhaps cross-OS friendly). Any suggestions?

我的一个想法是在 /Scripts 文件夹下查找 easy_install 脚本。我可以使用 sys.executable 猜测 Python 根目录,然后交换可执行文件名称本身。但我正在寻找更优雅的东西(也许是跨操作系统友好的)。有什么建议?

采纳答案by grim

This isn't great but it'll work.

这不是很好,但它会起作用。

A simple python script can do the check

一个简单的python脚本可以做检查

import sys
try:
    import setuptools
except ImportError:
    sys.exit(1)
else:
    sys.exit(0)

OR

或者

try:
    import setuptools
except ImportError:
    print("Not installed.")
else:
    print("Installed.")

Then just check it's exit code in the calling script

然后只需在调用脚本中检查它的退出代码

回答by Brian Zelip

you can check for easy_install and setuptools by running the following command line commands:

您可以通过运行以下命令行命令来检查 easy_install 和 setuptools:

which easy_install
#finds the path to easy_install if it exists

less path/to/easy_install
#where path/to/easy_install is the output from the above command
#this outputs your easy_install script which will mention the version of setuptools

If the easy_install/setuptools bundle is installed, your output from the second command above will probably read something like this:

如果安装了 easy_install/setuptools 包,上面第二个命令的输出可能会是这样的:

#EASY-INSTALL-ENTRY-SCRIPT: 'setuptools==0.6c11','console_scripts','easy_install'

回答by bosari

Try with this command.

试试这个命令。

$ pip list

It returns the versions of both pipand setuptools. Otherwise try with

它同时返回的版本pipsetuptools。否则尝试

$ pip install pil

If this also doesn't work, then try with

如果这也不起作用,请尝试使用

$ which easy_install

回答by Doge Woof

Just run the following code into IDLE:

只需在 IDLE 中运行以下代码:

import easy_install

If it just goes to the next line, I think it's installed. If it says:

如果它只是转到下一行,我认为它已安装。如果它说:

Error: invalid syntax

Then it probably isn't installed. I know this because I tested pip with it. Also just check import pipto see if pip is pre-installed. :)

那么它可能没有安装。我知道这一点,因为我用它测试了 pip。也只需检查import pippip 是否已预先安装。:)

回答by ivandov

It comes preinstalled with new versions of Python.

它预装了新版本的 Python。

pip3 list

was enough to identify it was installed for me

足以确定它是为我安装的

回答by Ritwika Gautam

This will display the version of your setuptools if it is installed already

如果已经安装,这将显示您的 setuptools 的版本

$python -c "import sys; import setuptools; print(setuptools.version.__version__)"

$python -c "import sys; import setuptools; print(setuptools.version.__version__)"

回答by jeffjayjay

Depends with the python version installed. you can try "pip list"or "pip3 list"and check for the setuptools and version installed.

取决于安装的 python 版本。您可以尝试"pip list""pip3 list"检查安装的 setuptools 和版本。