1

I am trying to build a program in python using Cython and PyInstaller. Before starting, I built this test program. However, the two modules aren't working together at all. I have tried them both separately, and they work. I looked at this question but that doesn't work.

I also tried using cython to compile it to c and then use gcc, but that doesn't work either (I added the python.h file, and gcc just aborts after complaining that the keyword doesn't exist)

#hello.pyx 
import pygame
def say_hello_to(name):
    print("Hello %s!" % name)
#hello.py
from hello import say_hello_to
say_hello_to('Me')
#setup.py
from setuptools import setup
from Cython.Build import cythonize

setup(
     name='Hello world app',
     ext_modules=cythonize("hello.pyx"),
     zip_safe=False,
)

All of this works in CPython, with :

python3 setup.py build_ext --inplace

Then

python3 hello.py

But PyInstaller keeps complaining it can't find the module hello. I tried add-data and add-binaries, as well as editing the spec file, and it still doesn't work, saying:

invalid add_data_or_binary value: 'home:Achyut-BK:hello.cpython-38-x86_64-linux-gnu.so'
NoobN3rd
  • 1,223
  • 1
  • 9
  • 19
Achyut-BK
  • 65
  • 7
  • 1
    For a start the two modules have __exactly the same name__ as far as Python is concerned – DavidW Jun 01 '20 at 07:33
  • @ DavidW I tried executing the so file created by the setup script, if that's what you mean i got a `Segmentation fault (core dumped)` error. If you meant something else, please excuse me, this is my first time doing cython – Achyut-BK Jun 01 '20 at 08:46
  • That is not what I mean. What I mean is that calling one file `hello.pyx / .so` and another file `hello.py` is going to confuse Python. How does the line `from hello import say_hello_to` know which one to use? – DavidW Jun 01 '20 at 08:52
  • @DavidW hello.py is the main, that imports hello(the so file), so, since hello.py can't be called, it calls the shared object. Also, this works with CPython3 but not PyInstaller – Achyut-BK Jun 01 '20 at 11:43
  • Right, well I'd suggest that this naming arrangement is confusing PyInstaller. I've said everything I plan to here so I won't reply further. – DavidW Jun 01 '20 at 12:00
  • Got the solution. The trick is to use the --hidden-import= command option – Achyut-BK Jun 01 '20 at 12:36
  • Aside from that, in the hello.py, change `from setuptools import setup` to `from distutils.core import setup` – Achyut-BK Jun 01 '20 at 12:49

0 Answers0