First time here? Check out the FAQ!

Ask Your Question
0

setting module import path in cython in the notebook

asked 14 years ago

Questors gravatar image

updated 13 years ago

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

Preview: (hide)

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 ( 14 years ago )

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 ( 14 years ago )

1 Answer

Sort by » oldest newest most voted
1

answered 14 years ago

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.

Preview: (hide)
link

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 ( 14 years ago )

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: 14 years ago

Seen: 2,149 times

Last updated: Feb 07 '11