Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if Python setuptools is installed?

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.

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.

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?

like image 452
Ryan Nelson Avatar asked Oct 17 '13 15:10

Ryan Nelson


2 Answers

Try with this command.

$ pip list

It returns the versions of both pip and setuptools. Otherwise try with

$ pip install pil

If this also doesn't work, then try with

$ which easy_install
like image 52
bosari Avatar answered Oct 08 '22 00:10

bosari


This isn't great but it'll work.

A simple python script can do the check

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

like image 45
grim Avatar answered Oct 08 '22 01:10

grim