Registry / testing / pyfakefs

pyfakefs

JSON →
library6.2.0pypypi✓ verified 27d ago

pyfakefs implements a fake file system that mocks the Python file system modules. It allows tests to operate on an in-memory file system without touching the real disk, requiring no modification to the software under test. pyfakefs supports pytest, unittest, and can be used as a context manager. It is actively maintained with frequent releases to support new Python versions and fix bugs.

pip install pyfakefs
INSTALL
IMPORT
SIG · PYFAKEFS
P
pyfakefs
testingpythonv6.2.0
Install
1.7s avg
Import
277ms
Disk
19MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v6.2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.300s · 20.3MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.7s · import 0.254s · 21MB
19MB installed
● package 19MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fake_filesystem
from pyfakefs import fake_filesystem
import pytest

This example demonstrates basic file operations using the `fs` pytest fixture, which automatically sets up and tears down an in-memory fake filesystem for your test. Simply run `pytest your_test_file.py`.

import os import pytest def test_file_operations(fs): # The 'fs' fixture provides a fake file system # All os.path and file operations will use this fake system assert not os.path.exists("/test_dir") os.mkdir("/test_dir") assert os.path.exists("/test_dir") file_path = "/test_dir/my_file.txt" with open(file_path, "w") as f: f.write("Hello, pyfakefs!") assert os.path.exists(file_path) with open(file_path, "r") as f: content = f.read() assert content == "Hello, pyfakefs!" os.remove(file_path) assert not os.path.exists(file_path)
Debug
Known issues
breakingpyfakefs version 6.0.0 and above dropped support for Python versions older than 3.10. Ensure your project runs on Python 3.10+ when upgrading.
fix
Upgrade your Python environment to 3.10 or newer, or pin pyfakefs to a version prior to 6.0.0 (e.g., `pyfakefs<6.0.0`).
affects: 6.0.0+
breakingSupport for patching legacy modules `scandir` and `pathlib2` was removed in pyfakefs version 6.0.0. If your tests relied on these, they will break.
fix
Migrate your code to use the standard `os.scandir` and `pathlib.Path` modules, which pyfakefs continues to patch.
affects: 6.0.0+
gotchapyfakefs cannot mock file system access that uses C libraries directly, rather than Python's standard `os`, `io`, or `pathlib` modules. Libraries like `lxml` fall into this category.
fix
Avoid using C-backed libraries for file I/O in code under test with pyfakefs, or patch them separately if possible. Consider using pure Python alternatives where applicable.
affects: All versions
gotchaUsing `unittest.mock.patch` on file system functions concurrently with pyfakefs can lead to incorrect behavior or conflicts, as both attempt to modify the same system modules.
fix
Prefer using pyfakefs's native patching mechanisms (e.g., `fs` fixture, `TestCase` inheritance, `Patcher`) for file system related mocks. Avoid `unittest.mock.patch` for `os`, `io`, or `pathlib` when pyfakefs is active.
affects: All versions
gotchapyfakefs does not retain the Method Resolution Order (MRO) for its fake file objects. This means `isinstance()` checks against original file types (e.g., `io.TextIOBase`) may fail unexpectedly.
fix
Avoid using `isinstance()` checks to differentiate file object types when using pyfakefs. Focus on duck typing or attribute checks if necessary.
affects: All versions
gotchapyfakefs is not guaranteed to work correctly in multi-threading environments, especially concerning concurrent write access to a single file from different threads.
fix
If testing multi-threaded code with file I/O, exercise caution. Consider restructuring tests to isolate file operations or to avoid concurrent access to the same fake files.
affects: All versions
Upgrade
Version history
6.2.0latest on PyPI · released Apr 12, 2026
Audit
Dependencies
pythonrequiredRequires Python 3.10 or newer.
pytestoptionalProvides a convenient 'fs' fixture for pytest users (pytest >= 6.2.5).
Agent activity
16 hits · last 30 days
node
14
Resources
pyfakefs — pip install pyfakefs · libregistry