4

I have a Python package that I'm distributing with pip. I need to add some custom code to be run at install time:

from setuptools import setup                                                        

from setuptools.command.install import install                                      

class CustomInstall(install):                                                       
    def run(self):                                                                  
        install.run(self)                                                           
        print "TEST"                                                           

setup(                                                                              
      ...                                      
      cmdclass={'install': CustomInstall},
      ...) 

I thought the problem might pip suppressing stdout: Custom pip install commands not running. But then I replaced print "TEST" with creating a file and writing some text, and that didn't happen either.

It appears that my custom run method is only happening when I create and upload my_package to test PyPI:

python setup.py sdist bdist_wheel upload -r https://testpypi.python.org/pypi

and not when I pip install it:

pip install -i https://testpypi.python.org/pypi my_package

Maybe I am fundamentally not understanding how pip and setuptools work, but that is the opposite of the behavior I expected.

My questions are:

  1. How can I get my CustomInstall class to work?

and

  1. What actually happens when you call pip install?

I've looked a the setuptools docs and the PyPI docs, and I haven't been able to figure it out. It seems like other people have had success with this: Run custom task when call `pip install`, so I'm not sure what's going wrong.

Community
  • 1
  • 1
J Jones
  • 3,060
  • 4
  • 26
  • 43

1 Answers1

0

So I'm not sure how much this will help, but I recently dealt with a similar issue, and here's what I learned.

  1. Your custom install code appears to be correct. However, there are more methods than just run that can be overridden. Another useful one is finalize_options because you can write code to dynamically change the parameters of your setup.py (example here.)

  2. This is a very good question to ask.pip install does various things depending on various factors. From where are you installing the package? From PyPI or some other package index? How was the package distributed? Is it a binary dist (.whl) or a source dist (.gz) file? Are you installing the package via a local directory? A remote repo via a VCS URL? Pip does not necessarily use the same approach for each of these cases. I would recommend using the -vvv flag to see what exactly pip is doing. It may not be running setuptools's install command for whatever reason...do you have

     packages=setuptools.find_packages(),
     include_package_data=True
    

in your setup.py file? Without these lines, pip could be installing your package's metadata but not the package itself.

MatTheWhale
  • 967
  • 6
  • 16