Registry / serialization / drawsvg

drawsvg

JSON →
library2.4.1pypypi✓ verified 86d ago

drawsvg is a Python 3 library designed for programmatically generating SVG (Scalable Vector Graphics) images and animations. It supports rendering drawings to various formats including PNG and MP4, and allows for interactive display within Jupyter Notebook and Jupyter Lab environments. The library maintains an active development status with regular updates, with version 2.4.1 being the latest stable release.

pip install "drawsvg~=2.0"
INSTALL
IMPORT
SIG · DRAWSVG
D
drawsvg
serializationpythonv2.4.1
Install
4.8s avg
Import
175ms
Disk
90MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.4.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.940 runs
installs and imports cleanly · install 0.0s · import 0.181s · 18.2MB
glibc
py 3.103.940 runs
installs and imports cleanly · install 4.8s · import 0.169s · 19MB
90MB installed
● package 90MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

drawsvg
import drawsvg as draw
import drawSvg as draw
The package name in Python 2.x and early 1.x versions used 'drawSvg' (camelCase). Version 2.x+ uses 'drawsvg' (snake_case).

This example demonstrates how to create a simple SVG drawing with a circle, rectangle, and text. It then saves the output to an SVG file. Instructions for displaying in Jupyter Notebook and saving to PNG are also included, highlighting the optional dependencies for the latter.

import drawsvg as draw import os # Create a new drawing with a viewBox of 200x100, origin at center d = draw.Drawing(200, 100, origin='center') # Draw a red circle centered at (0,0) d.append(draw.Circle(0, 0, 40, fill='red')) # Draw a blue rectangle d.append(draw.Rectangle(-90, -45, 60, 30, fill='blue', stroke='black', stroke_width=2)) # Draw some text d.append(draw.Text('Hello drawsvg!', 20, 0, 30, center=True, fill='green')) # Save the drawing to an SVG file d.save_svg('example.svg') # To display in a Jupyter Notebook, simply put 'd' as the last line in a cell: # d # To save as PNG (requires Cairo and 'drawsvg[all]' installation) # if os.path.exists('/usr/bin/cairo-svg'): # Simple check for Cairo-SVG presence # d.save_png('example.png') # else: # print("Cairo not installed, cannot save PNG. See 'install' section.") print("Generated example.svg")
Debug
Known issues
breakingUpgrading from drawsvg 1.x to 2.x introduces major breaking changes. Method and argument names transitioned from `camelCase` to `snake_case` (e.g., `saveSvg` to `save_svg`), and the package name for imports changed to lowercase (`drawSvg` to `drawsvg`). Additionally, the default coordinate system's Y-axis now matches SVG standards, increasing downwards.
fix
Update all method/argument calls and import statements to use `snake_case` and the lowercase package name `drawsvg`. Adjust Y-coordinate logic where necessary.
affects: 1.x to 2.x
gotchaGenerating raster image formats (PNG, MP4, GIF) with `save_png()` or `save_video()` requires the system-level Cairo library, which cannot be installed via `pip`. You must install it separately using your system's package manager (e.g., `apt-get` on Ubuntu, `brew` on macOS) in addition to `pip install "drawsvg[all]"`.
fix
Install Cairo on your operating system (e.g., `sudo apt-get install libcairo2` on Ubuntu, `brew install cairo` on macOS) and ensure `drawsvg` is installed with the `[all]` extra.
affects: 2.x+
gotchaThe interactive Jupyter widget, `drawsvg.widgets.DrawingWidget`, currently works reliably in Jupyter Notebook but does not yet function in Jupyter Lab.
fix
If interactive widget functionality is crucial, use Jupyter Notebook instead of Jupyter Lab.
affects: 2.x+
gotchaCustom fonts embedded using `embed_google_font` or similar methods will work in most browsers when viewed as SVG, but they will not be rendered when using `rasterize()`, `save_png()`, or `save_video()` to convert to raster formats.
fix
For raster output, ensure you are using system-installed fonts or fallback fonts that are universally available and don't rely on embedded custom fonts.
affects: 2.x+
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'drawSvg'
Attempting to import the library using the old `camelCase` package name `drawSvg` (capital S) instead of the current `snake_case` name `drawsvg`.
fix
Change your import statement from `import drawSvg` to `import drawsvg`.
AttributeError: 'Drawing' object has no attribute 'saveSvg'
Calling a method using the old `camelCase` naming convention from version 1.x on a `drawsvg` 2.x+ object.
fix
Update the method call to `snake_case`, e.g., change `d.saveSvg('file.svg')` to `d.save_svg('file.svg')`.
Error: Format: "png" not recognized. Use one of: [...]
This error typically occurs when trying to use `save_png()` or `save_video()` without the necessary Cairo system library installed, even if `drawsvg[all]` was installed via pip.
fix
Install the Cairo library at the system level (e.g., `sudo apt-get install libcairo2` on Ubuntu or `brew install cairo` on macOS).
Upgrade
Version history
2.4.1latest on PyPI · released Jan 4, 2026
Audit
Dependencies
CairooptionalRequired for raster image output (PNG, MP4, GIF) using `save_png()` or `save_video()`. Must be installed at the system level.
Agent activity
18 hits · last 30 days
node
16
Resources
drawsvg — pip install drawsvg · libregistry