#4298 Don't use __file__ in setup.py
Closed 10 months ago by tkopecek. Opened 10 months ago by tkopecek.
tkopecek/koji setuptools-file-fix  into  master

file modified
+2 -5
@@ -1,7 +1,6 @@ 

  #!/usr/bin/env python

  from __future__ import absolute_import

  

- import os.path

  from setuptools import setup

  

  
@@ -24,15 +23,13 @@ 

  

  

  def get_version():

-     cwd = os.path.dirname(__file__)

      lcls = {}

-     exec(open(os.path.join(cwd, 'koji/_version.py'), 'rt').read(), None, lcls)

+     exec(open('koji/_version.py', 'rt').read(), None, lcls)

      return lcls['__version__']

  

  

  def get_long_description():

-     cwd = os.path.dirname(__file__)

-     return open(os.path.join(cwd, "README.md"), "rt").read()

+     return open("README.md", "rt").read()

  

  

  setup(

When setuptools are run from other tooling (e.g. pip install git...),
the file would point to different location. Using current directory
seems to be safer.

Reproducer:

$ python3.11 -m venv venv
$ . ./venv/bin/activate
$ pip  install git+https://pagure.io/koji.git@koji-1.34.0

Metadata Update from @mfilip:
- Pull-request tagged with: testing-ready

10 months ago

This swaps one issue for another. Now the code will break if cwd is not the checkout.

I'm not convinced the problem is __file__, or at least not just that.

When I run the replication commands, this is the error I got:

  File "/tmp/pip-build-env-thcl182y/overlay/lib/python3.13/site-packages/setuptools/build_meta.py", line 304, in _get_build_requires
    self.run_setup()
    ~~~~~~~~~~~~~~^^
  File "/tmp/pip-build-env-thcl182y/overlay/lib/python3.13/site-packages/setuptools/build_meta.py", line 522, in run_setup
    super().run_setup(setup_script=setup_script)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/pip-build-env-thcl182y/overlay/lib/python3.13/site-packages/setuptools/build_meta.py", line 320, in run_setup
    exec(code, locals())
    ~~~~^^^^^^^^^^^^^^^^
  File "<string>", line 39, in <module>
  File "<string>", line 29, in get_version
KeyError: '__version__'

It's notable that this fails on the keyerror, but does not fail on the exec immediately prior. This says to me the pip run is finding koji/_version.py, but somehow isn't getting __version__ out of it.

If we can rely on setup.py having access to the correct git checkout, then __file__ should work. If we can't rely on that, the cwd is likely just as invalid

Ouch, nevermind, it is fixed in master by PR #4100

Metadata Update from @tkopecek:
- Pull-request untagged with: testing-ready

10 months ago

Pull-Request has been closed by tkopecek

10 months ago
Metadata