pytest-dev / pyfakefs

Provides a fake file system that mocks the Python file system modules.
https://pytest-pyfakefs.readthedocs.io
Apache License 2.0
651 stars 91 forks source link

Cannot import file from fakefs #1078

Closed Aran-Fey closed 1 month ago

Aran-Fey commented 1 month ago

Describe the bug Files that only exist on the fake file system cannot be imported, neither with a regular import statement nor via importlib.

How To Reproduce

import pytest

import importlib.util
import sys
from pathlib import Path

def test_import(fs):
    module_path = Path('heya.py')
    module_path.write_text('x = 1234')

    spec = importlib.util.spec_from_file_location('heya', module_path)
    module = importlib.util.module_from_spec(spec)

    sys.modules['heya'] = module
    spec.loader.exec_module(module)  # Crashes with FileNotFoundError

if __name__ == '__main__':
    pytest.main([__file__])

Your environment

Windows-11-10.0.22631-SP0 Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct 2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] pyfakefs 5.7.1 pytest 7.3.2