EditorConfig Core Python
The editorconfig-core-py library provides a Python implementation for locating and interpreting `.editorconfig` files. It allows applications to retrieve editor configuration properties (like indentation style, tab width, and line endings) for a given file path, ensuring consistent coding styles across different editors and IDEs. The current version is 0.17.1, with releases typically tied to core EditorConfig specification updates or Python compatibility.
Common errors
-
ModuleNotFoundError: No module named 'editorconfig'
cause The `editorconfig` Python package is not installed in the active Python environment or the Python interpreter cannot find it in its search path.fixInstall the package using pip: `pip install editorconfig` -
editorconfig.exceptions.ParsingError: Error parsing an .editorconfig file
cause The `.editorconfig` file encountered by the library contains syntax errors or is malformed, preventing it from being correctly parsed.fixReview the `.editorconfig` file for any syntax mistakes, incorrect property names, or invalid values. Ensure it follows the INI-like format and EditorConfig specifications. -
editorconfig.exceptions.PathError: Invalid filename specified
cause The path provided to `editorconfig.get_properties()` is invalid, inaccessible, or does not exist, preventing the library from locating the file or its `.editorconfig` settings.fixVerify that the filename passed to `get_properties()` is a correct and accessible path to an existing file. -
re.error: global flags not at the start of the expression
cause This error typically occurs with older versions of `editorconfig-core-py` (prior to v0.12.3) when used with Python 3.11 or newer, due to changes in Python's regular expression engine regarding global flag placement.fixUpgrade the `editorconfig` library to the latest version to include the fix: `pip install --upgrade editorconfig`
Warnings
- breaking Python 2 support was dropped in version 0.15.0. This library now requires Python 3.9 or newer.
- gotcha All property values returned by `get_properties` are strings, even for properties that represent numbers (e.g., `indent_size`) or booleans (e.g., `trim_trailing_whitespace`).
- gotcha The library correctly implements the EditorConfig specification for resolving properties, which means properties are determined by the closest `.editorconfig` file to the target path. Misunderstanding how file resolution works (e.g., the effect of `root = true` stopping the search) can lead to unexpected configurations.
- gotcha The `get_properties()` function requires an absolute file path as input. Providing a relative path will result in an `editorconfig.exceptions.PathError`.
- gotcha The `editorconfig.get_properties` function requires the input path to be an absolute path name. Providing a relative path will raise a `PathError`.
Install
-
pip install editorconfig
Imports
- get_properties
import editorconfig properties = editorconfig.get_properties(filepath)
Quickstart
import os
import editorconfig
# Create a dummy .editorconfig file and a test file for demonstration
temp_dir = "temp_editorconfig_test"
os.makedirs(temp_dir, exist_ok=True)
editorconfig_path = os.path.join(temp_dir, ".editorconfig")
with open(editorconfig_path, "w") as f:
f.write("[*.py]\nindent_style = space\nindent_size = 4\nend_of_line = lf\n\n[*.md]\ntrim_trailing_whitespace = true\n")
test_file_path_py = os.path.join(temp_dir, "my_module.py")
with open(test_file_path_py, "w") as f:
f.write("# Dummy Python file content")
test_file_path_md = os.path.join(temp_dir, "README.md")
with open(test_file_path_md, "w") as f:
f.write("# Dummy Markdown content")
try:
properties_py = editorconfig.get_properties(test_file_path_py)
print(f"EditorConfig properties for {test_file_path_py}:")
for key, value in properties_py.items():
print(f" {key}: {value}")
print("\n---\n")
properties_md = editorconfig.get_properties(test_file_path_md)
print(f"EditorConfig properties for {test_file_path_md}:")
for key, value in properties_md.items():
print(f" {key}: {value}")
finally:
# Clean up dummy files and directory
os.remove(editorconfig_path)
os.remove(test_file_path_py)
os.remove(test_file_path_md)
os.rmdir(temp_dir)