Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detect pip in setup.py

Gist: What is the best way to detect in setup.py that we are being triggered by pip install package?

Background: I have a package (bindings for a C-library), for which I provide eggs that include the library itself. In my readme/docs I note that this package is 'easy_install-able' on certain platforms. When building from source (e.g. with pip), the library itself is a build dependency. The problem is that I somewhat regularly have confused users who mistakenly believe that pip is a full replacement for easy_install, and expect pip install package to work on systems without the library, or even without a compiler, where the egg is what they really want.

I would like to detect that the build has been triggered by pip, so I can provide a friendly "pip != easy_install" message if it fails due to lacking the library. It doesn't need to be perfect, just catch the most common cases of pip install package. On inspection, it doesn't seem like there is a particularly robust way to do this, and the best I have come up with is:

probably_using_pip = '--single-version-externally-managed' in sys.argv

Is there a better (or better yet, official) way to detect pip from setup.py?

like image 568
minrk Avatar asked Mar 31 '12 20:03

minrk


People also ask

How does pip use setup py?

If you use setup.py , you have to visit the library's website, figure out where to download it, extract the file, run setup.py ... In contrast, pip will automatically search the Python Package Index (PyPi) to see if the package exists there, and will automatically download, extract, and install the package for you.

Does pip install invoke setup py?

For installing packages in “editable” mode (pip install --editable), pip will invoke setup.py develop , which will use setuptools' mechanisms to perform an editable/development installation.

How do I find pip path?

To check that pip has been added to your PATH variable: Press the Windows key + R to open Run. Type cmd and then press Enter for the command prompt. For a list of the locations added to your PATH variable, type echo %PATH% and hit Enter.

How do I get-pip in Python?

Step 1: Download the get-pip.py (https://bootstrap.pypa.io/get-pip.py) file and store it in the same directory as python is installed. Step 2: Change the current path of the directory in the command line to the path of the directory where the above file exists. Step 4: Now wait through the installation process. Voila!


1 Answers

__file__ in setup gives something like /tmp/pip-DNpsLw-build/setup.py if ran from pip.

from setuptools import setup

def determineInstaller():
    if 'pip' in __file__:
        print('========pip triggered build========') #add smiley for friendliness :)
    return 'dummy description'

setup(name='bla',
      version='0.0',
      description=determineInstaller(),
      )
like image 155
Jasper van den Bosch Avatar answered Oct 22 '22 12:10

Jasper van den Bosch