Pip-tools: pip-compile fails if setup.py contains the if __name__ == __main__

Created on 20 Dec 2020  路  5Comments  路  Source: jazzband/pip-tools

pip-compile will display an tracedump if setup.py contains the code such:

if __name__ == "__main__":
    ...

That may be problematic as this construct is quite popular: https://github.com/search?q=%22if+__name__%22+filename%3Asetup.py+path%3A%2F&type=Code&ref=advsearch&l=&l=

bug setuptools

Most helpful comment

IIUC, I believe the scenario would be more like:

# setup.py

from setuptools import setup

if __name__ == "__main__":
    setup(name='foo', version='0.1', install_requires=['six'])

Which makes sense that pip-tools fails. This construct tells Python to only run setuptools if setup.py is executed as the main script, but pip-tools doesn't do that. pip-tools imports setup.py at runtime from within a different main. So, IMO, the if __name__ == "__main__": should be dropped from user code.

Perhaps it would make sense to document this gotcha, but I don't think a code change in pip-tools is required.

All 5 comments

Couldn't reproduce it locally:

# setup.py

from setuptools import setup

if __name__ == "__main__":
    print('main')

setup(name='foo', version='0.1', install_requires=['six'])
$ pip-compile setup.py
#
# This file is autogenerated by pip-compile
# To update, run:
#
#    pip-compile setup.py
#
six==1.15.0
    # via foo (setup.py)

IIUC, I believe the scenario would be more like:

# setup.py

from setuptools import setup

if __name__ == "__main__":
    setup(name='foo', version='0.1', install_requires=['six'])

Which makes sense that pip-tools fails. This construct tells Python to only run setuptools if setup.py is executed as the main script, but pip-tools doesn't do that. pip-tools imports setup.py at runtime from within a different main. So, IMO, the if __name__ == "__main__": should be dropped from user code.

Perhaps it would make sense to document this gotcha, but I don't think a code change in pip-tools is required.

Considering that the if __name__ == "__main__" idiom was used and advertised extensively, likely even used by more than 50% of setup.py ever published, it would be highly desirable to find a trick to make it work, if even possible.

if not possible, pip-tools should fail with a clear message that includes a hint that explains that removing the construct should make it work.

Agreed, asking the users to dump the best practice sounds wrong. As for the trick, it should be possible to do the same sort of eval setuptools do.

Also, there's https://github.com/jaraco/jaraco.packaging that may have some useful recipes.

Fixed in #1311. Thanks for the issue!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

touilleMan picture touilleMan  路  4Comments

astrojuanlu picture astrojuanlu  路  3Comments

tuukkamustonen picture tuukkamustonen  路  5Comments

JulienPalard picture JulienPalard  路  3Comments

sirex picture sirex  路  4Comments