{"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,"search_vec":"'1.11.0':54 'cadenc':60 'code':25,64 'collect':8 'compon':42 'current':51 'dev':34 'develop':47 'filesystem':18,62 'focus':16 'foundat':41 'i/o':23 'ini':21 'introspect':26,65 'level':13 'librari':2,5 'log':28,66 'low':12 'low-level':11 'maintain':29 'manipul':20 'often':37 'pars':22 'path':19,63 'power':10 'primarili':15 'provid':6 'py':1,4 'pytest':33,68 'pytest-dev':32 'python':24 'releas':59 'serv':38 'stabl':58 'team':35 'test':45 'tool':48 'util':14,61 'version':53 'xml':67","created_at":"2026-03-28T15:18:51.258228+00:00","updated_at":"2026-04-16T18:16:11.823667+00:00","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,"quickstart_score":80,"quickstart_tag":"verified","pypi_latest":"1.11.0","cli_name":"py","cli_version":"sh: 1: py: not found","type":"library","homepage":"https://py.readthedocs.io/","github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/py/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing","serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-27","next_check":"2026-07-28","install_tag":"verified"}}