PyCIFRW
raw JSON → 5.0.1 verified Sat May 09 auth: no python
PyCIFRW provides support for reading and writing CIF (Crystallographic Information File) and STAR files in Python. Version 5.0.1 is the latest stable release, with a focus on bugfixes and dropping Python 2 support. The library is maintained with occasional releases.
pip install pycifrw Common errors
error ModuleNotFoundError: No module named 'pycifrw' ↓
cause Trying to `import pycifrw` or `from pycifrw import ...`. The top-level module is not 'pycifrw'—the actual modules are CifFile, StarFile, etc.
fix
Install with
pip install pycifrw but import from 'CifFile' (e.g., from CifFile import ReadCif). error SyntaxError: invalid syntax (for print statements, etc.) ↓
cause Running Python 2 code on Python 3. PyCIFRW 5+ requires Python 3.
fix
Ensure Python 3 interpreter is used (python3). If stuck on Python 2, install pycifrw==4.4.6.
error AttributeError: 'CifFile' object has no attribute 'keys' ↓
cause Reading a CIF file with errors; the object may be incomplete. Or using wrong method to access data.
fix
Use
ReadCifWithError to get partial results, or check the CIF syntax. Access data via ast.first_loop()['_tag'] or convert to dict: dict(ast.items()). Warnings
breaking Version 5.0 dropped Python 2 support entirely. Code written for Python 2 will not run. ↓
fix Upgrade to Python 3, or pin to pycifrw==4.4.6 if stuck on Python 2.
deprecated The function `ReadCif` still works but `ReadCifWithError` is recommended for more robust parsing. ↓
fix Use `from CifFile import ReadCifWithError` and handle returned parse info.
gotcha Importing from `pycifrw` directly fails. The correct import path is `CifFile`, `StarFile`, etc. ↓
fix Use `from CifFile import ReadCif` instead of `from pycifrw import ReadCif`.
Imports
- ReadCif wrong
from pycifrw import ReadCifcorrectfrom CifFile import ReadCif - CifFile wrong
from pycifrw import CifFilecorrectfrom CifFile import CifFile - StarFile
from StarFile import StarFile
Quickstart
from CifFile import ReadCif
# Replace with an actual CIF file path or content
cif_content = """data_test
_audit_creation_method 'manually created'
"""
with open('test.cif', 'w') as f:
f.write(cif_content)
ast = ReadCif('test.cif')
print(ast.keys())
# Clean up
import os
os.remove('test.cif')