Skip to content

Cannot import file from fakefs #1078

Description

@Aran-Fey

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions