Skip to content

Python for photonics

Python has become the standard platform for photonics simulation, modeling, and analysis. Its ecosystem of scientific computing libraries and integration with major simulation tools makes it the practical choice for streamlining photonic device development workflows.

This guide covers environment setup, essential libraries, development tools, and resources for Python-based photonics work.

Note

PreFab is built with Python. This guide reflects tools and practices that have proven effective in production photonics development.

Essential Python Libraries for Photonics

Simulation and electromagnetics:

  • Tidy3D: Commercial cloud-based FDTD solver with high-performance simulations and Python API integration.

  • Meep: Open-source FDTD package with extensive Python bindings. Strong documentation covers both software usage and electromagnetic fundamentals.

Layout and design:

  • gdsfactory: Industry-standard open-source library for photonic integrated circuit design, layout generation, and simulation tool integration.

  • KLayout: 2D mask layout viewer and editor with Python scripting capabilities for automation in chip manufacturing workflows.

  • SiEPIC: Comprehensive PIC design suite with KLayout integration. Includes component libraries, circuit simulation, and verification tools for silicon photonics.

  • gdstk: Library for creating and manipulating GDS files with support for complex geometric operations.

Resources:

  • Awesome Photonics: Curated repository of photonics tools and resources, including Python-compatible frameworks.

Setting up Your Development Environment

Choosing an IDE

Visual Studio Code: Free, open-source IDE with extensive extension support, code completion, debugging tools, and Git integration.

Cursor: VS Code fork with integrated AI coding assistance. Provides code completion, analysis, and refactoring through language model integration.

Zed: High-performance IDE optimized for speed and responsive editing. Still in active development with limited platform support.

This guide uses VS Code as the reference environment.

Essential VS Code Extensions

Python: Core Python support including IntelliSense, linting, debugging, and code formatting.

Jupyter: Run Jupyter Notebooks directly in VS Code for interactive development and data analysis.

Ruff: Fast Python linter and formatter. Enforces consistent code style automatically on save.

Installing Extensions

  1. Open VS Code Extensions view: Click the Extensions icon or press Ctrl+Shift+X (Cmd+Shift+X on macOS)
  2. Search for the extension name
  3. Click Install
  4. Reload VS Code if prompted

Configuring VS Code for Python

Open your VS Code settings file:

  • File > Preferences > Settings (Ctrl+, / Cmd+,), then click the {} icon (upper right)
  • Or use Command Palette (Ctrl+Shift+P / Cmd+Shift+P) and search for Open User Settings (JSON)

Add or merge the following configuration:

{
  "editor": {
    "formatOnSave": true,
    "codeActionsOnSave": {
      "source.fixAll": "explicit",
      "source.organizeImports": "explicit"
    },
    "defaultFormatter": "charliermarsh.ruff"
  },
  "notebook": {
    "formatOnSave": {
      "enabled": true
    },
    "codeActionsOnSave": {
      "notebook.source.fixAll": true,
      "notebook.source.organizeImports": true
    },
    "defaultFormatter": "charliermarsh.ruff"
  },
  "jupyter.runStartupCommands": ["%load_ext autoreload", "%autoreload 2"]
}

This configuration enables:

  • Automatic code formatting on save using Ruff
  • Automatic import organization and linting fixes
  • Jupyter auto-reload to eliminate stale import issues during development

Virtual Environment Management

Virtual environments isolate project dependencies and Python versions, preventing conflicts between projects and system installations.

Environment managers:

  • uv: High-performance environment and package manager from Astral (creators of Ruff). Significantly faster than traditional tools with modern dependency resolution.

  • Conda: Cross-platform manager supporting both Python and non-Python dependencies. Use when projects require compiled scientific libraries or complex system dependencies.

  • venv: Built-in Python 3 module for basic virtual environment creation.

This guide uses uv for its performance and simplicity.

Setting Up uv

1. Install uv

macOS/Linux:

curl -LsSf https://astral.sh/uv/install.sh | sh

Windows:

powershell -c "irm https://astral.sh/uv/install.ps1 | iex"

2. Create and activate an environment

Open the VS Code terminal (View > Terminal or Ctrl+` / Cmd+`):

uv venv
source .venv/bin/activate  # macOS/Linux
# or
.venv\Scripts\activate  # Windows

3. Install packages

uv pip install prefab numpy scipy

uv handles package installation significantly faster than pip while maintaining compatibility with the PyPI ecosystem.

Selecting the Environment in VS Code

For Python files:

  1. Open a .py file
  2. Click the Python version in the status bar (bottom right)
  3. Select the environment from your project directory (.venv)

For Jupyter notebooks:

  1. Open a .ipynb file
  2. Click the kernel name (top right)
  3. Select the Python interpreter from .venv

Troubleshooting:

If your environment doesn't appear in the kernel list:

uv pip install ipykernel
python -m ipykernel install --user --name=myenv

Restart VS Code after installation.

Cloud and Remote Computing

Photonics simulations often require significant computational resources. Cloud platforms provide access to GPUs and high-performance compute without local hardware investment.

Google Colab: Free cloud-based Jupyter environment with GPU and TPU access. Browser-based with no setup required.

GitHub Codespaces: Full cloud development environment integrated with GitHub repositories. Runs VS Code in the browser.

Modal: Serverless computing platform for running Python functions in the cloud with minimal configuration.

SSH VS Code Extension: Connect VS Code to remote servers and cloud instances for development on remote hardware.

Terminal Tools

Git: Distributed version control system. Essential for code management and collaboration. See this introduction for Git fundamentals.

lazygit: Terminal UI for Git operations. Provides an intuitive interface for staging, committing, branching, and managing repositories without memorizing commands.

htop: Interactive process viewer showing real-time CPU and memory usage. Particularly useful for monitoring remote compute instances.

nvtop: GPU process viewer for NVIDIA hardware. Similar to htop but for GPU monitoring.

Oh My Zsh: Framework for managing Zsh shell configuration. Provides plugins and themes for enhanced terminal functionality.

Claude Code: Terminal-based AI coding assistant from Anthropic. Integrates with your development workflow for code generation, debugging, and technical explanations directly in the command line.

AI Coding Assistants

AI coding assistants provide code completion, debugging help, and technical explanations. Useful for both learning Python and exploring new libraries.

ChatGPT: OpenAI's language model with broad programming knowledge. Regular model improvements and feature updates.

Claude: Anthropic's language model. Different training approach may provide complementary capabilities to ChatGPT.

Cursor: AI-enhanced IDE with multi-model support. Choose between different language models based on task requirements.

GitHub Copilot: AI code completion integrated into VS Code. Provides inline suggestions during coding.

Research indicates measurable productivity improvements from AI assistant usage in development workflows.

Additional Resources

Community and support:

  • Stack Overflow: Q&A platform for specific coding issues and technical problems.

  • GitHub: Many photonics projects host issues and discussions on GitHub. Contributing to open-source projects provides direct engagement with tool developers.

Books:

  • "Think Python: How to Think Like a Computer Scientist" by Allen B. Downey: Introduction to Python focused on computational thinking fundamentals.

  • "Effective Computation in Physics: Field Guide to Research with Python" by Anthony Scopatz and Kathryn D. Huff: Python for scientific computing and physics research workflows.

Podcasts:

  • "Talk Python to Me": Interviews with Python community contributors covering language evolution and ecosystem developments.

Next Steps

  1. Run simulations: Work through examples in Meep and Tidy3D documentation.

  2. Contribute to open source: Engage with projects like gdsfactory and SiEPIC.

  3. Try PreFab: Run example notebooks demonstrating fabrication-aware photonics design with virtual nanofabrication models.