Skip to main content
Shed Skin compiles pure Python programs into optimized C++ code. This guide will walk you through compiling your first program.

Before you begin

Make sure you have Shed Skin installed. If not, see the installation guide.
Shed Skin works with a restricted subset of Python. Your program must be implicitly statically typed and cannot use dynamic features like eval or getattr.

Compile your first program

Let’s compile the classic “Hello, World!” program and see Shed Skin in action.
1

Create a Python file

Create a file called test.py with the following content:
test.py
print('hello, world!')
This simple program will help you verify that Shed Skin is working correctly.
2

Compile the program

Run the Shed Skin compiler on your Python file:
shedskin build test
This command translates your Python code to C++, then compiles it to a native binary. The --conan flag on Windows downloads required dependencies automatically.
On Windows, if you get an error about nmake not being found, open a “Visual Studio Developer Command Prompt” and try again.
You should see output indicating the translation and compilation process. When complete, you’ll find the generated files in a build directory.
3

Run your compiled program

Execute the compiled binary:
build/test
You should see:
hello, world!
Congratulations! You’ve successfully compiled and run your first Shed Skin program.

Try a more complex example

Let’s compile a program that does some real computation to see Shed Skin’s performance benefits.
1

Create a computation-intensive program

Create a file called mandelbrot.py:
mandelbrot.py
import time

def mandelbrot(max_iterations=1000):
    bailout = 16
    for y in range(-39, 39):
        line = []
        for x in range(-39, 39):
            cr = y/40 - 0.5
            ci = x/40
            zi = 0
            zr = 0
            i = 0
            while True:
                i += 1
                temp = zr * zi
                zr2 = zr * zr
                zi2 = zi * zi
                zr = zr2 - zi2 + cr
                zi = temp + temp + ci
                if zi2 + zr2 > bailout:
                    line.append(" ")
                    break
                if i > max_iterations:
                    line.append("#")
                    break
        print(''.join(line))

t0 = time.time()
mandelbrot()
print('Time: %.2f seconds' % (time.time()-t0))
This program renders a Mandelbrot fractal and measures execution time.
2

Compare performance

First, run it with Python:
python mandelbrot.py
Note the execution time. Then compile and run it with Shed Skin:
shedskin build mandelbrot
build/mandelbrot
The compiled version should be significantly faster - typically 10-100x speedup depending on your system.

Build options

Shed Skin provides several options to optimize your compiled programs:

Disable bounds checking

For maximum performance, disable array bounds checking:
shedskin build --nobounds test
This can dramatically improve performance for indexing-heavy code, but be careful - invalid indices will cause crashes instead of raising IndexError.

Integer and float precision

Control the size of numeric types:
# Use 64-bit integers (default is 32-bit)
shedskin build --int64 test

# Use 64-bit floats (default)
shedskin build --float64 test
Use --int64 if your program needs to handle integers larger than 2³¹-1.

Building extension modules

You can also compile Python code as extension modules that can be imported by regular Python programs. Create simple_module.py:
simple_module.py
def func1(x):
    return x + 1

def func2(n):
    d = dict([(i, i*i) for i in range(n)])
    return d

if __name__ == '__main__':
    print(func1(5))
    print(func2(10))
Compile as an extension module:
shedskin build -e simple_module
Then use it from Python:
# Change to build directory first
import sys
sys.path.insert(0, 'build')

from simple_module import func1, func2

print(func1(5))  # Output: 6
print(func2(10)) # Output: {0: 0, 1: 1, 2: 4, ..., 9: 81}
Extension modules must call their own functions (directly or indirectly) to enable type inference. Place test calls under if __name__ == '__main__' so they don’t run when imported.

Next steps

Now that you’ve compiled your first programs, learn more about:

Typing restrictions

Understand the static typing requirements

Python subset

Learn which Python features are supported

Writing compatible code

Best practices for Shed Skin programs

Optimization guide

Get the best performance from your code

Troubleshooting

Compilation errors

If compilation fails, common issues include:
  • Dynamic typing: Variables changing types between int and string
  • Mixed collections: Lists containing different types like [1, 'hello']
  • Unsupported features: Using *args, **kwargs, or nested functions
See the troubleshooting guide for detailed solutions.

Performance not as expected

  • Use --nobounds to disable bounds checking
  • Profile your code to find bottlenecks
  • Reduce small object allocations (lists, tuples, class instances)
  • See the optimization guide for more tips