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.