Install & Compatibility
Where this runs
tested against v1.11.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
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.018s · 18.6MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.7s · import 0.016s · 19MB
17MB installed
● package 17MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
path
✓ import py.path; p = py.path.local('.')
Accessing the path utilities.
code
✓ import py.code; c = py.code.Source('print("hello")')
Accessing code introspection utilities.
io
✓ import py.io; cap = py.io.StdCapture()
Accessing I/O capturing utilities.
log
✓ import py.log; py.log.setconsumer(my_consumer)
Accessing logging utilities.
This quickstart demonstrates basic file system operations using `py.path.local`, including creating a directory, writing to a file, reading from it, and cleaning up. This highlights the object-oriented approach to path manipulation provided by the `py` library.
import py
import os
# Create a temporary local path object
temppath = py.path.local('my_temp_dir')
temppath.ensure(dir=True) # Ensure it's a directory
# Join paths and write content
filepath = temppath.join('hello.txt')
filepath.write('Hello, py world!')
# Read content
print(filepath.read())
# Cleanup
filepath.remove()
temppath.remove()
py --version
Debug
Known issues
gotchaFor new projects or general file system path manipulation, Python's built-in `pathlib` module (introduced in Python 3.4) is often the more idiomatic and recommended choice over `py.path`. `pathlib` provides a fully object-oriented interface for paths and is part of the standard library.fixConsider using `from pathlib import Path` and its methods (`Path.cwd()`, `Path.joinpath()`, `Path.read_text()`, etc.) for modern Python development. `py.path` remains useful for compatibility with projects built around it (e.g., `pytest`) or for its specific utility functions.
affects: All versions of 'py' when used with Python 3.4+
gotchaThe `py.log` module provides basic logging facilities. However, for robust application logging, structured logging, or integration with existing logging ecosystems, Python's standard `logging` module is generally more powerful and flexible, offering advanced features like handlers, formatters, and hierarchical loggers.fixEvaluate whether Python's standard `logging` module meets your requirements for application-wide logging before opting for `py.log` for general use cases.
affects: All versions
gotchaThe `py.code` module offers powerful introspection and manipulation of Python code objects, often utilized by testing frameworks like `pytest`. Directly relying on `py.code` for general-purpose introspection or metaprogramming in application code might lead to less standard or harder-to-maintain solutions. Python's `inspect` module or `ast` module typically provide more standard APIs for these tasks.fixFor common code introspection needs, explore Python's standard `inspect` module. For code modification or static analysis, consider the `ast` module.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'py'
This error occurs when the 'py' library is not installed in the current Python environment or Python cannot find it in the configured paths.
fixInstall the 'py' library using pip: `pip install py`
AttributeError: module 'py' has no attribute 'path'
This error often arises in `pytest` contexts when trying to use `py.path.local`, which is a legacy fixture. Modern `pytest` versions encourage the use of `tmp_path` which returns a `pathlib.Path` object instead. It can also be caused by a local file or directory named 'py' shadowing the installed library.
fixIf using `pytest`, replace `tmpdir` (which returns `py.path.local`) with `tmp_path` (which returns `pathlib.Path`). For example, change `tmpdir.join('file.txt')` to `tmp_path / 'file.txt'`. If it's a shadowing issue, rename any local 'py.py' files or 'py/' directories. OSError: [Errno 2] ENOENT
This 'Error NO ENTry' signifies that a file or directory path provided to a 'py.path.local' operation (or similar filesystem interaction) does not exist or cannot be found at the specified location.
fixEnsure that the file or directory path you are trying to access or create actually exists and is correctly specified. Use methods like `py.path.local(path).check()` to verify existence before operations, or ensure parent directories are created with `ensure=True` if writing a file or `mkdir()` if creating a directory.
Upgrade
Version history
1.11.0latest on PyPI · released Nov 4, 2021
Audit
Dependencies
No dependency data recorded yet.