Circuit simulation usually starts with a schematic or a SPICE netlist. That works, but it can feel awkward when the rest of your workflow already lives in Python.
PySpice connects those worlds. It lets you describe circuits with Python objects, run external SPICE engines like Ngspice or Xyce, and bring the results back into NumPy, SciPy, and Matplotlib.
PySpice is a Python interface to the Ngspice and Xyce circuit simulators, with an object-oriented circuit API, unit handling, waveform objects, examples, and partial SPICE netlist parsing.
The practical catch is installation. PySpice is a wrapper around external simulators, so a successful pip install is only half the story. You also need a compatible simulator binary or shared library.
What is PySpice?
PySpice is an open-source Python package hosted on GitHub , with documentation at the PySpice documentation site .
It gives you a Python-native way to:
- define SPICE circuits programmatically;
- use units such as volts, ohms, seconds, and kilohms directly in code;
- run operating point, DC, AC, transient, and other analyses;
- interface with Ngspice through shared-library or subprocess modes;
- interface with Xyce through a subprocess backend;
- parse useful parts of existing SPICE netlists;
- convert simulation output into NumPy-friendly waveform objects;
- plot results with Matplotlib.
License: GPL-3.0
This is not a self-hosted dashboard or a Docker service. It is a local engineering library for circuit simulation and analysis.
Why Use Python for SPICE?
SPICE is excellent at solving circuits, but plain netlists are not always pleasant for automation.
Python helps when you want to:
- generate many circuit variants;
- sweep parameters from a script;
- post-process waveforms with NumPy or SciPy;
- plot repeatable reports;
- integrate simulation into tests or notebooks;
- bridge KiCad, SPICE models, and data analysis.
PySpice does not replace Ngspice or Xyce. It wraps them and gives you a friendlier Python layer around circuit definition and output processing.
Tech Overview of PySpice
The repository is a classic Python package with setup.py, requirements.txt, Sphinx docs, examples, and a unit-test/ suite.
Its main modules are:
PySpice.Spice.Netlist: circuit, subcircuit, model, node, and element API.PySpice.Spice.Simulation: analysis declarations and simulator factory.PySpice.Spice.NgSpice: Ngspice shared-library and subprocess integration.PySpice.Spice.Xyce: Xyce subprocess integration.PySpice.Spice.Parser: partial SPICE parser andcir2pysupport.PySpice.Unit: SI prefixes and unit-aware values.PySpice.Probe: waveform and analysis result objects.
The runtime Python dependencies are compact:
PyYAML
cffi
matplotlib
numpy
ply
scipy
The external dependency is the important one: you need Ngspice or Xyce installed separately, unless you use a package manager that installs the simulator for you.
Defining a Circuit
A voltage divider in PySpice looks like this:
from PySpice.Spice.Netlist import Circuit
from PySpice.Unit import *
circuit = Circuit('Voltage Divider')
circuit.V('input', 'in', circuit.gnd, 10@u_V)
circuit.R(1, 'in', 'out', 9@u_kOhm)
circuit.R(2, 'out', circuit.gnd, 1@u_kOhm)
That renders to a SPICE-style netlist:
.title Voltage Divider
Vinput in 0 10V
R1 in out 9kOhm
R2 out 0 1kOhm
Then you can create a simulator and run an analysis:
simulator = circuit.simulator(temperature=25, nominal_temperature=25)
analysis = simulator.operating_point()
print(float(analysis.out))
The appeal is that your circuit generation, simulation call, result extraction, and plotting can all stay in the same Python workflow.
Installing PySpice
For many users, the cleanest route is conda-forge:
conda install -c conda-forge pyspice
The project documentation recommends this path because conda-forge can also provide Ngspice packages. That avoids a common failure mode: Python installs correctly, but the simulator library cannot be found or is the wrong version.
For pip:
python -m venv .venv
. .venv/bin/activate
pip install PySpice
On Linux, you still need simulator packages. For Debian/Ubuntu-style systems, that usually means:
sudo apt install ngspice libngspice0
Depending on the distribution, PySpice may still need help finding the shared library:
export NGSPICE_LIBRARY_PATH=/lib/x86_64-linux-gnu/libngspice.so.0
Why not Docker Compose?
PySpice is not a server application. There is no main web service to expose, no database to persist, and no Compose stack in the repository.
You can put PySpice in a container for reproducible engineering workflows, but that would be a custom Python/simulator environment rather than a self-hosted app deployment.
Field Note: Local Trial with Ngspice 42
I tested PySpice from source on 2026-06-06.
The inspected commit was fe08718d5a1a9af94576b5aef1fa81910f09b037.
The local machine had:
- Python
3.12.3on the host; uvcreating a Python3.13.12virtualenv;/usr/bin/ngspice;- Ngspice
42; /lib/x86_64-linux-gnu/libngspice.so.0.
The install worked:
uv venv .venv
uv pip install -e . -r requirements.txt pytest
The checked-in unit tests also passed:
.venv/bin/python -m pytest unit-test -q
Result:
28 passed, 2 skipped, 2 warnings
A simple circuit could be created and rendered to a valid SPICE netlist. But the real simulator run did not succeed on this machine.
First, the shared-library loader looked for libngspice.so, while the OS only exposed libngspice.so.0:
OSError: cannot load library 'libngspice.so'
After setting NGSPICE_LIBRARY_PATH, PySpice loaded the library but warned:
Unsupported Ngspice version 42
The run then failed with:
PySpice.Spice.NgSpice.Shared.NgSpiceCommandError: Command 'run' failed
Subprocess mode also failed because Ngspice output began with a line PySpice did not expect:
NameError: Expected label Circuit instead of Note
This is the main practical lesson: pin your simulator environment. PySpice 1.5 documents support up to Ngspice 34, while this host has Ngspice 42. The README’s development notes mention fixes for newer Ngspice messages and recent CFFI behavior, so newer source branches may improve this, but the production release needs careful version matching.
Cleanup note: the temporary source checkout at tmp/foss-post-repos/PySpice was removed after review to free disk space.
Best Fit Use Cases
PySpice is most useful when you want code-driven circuit simulation:
- parameter sweeps;
- generated circuits;
- repeatable plots;
- educational notebooks;
- SPICE model experiments;
- automated checks around simple analog circuits;
- KiCad-to-analysis workflows;
- Python data analysis after SPICE simulation.
It is less ideal if you want a maintained GUI simulator, a purely Python solver, or a plug-and-play install on the newest system Ngspice.
Maintenance Notes
PySpice has been around for years and contains a lot of domain-specific work: units, waveform handling, parser code, examples, Ngspice bindings, and Xyce integration.
But the maintenance signal is mixed. The repository README includes a 2024 note saying the maintainer works on PySpice in free time and may be less reactive. The old forum was closed due to lack of activity, and support has moved back to GitHub issues.
That does not make the project unusable. It does mean you should approach it like engineering infrastructure: pin versions, write a smoke test, and keep a reproducible environment.
Alternatives and Complements
PySpice is one tool in a broader electronics simulation toolbox.
- Ngspice is the simulator backend many users will use directly.
- Xyce is a high-performance SPICE-compatible simulator from Sandia.
- KiCad/ngspice is a GUI schematic-driven route.
- SKiDL is useful for Pythonic circuit/netlist generation.
- LTspice remains popular for interactive simulation, though it is not Python-native.
The right choice depends on whether you want a GUI workflow, a Python automation workflow, or a solver backend you call from other tools.
Conclusion
PySpice is a practical bridge between Python and SPICE simulation. Its best feature is not that it hides SPICE, but that it lets Python orchestrate SPICE: generate circuits, run analyses, and process waveforms in the same environment.
The main caveat is simulator compatibility. Install PySpice and Ngspice/Xyce together when possible, prefer conda-forge for reproducible setups, and test a tiny circuit before trusting a larger workflow.
For electronics scripting, education, and repeatable analog experiments, PySpice is still worth knowing.
FAQ
Is PySpice a circuit simulator?
Should I use pip or conda?
Why does PySpice fail to find libngspice?
libngspice.so.0 without an unversioned libngspice.so. Set NGSPICE_LIBRARY_PATH to the actual shared object path, or install the appropriate development package/symlink through your package manager.
Does PySpice work with newest Ngspice releases?
1.5 and documents support up to Ngspice 34. Local testing with Ngspice 42 loaded the library only after an override and then failed during simulation. Pin and test your simulator version.
Comments