KFactory
KFactory is a Python library providing a high-level, Pythonic API for KLayout, a popular GDSII layout editor. It simplifies the creation and manipulation of GDSII layouts for chip design, acting as an abstraction layer over KLayout's native Python API (pya). It is often used in conjunction with `gdsfactory` for photonic integrated circuit (PIC) design. The library is actively maintained, with the current version being 2.4.6, and typically releases updates as new KLayout features are exposed or `gdsfactory` integration evolves.
Common errors
-
ERROR: Package 'kfactory' requires Python '>=3.11' but the currently active Python environment is '3.x'
cause Attempting to install `kfactory` in a Python environment older than 3.11.fixUpgrade your Python environment to 3.11 or newer, or create a new virtual environment with a compatible Python version (e.g., `python3.11 -m venv .venv`). -
ModuleNotFoundError: No module named 'pya'
cause The KLayout Python API (`pya`) is not found by your Python interpreter. This typically means KLayout is not installed, or its `pya` module's location is not included in your `PYTHONPATH`.fixInstall KLayout (the full GUI application, not just `klayout-python`). Then, either ensure KLayout's `pya` module directory is added to your `PYTHONPATH` environment variable, or run your Python script using KLayout's integrated Python environment (e.g., `klayout -r myscript.py`). Consult KLayout documentation for `pya` setup. -
AttributeError: module 'kfactory.kcell' has no attribute 'Layer'
cause Attempting to access an older or non-existent layer API, such as `kf.kcl.Layer`. The current API for defining layers is a function, `kf.kcl.layer()`.fixUse the `kf.kcl.layer(layer_id, datatype_id)` function to create KFactory layer objects. For instance, `my_layer = kf.kcl.layer(1, 0)`.
Warnings
- gotcha KFactory requires Python 3.11 or newer. Attempting to install or run with older Python versions will result in installation failures or runtime errors.
- gotcha KFactory is a wrapper around KLayout's native Python API (`pya`). You must have KLayout (the full GUI application) installed on your system, and its `pya` module needs to be discoverable by your Python environment (e.g., via `PYTHONPATH`). Without KLayout and `pya`, KFactory cannot function, leading to `ModuleNotFoundError` for `pya`.
- deprecated The API for defining layers has evolved. Older patterns like direct access to `kf.Layer` or `kf.kcell.Layer` (or attempting to instantiate them as classes) are deprecated or no longer function as expected. The correct and current method is to use the `kf.kcl.layer()` function.
Install
-
pip install kfactory
Imports
- kfactory
import kfactory as kf
- KCell
import kfactory as kf c = kf.KCell('my_cell') - layer
import kfactory as kf LAYER = kf.kcl.Layer(1,0)
import kfactory as kf LAYER = kf.kcl.layer(1, 0)
Quickstart
import kfactory as kf
import os
c = kf.KCell('my_first_cell')
# Create a KLayout KCellLayer object for layer (1,0)
# Arguments are (layer_id, datatype_id)
LAYER_1_0 = kf.kcl.layer(1, 0)
# Add a rectangle to the cell on LAYER_1_0
# Coordinates are in nanometers (1000 = 1um, 2000 = 2um)
c.create_rectangle(LAYER_1_0, 1000, 2000)
# You can also add text
c.create_text("Hello KFactory!", LAYER_1_0, 1000)
# Save the cell to a GDSII file
output_gds_path = os.path.join(os.getcwd(), 'my_first_cell.gds')
c.write(output_gds_path)
print(f"GDSII file saved to: {output_gds_path}")
# To visualize, KFactory can launch KLayout if installed and configured
# Uncomment the line below to open the cell in KLayout GUI
# c.show()