pex-tool / pex

A tool for generating .pex (Python EXecutable) files, lock files and venvs.
https://docs.pex-tool.org/
Apache License 2.0
2.53k stars 258 forks source link

Trouble using pkg_resources in setup.py #160

Closed chris-martin closed 1 month ago

chris-martin commented 9 years ago

I set up a simple example project that uses pkg_resources.resource_string to read from a file in its setup.py to set the version number.

  1. I've run python setup.py sdist to verify that the setup.py works.
  2. I would expect to be able to run pex . --no-wheel and have that work as well. Instead I get:
**** Failed to install demo-0.0.1. stdout:

**** Failed to install demo-0.0.1. stderr:
Traceback (most recent call last):
  File "<stdin>", line 6, in <module>
  File "setup.py", line 7, in <module>
    version=get_version(),
  File "./demo/version.py", line 5, in get_version
    return resource_string('demo.version', 'version.txt').decode('utf-8').strip()
  File "/usr/lib/python3.3/site-packages/pkg_resources.py", line 966, in resource_string
    self, resource_name
  File "/usr/lib/python3.3/site-packages/pkg_resources.py", line 1393, in get_resource_string
    return self._get(self._fn(self.module_path, resource_name))
  File "/usr/lib/python3.3/site-packages/pkg_resources.py", line 1514, in _get
    with open(path, 'rb') as stream:
FileNotFoundError: [Errno 2] No such file or directory: './demo/version.txt'

Traceback (most recent call last):
  File "/bin/pex", line 9, in <module>
    load_entry_point('pex==1.0.3', 'console_scripts', 'pex')()
  File "/usr/lib/python3.3/site-packages/pex/bin/pex.py", line 509, in main
    pex_builder = build_pex(reqs, options, resolver_options_builder)
  File "/usr/lib/python3.3/site-packages/pex/bin/pex.py", line 471, in build_pex
    resolveds = resolver.resolve(resolvables)
  File "/usr/lib/python3.3/site-packages/pex/resolver.py", line 191, in resolve
    dist = self.build(package, resolvable.options)
  File "/usr/lib/python3.3/site-packages/pex/resolver.py", line 248, in build
    dist = super(CachingResolver, self).build(package, options)
  File "/usr/lib/python3.3/site-packages/pex/resolver.py", line 160, in build
    raise Untranslateable('Package %s is not translateable by %s' % (package, translator))
pex.resolver.Untranslateable: Package SourcePackage('file:///tmp/tmpbfj8z4/demo-0.0.1.tar.gz') is not translateable by ChainedTranslator(EggTranslator, SourceTranslator)

Oddly, it seems to have worked a little bit, because it did end up pulling the version number 0.0.1 out of the file at some point.

I've always been rather fuzzy on exactly how python packaging works, so apologies if this is a foolish or non-pex-related question.

jsirois commented 1 month ago

@chris-martin it's been a long time, but I'm doing an old issue sweep and saw this. With the fix to your project here: https://github.com/jsirois/pex-resources-demo/commit/c7805b46066bb05c1c7f521cbd42af2fb1c60880 modern Pex works fine:

:; pex demo@git+https://github.com/jsirois/pex-resources-demo setuptools -odemo.pex
:; ./demo.pex -c 'from demo.version import get_version; print(get_version())'
0.0.1

Let me know if you have further questions. The packaging world has certainly moved on as has the stdlib with pkgutil (which was always an alternative to pkg_resources) -> importlib.resources.