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

cwd on sys.path mask stdlib modules #11

Open
jaraco opened this issue Aug 26, 2024 · 2 comments
Open

cwd on sys.path mask stdlib modules #11

jaraco opened this issue Aug 26, 2024 · 2 comments

Comments

@jaraco
Copy link
Member

jaraco commented Aug 26, 2024

While experimenting with adding distutils to the coherent system, I ran into an ImportError.

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/jaraco/Library/Application Support/pipx/venvs/coherent-cli/lib/python3.12/site-packages/pytest/__main__.py", line 9, in <module>
    raise SystemExit(pytest.console_main())
                     ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jaraco/Library/Application Support/pipx/venvs/coherent-cli/lib/python3.12/site-packages/_pytest/config/__init__.py", line 201, in console_main
    code = main()
           ^^^^^^
  File "/Users/jaraco/Library/Application Support/pipx/venvs/coherent-cli/lib/python3.12/site-packages/_pytest/config/__init__.py", line 156, in main
    config = _prepareconfig(args, plugins)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jaraco/Library/Application Support/pipx/venvs/coherent-cli/lib/python3.12/site-packages/_pytest/config/__init__.py", line 341, in _prepareconfig
    config = pluginmanager.hook.pytest_cmdline_parse(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jaraco/Library/Application Support/pipx/venvs/coherent-cli/lib/python3.12/site-packages/pluggy/_hooks.py", line 513, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jaraco/Library/Application Support/pipx/venvs/coherent-cli/lib/python3.12/site-packages/pluggy/_manager.py", line 120, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jaraco/Library/Application Support/pipx/venvs/coherent-cli/lib/python3.12/site-packages/pluggy/_callers.py", line 139, in _multicall
    raise exception.with_traceback(exception.__traceback__)
  File "/Users/jaraco/Library/Application Support/pipx/venvs/coherent-cli/lib/python3.12/site-packages/pluggy/_callers.py", line 122, in _multicall
    teardown.throw(exception)  # type: ignore[union-attr]
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jaraco/Library/Application Support/pipx/venvs/coherent-cli/lib/python3.12/site-packages/_pytest/helpconfig.py", line 105, in pytest_cmdline_parse
    config = yield
             ^^^^^
  File "/Users/jaraco/Library/Application Support/pipx/venvs/coherent-cli/lib/python3.12/site-packages/pluggy/_callers.py", line 103, in _multicall
    res = hook_impl.function(*args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jaraco/Library/Application Support/pipx/venvs/coherent-cli/lib/python3.12/site-packages/_pytest/config/__init__.py", line 1140, in pytest_cmdline_parse
    self.parse(args)
  File "/Users/jaraco/Library/Application Support/pipx/venvs/coherent-cli/lib/python3.12/site-packages/_pytest/config/__init__.py", line 1490, in parse
    self._preparse(args, addopts=addopts)
  File "/Users/jaraco/Library/Application Support/pipx/venvs/coherent-cli/lib/python3.12/site-packages/_pytest/config/__init__.py", line 1377, in _preparse
    self.pluginmanager.load_setuptools_entrypoints("pytest11")
  File "/Users/jaraco/Library/Application Support/pipx/venvs/coherent-cli/lib/python3.12/site-packages/pluggy/_manager.py", line 421, in load_setuptools_entrypoints
    plugin = ep.load()
             ^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.12.5/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/metadata/__init__.py", line 205, in load
    module = import_module(match.group('module'))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.12.5/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/Users/jaraco/Library/Application Support/pipx/venvs/coherent-cli/lib/python3.12/site-packages/coherent/test/monkey.py", line 8, in <module>
    from coherent.build import discovery
  File "/Users/jaraco/Library/Application Support/pipx/venvs/coherent-cli/lib/python3.12/site-packages/coherent/build/__init__.py", line 17, in <module>
    from .backend import (
  File "/Users/jaraco/Library/Application Support/pipx/venvs/coherent-cli/lib/python3.12/site-packages/coherent/build/backend.py", line 20, in <module>
    from .metadata import Message
  File "/Users/jaraco/Library/Application Support/pipx/venvs/coherent-cli/lib/python3.12/site-packages/coherent/build/metadata.py", line 17, in <module>
    from . import discovery
  File "/Users/jaraco/Library/Application Support/pipx/venvs/coherent-cli/lib/python3.12/site-packages/coherent/build/discovery.py", line 17, in <module>
    import packaging.requirements
  File "/Users/jaraco/.local/pip-run/packaging/requirements.py", line 8, in <module>
    from ._parser import parse_requirement as _parse_requirement
  File "/Users/jaraco/.local/pip-run/packaging/_parser.py", line 12, in <module>
    from ._tokenizer import DEFAULT_RULES, Tokenizer
  File "/Users/jaraco/.local/pip-run/packaging/_tokenizer.py", line 8, in <module>
    from .specifiers import Specifier
  File "/Users/jaraco/.local/pip-run/packaging/specifiers.py", line 18, in <module>
    from .utils import canonicalize_version
  File "/Users/jaraco/.local/pip-run/packaging/utils.py", line 10, in <module>
    from .tags import Tag, parse_tag
  File "/Users/jaraco/.local/pip-run/packaging/tags.py", line 13, in <module>
    import sysconfig
  File "/Users/jaraco/code/pypa/distutils/sysconfig.py", line 19, in <module>
    from ._functools import pass_none
ImportError: attempted relative import with no known parent package

Because distutils exposes a sysconfig module and because that name masks one in the stdlib, it causes problems. Probably the test runner should make sure . isn't on sys.path.

@jaraco
Copy link
Member Author

jaraco commented Aug 26, 2024

On Python 3.11 and later, -P/PYTHONSAFEPATH can be used. On older Pythons, probably a sitecustomize or some other more involved intervention would be required.

@jaraco jaraco changed the title cwd on sys.path causes problems cwd on sys.path mask stdlib modules Aug 26, 2024
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

2 participants
@jaraco and others