How to Import FontForge in an External Script

This guide explains how to import and use the FontForge module within external Python scripts. While the syntax itself is straightforward, successfully running the script requires ensuring that your Python environment can locate the FontForge C-extension libraries or that you execute the script directly through the FontForge interpreter.

Python Import Syntax

In your external Python script, the syntax to import the module is:

import fontforge

You can also import specific classes or functions directly:

from fontforge import open, font

Executing the External Script

Because the fontforge module is a compiled C-extension rather than a pure Python package available via standard pip install, simply running python script.py may return a ModuleNotFoundError unless your environment is properly configured. You can execute your script using one of the following methods.

Method 1: Run via the FontForge Command-Line Interface

The most reliable approach is to execute your script using the FontForge executable itself, which automatically exposes the built-in module:

fontforge -script your_script.py

To pass arguments to your script:

fontforge -script your_script.py arg1 arg2

Method 2: Use System Python Packages (Linux)

On Debian or Ubuntu-based Linux distributions, you can install the system-level Python bindings to use import fontforge directly in standard Python:

sudo apt-get install python3-fontforge

Once installed, run your script normally:

python3 your_script.py

Method 3: Append to sys.path (Windows / macOS)

If FontForge is installed as a standalone application, you can dynamically add its internal site-packages directory to your Python path before importing:

import sys
# Example path for Windows standard installation
sys.path.append(r"C:\Program Files (x86)\FontForgeBuilds\lib\python3.8\site-packages")

import fontforge

Basic Example Script

Below is a complete external script demonstrating font creation:

import fontforge

# Create a new font
font = fontforge.font()
font.fontname = "MyCustomFont"
font.familyname = "MyCustomFont"

# Create a glyph for the letter 'A'
glyph = font.createChar(65, "A")

# Save and generate font file
font.generate("MyCustomFont.otf")
font.close()