{"id":559,"library":"py","title":"py library","description":"The 'py' library provides a collection of powerful, low-level utilities primarily focused on filesystem path manipulation, INI parsing, I/O, Python code introspection, and logging. Maintained by the pytest-dev team, it often serves as a foundational component for other testing and development tools. It is currently at version 1.11.0 and has a stable release cadence.","status":"active","version":"1.11.0","language":"python","source_language":"en","source_url":"https://github.com/pytest-dev/py/","tags":["utility","filesystem","path","code introspection","logging","xml","pytest"],"install":[{"cmd":"pip install py","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"Accessing the path utilities.","symbol":"path","correct":"import py.path; p = py.path.local('.')"},{"note":"Accessing code introspection utilities.","symbol":"code","correct":"import py.code; c = py.code.Source('print(\"hello\")')"},{"note":"Accessing I/O capturing utilities.","symbol":"io","correct":"import py.io; cap = py.io.StdCapture()"},{"note":"Accessing logging utilities.","symbol":"log","correct":"import py.log; py.log.setconsumer(my_consumer)"}],"quickstart":{"code":"import py\nimport os\n\n# Create a temporary local path object\ntemppath = py.path.local('my_temp_dir')\ntemppath.ensure(dir=True) # Ensure it's a directory\n\n# Join paths and write content\nfilepath = temppath.join('hello.txt')\nfilepath.write('Hello, py world!')\n\n# Read content\nprint(filepath.read())\n\n# Cleanup\nfilepath.remove()\ntemppath.remove()","lang":"python","description":"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."},"warnings":[{"fix":"Consider 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.","message":"For 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.","severity":"gotcha","affected_versions":"All versions of 'py' when used with Python 3.4+"},{"fix":"Evaluate whether Python's standard `logging` module meets your requirements for application-wide logging before opting for `py.log` for general use cases.","message":"The `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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For common code introspection needs, explore Python's standard `inspect` module. For code modification or static analysis, consider the `ast` module.","message":"The `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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-05-12T15:07:48.190Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"Install the 'py' library using pip: `pip install py`","cause":"This error occurs when the 'py' library is not installed in the current Python environment or Python cannot find it in the configured paths.","error":"ModuleNotFoundError: No module named 'py'"},{"fix":"If 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.","cause":"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.","error":"AttributeError: module 'py' has no attribute 'path'"},{"fix":"Ensure 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.","cause":"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.","error":"OSError: [Errno 2] ENOENT"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":80,"quickstart_tag":"verified","pypi_latest":null,"install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.04,"mem_mb":1.2,"disk_size":"18.6M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.03,"mem_mb":1.2,"disk_size":"19M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.05,"mem_mb":0.8,"disk_size":"20.6M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":0.8,"disk_size":"21M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.6,"disk_size":"12.5M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.02,"mem_mb":0.6,"disk_size":"13M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"12.1M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.5,"disk_size":"13M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.04,"mem_mb":1.2,"disk_size":"18.1M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.03,"mem_mb":1.2,"disk_size":"19M"}]},"quickstart_checks":{"last_tested":"2026-04-23","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}