Skip to content

Instantly share code, notes, and snippets.

@bsmithyman
Created May 24, 2013 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bsmithyman/5647232 to your computer and use it in GitHub Desktop.
Save bsmithyman/5647232 to your computer and use it in GitHub Desktop.
amodulename = 'modulename'
try:
import pyximport
pyximport.install()
except:
print('Cython import failed; {0} will use the legacy (pure Python) mode.'.format(amodulename))
from modulenamevanilla import class1, class2, class3
else:
try:
from modulename import class1, class2, class3
except ImportError:
print('Could not build/import {0}.pyx; {0} will use the legacy (pure Python) mode.'.format(amodulename))
from modulenamevanilla import class1, class2, class3
@bsmithyman
Copy link
Author

This is the magic ingredient to make your Cython module compile automatically when imported (with a pure-Python fallback).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment