Ask Your Question
0

setting module import path in cython in the notebook

asked 2011-02-01 15:56:50 +0100

Questors gravatar image

updated 2011-04-28 17:21:01 +0100

Kelvin Li gravatar image

I want to break up a long chunk of cythonised code in the notebook and put the cython code in my own user directory and then cimport as needed. So I have created a directory and created the MyEi.pxd and MyEi.pyx file. In the notebook cell I have:

''%cython  
#cinclude /home/dad/MyModule/  
import cython  
cimport MyEi  
from MyEi cimport MyEiz''

On running I get:
ImportError: No module named MyEi

Where am I going wrong? I am now totally confused about search paths.

Martin

edit retag flag offensive close merge delete

Comments

Thank you for the response. Being a bit of a linux newbie I was hoping I could stay in the notebook. Oh well something else to learn I suppose.

Questors gravatar imageQuestors ( 2011-02-13 11:45:22 +0100 )edit

Thank you for the response. Being a bit of a linux newbie I was hoping I could stay in the notebook. OH well something else to learn I suppose.

Questors gravatar imageQuestors ( 2011-02-13 11:45:22 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2011-02-07 03:00:30 +0100

Shashank gravatar image

If you have a pyx file you can compile it in the terminal and create a library first and then import the library

cython filename.pyx
gcc -O3 -I/usr/include/python2.6/ -I/Library/Python/2.6/site-packages/numpy-2.0.0.dev_f72c605_20110113-py2.6-macosx-10.6-universal.egg/numpy/core/include/ -c -o filename.o filename.c
gcc -dynamiclib -undefined dynamic_lookup -single_module -o filename.so filename.o

for Mac. For Linux use

cython -a filename.pyx
gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python2.5 -o filename.so filename.c

Depending on what the path of the module is you may have to add the following lines in the beginning

import sys
sys.path.append('path-to-module')

Then you can import modules.

edit flag offensive delete link more

Comments

You can find out more about how to compile Cython code by visiting the Cython documentation. This article in particular should help: http://docs.cython.org/src/reference/compilation.html

cswiercz gravatar imagecswiercz ( 2011-02-07 09:30:15 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-02-01 15:56:50 +0100

Seen: 2,124 times

Last updated: Feb 07 '11