Get cython standalone program to import python modules from local installation

1,851 views
Skip to first unread message

Flo

unread,
Feb 23, 2012, 7:05:38 AM2/23/12
to cython-users
Hi,

when I run a standalone cython program (cython'ed with --embed and
then compiled and linked), I get an

ImportError: No module ...

in a Python C-API import call.

I'm trying to get this program to import python modules from a local
python installation (created using virtualenv). When I install the
modules into the global python package directory (in my case /usr/
local/lib/python2.7/site-packages), everything works fine.

I can't seem to find a way to tell Cython to import python modules
from a non-standard directory. The generated C-code always looks up
the modules in the original python site-packages dir.

Any ideas?
Thanks, Flo

Samantha Robertson

unread,
Jul 13, 2017, 10:24:24 PM7/13/17
to cython-users, florianw...@googlemail.com
Hi Flo,

I notice this was posted in 2012 however I have run into this very same problem and have been unable to find any solution. Did you ever figure this out?

Thanks!

Sam

20100.d...@gmail.com

unread,
Jul 14, 2017, 7:05:00 PM7/14/17
to cython-users, florianw...@googlemail.com
What did you actually do? By default, Python looks for modules 1) in the current repository 2) in the Python path (look at sys.path in your python shell). If your module is not installed and you want to use it it should either be in the same directory as the current directory of your shell or you should modify the Python path accordingly.

Samantha Robertson

unread,
Jul 17, 2017, 1:24:57 PM7/17/17
to cython-users, florianw...@googlemail.com

The problem arises only when working in a virtual environment and only if pygame is installed only within the virtual environment, not at system level. The Python script is game.py, and the Cython is game.pyx. Running python game.py works fine, and creating a .so with python setup.py build_ext --inplace where setup.py is as follows:

from distutils.core import setup

from Cython.Build import cythonize


setup(

   ext_modules = cythonize("game.pyx")

)

Allows us to call import game within the python interpreter, which also works.

The problem is when we try to compile game.pyx using the --embed option as follows:

cython --embed -o game.c game.pyx

gcc -O2 -Wall -I/usr/include/python3.5m game.c -o game -lpython3.5m

Using other -I and -L flags e.g. -I/home/a/test/include/site/python3.5/pygame has not had an effect.

This compiles, but running the executable as ./game gives the following error:

Traceback (most recent call last):

 File "game.pyx", line 2, in init game (game.c:1385)

   import pygame

ImportError: No module named 'pygame'

The python path appears to be correct:

>>> sys.path

['', '/home/a/test/lib/python35.zip', '/home/a/test/lib/python3.5', '/home/a/test/lib/python3.5/plat-x86_64-linux-gnu', '/home/a/test/lib/python3.5/lib-dynload', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/home/a/test/lib/python3.5/site-packages']

As the pygame module is in /home/a/test/lib/python3.5/site-packages where test is the virtualenv.

This is the $PATH within the virtual env:

echo $PATH

/home/a/test/bin:/home/a/bin:/home/a/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

This problem is solved by installing pygame at the system level; ./game works inside and outside the virtual environment, but we would like to be able to use virtual environments.


Reply all
Reply to author
Forward
0 new messages