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=
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!
Most helpful comment
IIUC, I believe the scenario would be more like:
Which makes sense that
pip-toolsfails. This construct tells Python to only run setuptools ifsetup.pyis executed as the main script, but pip-tools doesn't do that. pip-tools importssetup.pyat runtime from within a different main. So, IMO, theif __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.