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

Importing DistributionNotFound in a python 3.9 script causes AttributeError #2548

Closed
OJFord opened this issue Jan 31, 2021 · 4 comments
Closed

Comments

@OJFord
Copy link

OJFord commented Jan 31, 2021

Environment

  • pip version: 21.0.1
  • Python version: 3.9.1
  • OS: Darwin Kernel Version 20.2.0

Description

from pkg_resources import DistributionNotFound in a script causes and AttributeError, but works fine in an interpreter, (I assume it just doesn't hit this code path where it tries to find the module name) in both cases using the same python v3.9.1.

For context, pikepdf does this on import, so neither it nor anything which imports it (e.g. ocrmypdf) can be imported in a script or module.

Expected behavior
No error, imports.

How to Reproduce
Create a file:

#!/usr/bin/env python3.9
from pkg_resources import DistributionNotFound

and execute it.

Output

    from pkg_resources import DistributionNotFound
  File "/usr/local/lib/python3.9/site-packages/pkg_resources/__init__.py", line 3243, in <module>
    def _initialize_master_working_set():
  File "/usr/local/lib/python3.9/site-packages/pkg_resources/__init__.py", line 3226, in _call_aside
    f(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/pkg_resources/__init__.py", line 3268, in _initialize_master_working_set
    tuple(
  File "/usr/local/lib/python3.9/site-packages/pkg_resources/__init__.py", line 3269, in <genexpr>
    dist.activate(replace=False)
  File "/usr/local/lib/python3.9/site-packages/pkg_resources/__init__.py", line 2783, in activate
    declare_namespace(pkg)
  File "/usr/local/lib/python3.9/site-packages/pkg_resources/__init__.py", line 2282, in declare_namespace
    _handle_ns(packageName, path_item)
  File "/usr/local/lib/python3.9/site-packages/pkg_resources/__init__.py", line 2199, in _handle_ns
    loader = importer.find_module(packageName)
  File "<frozen importlib._bootstrap_external>", line 493, in _find_module_shim
  File "<frozen importlib._bootstrap_external>", line 1428, in find_loader
  File "<frozen importlib._bootstrap_external>", line 1473, in find_spec
  File "<frozen importlib._bootstrap_external>", line 64, in _path_join
  File "<frozen importlib._bootstrap_external>", line 64, in <listcomp>
AttributeError: 'PosixPath' object has no attribute 'rstrip'
@jaraco
Copy link
Member

jaraco commented Feb 1, 2021

I'm unable to elicit the behavior you describe with the repro. What other factors are present in your environment that might be contributing to the failure:

~ $ cd draft
cd: no such file or directory: draft
~ $ mkdir draft
~ $ cd draft
draft $ cat > script
#!/usr/bin/env python3.9
from pkg_resources import DistributionNotFound
draft $ chmod u+x script
draft $ ./script
Traceback (most recent call last):
  File "/Users/jaraco/draft/./script", line 2, in <module>
    from pkg_resources import DistributionNotFound
ModuleNotFoundError: No module named 'pkg_resources'
draft $ pip install setuptools
Collecting setuptools
  Using cached setuptools-52.0.0-py3-none-any.whl (784 kB)
Installing collected packages: setuptools
Successfully installed setuptools-52.0.0
draft $ ./script
draft $

@OJFord
Copy link
Author

OJFord commented Feb 1, 2021

Ah! Apologies, it was my fault - I put a Path in sys.path. (Which for the purposes just of importing, works, but the docs do say only that it should be strings, so mea culpa.)

Cheers.

@OJFord OJFord closed this as completed Feb 1, 2021
@yyunhh
Copy link

yyunhh commented Oct 19, 2021

@OJFord Hello, I face the same issue here, how do you fix it?

    def _zipinfo_name(self, fspath):
        # Convert a virtual filename (full path to file) into a zipfile subpath
        # usable with the zipimport directory cache for our target archive
        while fspath.endswith(os.sep):
            fspath = fspath[:-1]
        #fspath = os.fspath(path) #fspath = fspath.rstrip(os.sep)
        if fspath == self.loader.archive:
            return ''
        if fspath.startswith(self.zip_pre):
            return fspath[len(self.zip_pre):]
        raise AssertionError(
            "%s is not a subpath of %s" % (fspath, self.zip_pre)
        )

Thanks in advance!

@vdmitriyev
Copy link

@yyunhh Thank for your contribution! Unfortunately, suggested by you approach didn't work for me. However, using the latest Python version, which was available to me (Python 3.8.10) solved this particular problem.

pjbollinger added a commit to pjbollinger/remotestorage.js that referenced this issue Jan 20, 2022
This resolves remotestorage#1262.

Per this comment pypa/setuptools#2548 (comment) and the Python docs https://docs.python.org/3/library/sys.html#sys.path, `sys.path` should only contain strings and not `Path` objects. Since we do not have dependencies frozen, a recent update to Sphinx broke the documentation build.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants