Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pip-compile fails if setup.py contains the if __name__ == __main__ #1278

Closed
ssbarnea opened this issue Dec 20, 2020 · 5 comments
Closed

pip-compile fails if setup.py contains the if __name__ == __main__ #1278

ssbarnea opened this issue Dec 20, 2020 · 5 comments
Labels
bug Something is not working setuptools Related to compiling requirements form setup.py

Comments

@ssbarnea
Copy link
Member

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=

@atugushev
Copy link
Member

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)

@atugushev atugushev added setuptools Related to compiling requirements form setup.py needs reproduce Need to reproduce an issue bug Something is not working labels Dec 30, 2020
@jdufresne
Copy link
Member

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.

@ssbarnea
Copy link
Member Author

ssbarnea commented Jan 1, 2021

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.

@atugushev atugushev removed the needs reproduce Need to reproduce an issue label Jan 1, 2021
@webknjaz
Copy link
Member

webknjaz commented Jan 2, 2021

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.

@atugushev
Copy link
Member

Fixed in #1311. Thanks for the issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is not working setuptools Related to compiling requirements form setup.py
Projects
None yet
Development

No branches or pull requests

4 participants