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

ModuleNotFoundError with multiprocessing and import-mode=importlib #12178

Open
jaraco opened this issue Apr 2, 2024 · 1 comment
Open

ModuleNotFoundError with multiprocessing and import-mode=importlib #12178

jaraco opened this issue Apr 2, 2024 · 1 comment
Labels
topic: collection related to the collection phase

Comments

@jaraco
Copy link
Contributor

jaraco commented Apr 2, 2024

In jaraco/keyring#673, I captured an issue stemming from the introduction of --import-mode importlib to jaraco/skeleton projects. In jaraco/keyring@6ff02e0, I'm attempting to enable importlib mode for all projects in order to support projects that use namespaces and those that don't in a uniform way. However, enabling importlib mode causes failures in tests that involve multiprocessing. Consider this minimal example:

 @ cat > tox.ini
[testenv]
deps =
	pytest
commands =
	pytest {posargs:--import-mode=importlib}
 @ mkdir tests
 @ cat > tests/test_multiprocess.py
import multiprocessing


def multiprocess_do():
    pass


def test_multiprocess():
    proc1 = multiprocessing.Process(target=multiprocess_do)
    proc1.start()
    proc1.join()
    assert proc1.exitcode == 0
 @ tree
.
├── tests
│   └── test_multiprocess.py
└── tox.ini

2 directories, 2 files
 @ tox
py: commands[0]> pytest --import-mode=importlib
============================= test session starts ==============================
platform darwin -- Python 3.12.2, pytest-8.1.1, pluggy-1.4.0
cachedir: .tox/py/.pytest_cache
rootdir: /Users/jaraco/draft
collected 1 item

tests/test_multiprocess.py F                                             [100%]

=================================== FAILURES ===================================
______________________________ test_multiprocess _______________________________

    def test_multiprocess():
        proc1 = multiprocessing.Process(target=multiprocess_do)
        proc1.start()
        proc1.join()
>       assert proc1.exitcode == 0
E       AssertionError: assert 1 == 0
E        +  where 1 = <Process name='Process-1' pid=38572 parent=38570 stopped exitcode=1>.exitcode

tests/test_multiprocess.py:12: AssertionError
----------------------------- Captured stderr call -----------------------------
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/opt/homebrew/Cellar/[email protected]/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/multiprocessing/spawn.py", line 122, in spawn_main
    exitcode = _main(fd, parent_sentinel)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/multiprocessing/spawn.py", line 132, in _main
    self = reduction.pickle.load(from_parent)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'tests'
=========================== short test summary info ============================
FAILED tests/test_multiprocess.py::test_multiprocess - AssertionError: assert...
============================== 1 failed in 0.09s ===============================
py: exit 1 (0.19 seconds) /Users/jaraco/draft> pytest --import-mode=importlib pid=38570
  py: FAIL code 1 (0.22=setup[0.02]+cmd[0.19] seconds)
  evaluation failed :( (0.25 seconds)

The same error occurs even if tests/__init__.py is present. The issue occurs also with pytest 8.0.2 and 7.4.4. My guess is that it never worked with this mode.

It seems that tests.test_multiprocess is in sys.modules and objects passed between the processes might reference that module, so the child process needs to have the same path handling that the parent had but doesn't.

Running tox -- --import-mode prepend or tox -- --import-mode append bypasses the failure.

Can multiprocessing be made to work with import-mode importlib?

@jaraco
Copy link
Contributor Author

jaraco commented Apr 2, 2024

Also, the failure doesn't occur if the tests are invoked with .tox/py/bin/python -m pytest, presumably because that mode adds . to sys.path.

clrpackages pushed a commit to clearlinux-pkgs/pypi-keyring that referenced this issue Apr 5, 2024
…sion 25.1.0

Jason R. Coombs (11):
      Re-enable ignoring of temporary merge queue branches. Closes jaraco/skeleton#103.
      Fetch unshallow clones in readthedocs. Closes jaraco/skeleton#114.
      Move Python 3.11 out of the test matrix.
      Configure pytest to support namespace packages. Ref pytest-dev/pytest#12112.
      Replace ExceptionRaisedContext with ExceptionTrap.
      Remove test skip, no longer relevant with Python 3.8 minimum.
      Inject the current directory to sys.path in multiprocessing tests. Closes #673. Ref pytest-dev/pytest#12178.
      Move Python 3.8 compatibility logic into the compat package.
      When completion is unavailable, exit with non-zero status and emit message to stderr.
      Finalize
      Finalize
@Zac-HD Zac-HD added the topic: collection related to the collection phase label Apr 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: collection related to the collection phase
Projects
None yet
Development

No branches or pull requests

2 participants