Derek 朕會功夫
Derek 朕會功夫

Reputation: 94319

How to tell from setup.py if the module is being installed in editable mode

My current setup.py writes the git commit hash in a file which the module can access after it has been installed. However, I would like to disable that when I am developing the module since the setup.py file will not be triggered twice and the hash would be inaccurate. This brings us to the question:

Is there a way to tell from setup.py whether the module is being installed in editable mode? i.e.,

pip install -e .

I found a similar question here, but even the "hack" will not work in my case since the module will be installed directly with git, and the .git directory will exist even for normal installs during installation.

Upvotes: 6

Views: 1401

Answers (1)

hoefling
hoefling

Reputation: 66341

Just override the correct command. install is being run on pip install ., develop on pip install --editable ..

# setup.py
from distutils import log
from setuptools import setup
from setuptools.command.install import install as install_orig
from setuptools.command.develop import develop as develop_orig


class develop(develop_orig):

    def run(self):
        self.announce('this code will run on editable install only', level=log.INFO)
        super().run()


class install(install_orig):

    def run(self):
        self.announce('this code will run on normal install only', level=log.INFO)
        super().run()


setup(
    name='spam',
    cmdclass={'install': install, 'develop': develop}
)

Test it:

$ pip install . -vvv | grep "this code"                                
  this code will run on normal install only
$ pip install -e . -vvv | grep "this code"
  this code will run on editable install only

Upvotes: 8

Related Questions