Install & Compatibility
Where this runs
tested against v2024.7.31 · 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.052s · 17.9MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.6s · import 0.050s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
pyc
✓ from pycnite import pyc
The primary module for .pyc file parsing.
bytecode
✓ from pycnite import bytecode
Contains utilities for bytecode manipulation and representation.
This quickstart demonstrates how to programmatically compile a Python source file into a .pyc file using the standard `py_compile` module, and then use `pycnite` to load and inspect the compiled bytecode. This allows for programmatic analysis of Python's low-level execution instructions and metadata contained within .pyc files. Ensure the `dummy_pyc_path` matches the actual filename convention for your Python version (e.g., `cpython-310.pyc` for Python 3.10).
import os
import py_compile
import tempfile
# Create a dummy Python file
dummy_py_content = '''
def greet(name):
message = f"Hello, {name}!"
return message
result = greet("World")
'''
# Use tempfile to create and manage temporary files
with tempfile.TemporaryDirectory() as tmpdir:
dummy_py_path = os.path.join(tmpdir, 'dummy_module.py')
dummy_pyc_path = os.path.join(tmpdir, '__pycache__', 'dummy_module.cpython-310.pyc') # Adjust for your Python version
with open(dummy_py_path, 'w') as f:
f.write(dummy_py_content)
# Compile the .py file to .pyc
# py_compile automatically creates __pycache__ and the .pyc file
py_compile.compile(dummy_py_path, cfile=dummy_pyc_path, doraise=True)
print(f"Generated .pyc file at: {dummy_pyc_path}")
try:
from pycnite import pyc
# Load the .pyc file using pycnite
# You may need to specify the Python version if pycnite can't infer it
# For example, pyc.load_pyc(dummy_pyc_path, python_version=(3, 10))
pyc_file = pyc.load_pyc(dummy_pyc_path)
print(f"\nLoaded .pyc file successfully. Python version: {pyc_file.version}")
print(f"Magic number: {hex(pyc_file.magic)}")
# Accessing the code object (example for the module's code object)
code_obj = pyc_file.code
print(f"\nCode object name: {code_obj.co_name}")
print(f"Number of arguments: {code_obj.co_argcount}")
print(f"Constants: {code_obj.co_consts}")
print(f"Names used: {code_obj.co_names}")
# print(f"Disassembled bytecode:\n{code_obj.dis()}") # Requires 'dis' module and pycnite's dis method
except ImportError:
print("\nPycnite not installed. Skipping pycnite specific checks.")
except Exception as e:
print(f"\nError loading .pyc with pycnite: {e}")
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pycnite'
The `pycnite` library is not installed in your current Python environment or the Python interpreter cannot locate it.
fixEnsure `pycnite` is installed using pip: `pip install pycnite`
ValueError: Unsupported Python bytecode version X.Y
Pycnite was used to parse a `.pyc` file compiled for a Python version (X.Y) that it does not currently support. Pycnite supports Python versions 3.8 through 3.12.
fixUse a `.pyc` file compiled for a supported Python version (3.8-3.12) or ensure your `pycnite` library is up-to-date if a newer Python version has been added. You can check the `.pyc` file's Python version by inspecting its magic number (the first few bytes of the file).
pycnite.errors.InvalidBytecodeError: Malformed .pyc file (e.g., unexpected EOF or invalid header)
The provided file is either not a valid Python `.pyc` file, is corrupted, or does not conform to the expected bytecode structure for any supported Python version. The library encountered an unrecoverable error during parsing due to invalid data.
fixEnsure the `.pyc` file is valid and uncorrupted. If possible, try recompiling the original `.py` source file to generate a new `.pyc`. Verify the file path and read permissions.
AttributeError: 'module' object has no attribute 'parse_pyc'
This error occurs when attempting to call a method or access an attribute that does not exist directly on the `pycnite` module, or when using an outdated/incorrect API call.
fixConsult the `pycnite` documentation to ensure the correct module structure and method names are being used for parsing. For example, you might need to import a specific parser class from `pycnite` instead of calling a method directly on the top-level `pycnite` module.
Upgrade
Version history
2024.7.31latest on PyPI · released Jul 31, 2024
Audit
Dependencies
No dependency data recorded yet.