pyfakefs
Implements a fake file system that mocks the Python file system modules.
What it is and what it does
pyfakefs is a testing library that replaces the real filesystem with an in-memory fake one during test execution. It automatically mocks Python's file system modules so your tests can create, read, and manipulate files without touching disk. Each test gets a fresh empty filesystem, making tests isolated and fast. The library works with unittest (via TestCase subclass or Patcher context manager) and as a decorator on individual tests.
The package handles cross-platform testing by emulating Linux, Windows, and macOS filesystems, and includes features like mapping real files into the fake filesystem, tracking filesystem size, pausing/resuming patching, and simulating non-root user behavior. It does not work with libraries that use C-level filesystem access, since pyfakefs cannot patch underlying C library calls.
Use it for:
- Test file I/O code (reading, writing, deleting) without creating temporary files on disk
- Verify error handling for missing files or permission issues in a controlled environment
- Run filesystem-heavy test suites in parallel without disk contention or cleanup overhead
- Test cross-platform file path logic (Windows vs. POSIX) on a single machine
- Simulate full filesystem scenarios (large directory trees, disk full conditions) cheaply in memory
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
pyfakefs mocks the Python file system modules to run tests against an in-memory fake filesystem instead of the real disk, with no changes required to the code under test.
Yes. pyfakefs is production-stable (Development Status 5), actively maintained, has no security vulnerabilities, and zero runtime dependencies. It solves a real testing problem—isolating filesystem operations—and is widely used. Install it if your test suite touches the filesystem.
Install
pyfakefs on PyPI
pip
pip install pyfakefsuv
uv add pyfakefspoetry
poetry add pyfakefsInstalling pyfakefs
Before you install
Low friction: pure Python wheel with no runtime dependencies. Actively maintained with recent releases; last commit 2026-08-13. Supports current Python versions on Linux, Windows, and macOS.
License in practice
Apache-2.0 (permissive): you can use, modify, and distribute pyfakefs freely in commercial and private projects, provided you include a copy of the license and state significant changes.
Quickstart
pip install pyfakefs
from pyfakefs import fake_filesystem_unittest
class TestWithFakeFS(fake_filesystem_unittest.TestCase):
def setUp(self):
self.setUpPyfakefs()
def test_file_operations(self):
self.fs.create_file('/tmp/test.txt', contents='hello')
with open('/tmp/test.txt') as f:
self.assertEqual(f.read(), 'hello')
Requires Python 3.10 or later; C-based filesystem libraries cannot be mocked by pyfakefs.
Verify before relying
- Performance characteristics when filesystem size grows or test count scales
- Compatibility with specific C-based libraries beyond the documented limitations
- Whether all test frameworks and plugins work seamlessly with pyfakefs patching
Package facts
| License | Apache-2.0 (permissive) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 124 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 4,466,529/month — #2,298 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pyfakefs-6.2.0-py3-none-any.whl
Keywords: testing, filesystem, mocking, unittest, pytest
Tags
More Libraries packages
urllib3 is an HTTP client library that provides…
permissive · top 100 on PyPI
requestsRequests is a Python HTTP library that…
permissive · top 100 on PyPI
pluggyPluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
python-dateutilProvides parsing, arithmetic, and recurrence…
permissive · top 100 on PyPI
sixSix provides utility functions to write Python…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
mock-openProvides a drop-in replacement for…
permissive · top 15,000 on PyPI
mock-firestoreProvides an in-memory mock of the Google Cloud…
permissive · top 15,000 on PyPI
flexmockflexmock creates mock, stub, spy, and fake…
permissive · top 15,000 on PyPI
testpathTestpath provides utilities for testing code…
permissive · top 5,000 on PyPI
fatfs-ngfatfs-ng wraps ChaN's FatFS library to create,…
permissive · top 5,000 on PyPI
morefsmorefs provides fsspec-based filesystem…
permissive · top 15,000 on PyPI
fakesnowRuns a fake Snowflake database locally for…
permissive · top 5,000 on PyPI
pytest-responsesAutomatically mocks HTTP requests across your…
permissive · top 15,000 on PyPI
kgbkgb intercepts and records function calls in…
permissive · top 5,000 on PyPI
mfusepymfusepy provides Python bindings to libfuse 2…
permissive · top 15,000 on PyPI