6

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.

0

1 Answer 1

8

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
Sign up to request clarification or add additional context in comments.

2 Comments

Could you please update the post to remove the one remaining use of distutils, since it's deprecated?
And in fact, this also doesn't work. You actually have to override the build_py cmdclass and check the editable_mode attribute as described here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.