GDSFactory
gdsfactory is a Python library for generating GDS layouts, primarily used in photonics and electronics design. It provides a robust framework for creating parametric cells, routing, and packaging complex circuits into GDSII format. The current version is 9.40.1, and it maintains a rapid release cadence with frequent updates and new features.
Common errors
-
ModuleNotFoundError: No module named 'gdsfactory'
cause The `gdsfactory` package is not installed in the active Python environment.fixInstall the package using pip: `pip install gdsfactory`. Ensure your IDE or script uses the correct Python interpreter where it's installed. -
ImportError: cannot import name 'Component' from 'gdsfactory.component'
cause This import path was common in older GDSFactory versions (e.g., pre-v7). In current versions, `Component` is directly available from the top-level `gdsfactory` module.fixChange `from gdsfactory.component import Component` to `from gdsfactory import Component` or, more commonly, use `import gdsfactory as gf` and access it via `gf.Component`. -
TypeError: __init__() got an unexpected keyword argument 'name'
cause This error often indicates a Pydantic version incompatibility. GDSFactory v9+ requires Pydantic v2, but Pydantic v1 might be installed in your environment, leading to API mismatches during object initialization.fixUpgrade Pydantic to version 2: `pip install "pydantic>=2.0.0,<3.0.0"`. Verify that any custom Pydantic models you use are compatible with Pydantic v2.
Warnings
- breaking GDSFactory v9+ has a strict dependency on Pydantic v2. If your project uses Pydantic v1 for other purposes, you may encounter compatibility errors due to API changes between Pydantic versions.
- deprecated The method `Component.add_ref()` is deprecated. The preferred and more idiomatic way to add references to a component is now `parent_component << child_component`.
- gotcha Using `gf.show()` or `component.show()` to visualize GDS layouts requires Klayout to be installed and properly configured in your system's PATH, or by setting the `GDSFACTORY_KLAYOUT_PATH` environment variable. Without this, the `show()` function may fail to open the GDS viewer.
Install
-
pip install gdsfactory
Imports
- gdsfactory
import gdsfactory as gf
- Component
from gdsfactory.component import Component
from gdsfactory import Component
Quickstart
import gdsfactory as gf
# Create a new component
c = gf.Component("my_first_device")
# Add a straight waveguide to the component
waveguide = c << gf.components.straight(length=10, width=0.5)
# Optionally, rotate the waveguide
waveguide.rotate(45)
# Write the component to a GDSII file
c.write_gds("my_first_device.gds")
print(f"Generated GDS file: {c.name}.gds")