19

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?

Ryan Nelson
  • 4,466
  • 5
  • 29
  • 45

7 Answers7

27

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
Sithsu
  • 2,209
  • 2
  • 21
  • 28
bosari
  • 1,922
  • 1
  • 19
  • 38
  • Does `pip list` work? In my system its giving a command not found error. Also check out the Markdown syntax in edit help to see what options are available for editing. – Sithsu Nov 06 '14 at 11:23
  • Seems I'm running an older pip. `pip list` is available with pip 1.3. http://stackoverflow.com/questions/6600878/find-all-packages-installed-with-easy-install-pip – Sithsu Nov 06 '14 at 11:43
  • Yes, it works. Being an installable package, you may want to know the setuptools version inside your virtualenv. – HFSDev Nov 01 '16 at 00:30
13

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

Sraw
  • 18,892
  • 11
  • 54
  • 87
grim
  • 760
  • 4
  • 13
4

Just run the following code into 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 pip to see if pip is pre-installed. :)

Doge Woof
  • 41
  • 1
2

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

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-ENTRY-SCRIPT: 'setuptools==0.6c11','console_scripts','easy_install'
Brian Zelip
  • 2,909
  • 4
  • 33
  • 45
2

It comes preinstalled with new versions of Python.

pip3 list

was enough to identify it was installed for me

ivandov
  • 619
  • 8
  • 14
2

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

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

0

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

Humayun Ahmad Rajib
  • 1,502
  • 1
  • 10
  • 22
Geoffrey
  • 11
  • 1