1

I want to compile some python libraries I wrote into a single so file using python. The folder structure is like the following:

src/
    main.py
    packages/
        lib1.py
        lib2.py
        lib3.py
        lib4.py
        ...
        __init__.py

(__init__.py is a blank file)

In the setup I got something like:

cythonize(["src/packages/*.py"])
compile = "gcc -shared -Os -I /usr/include/python2.7 -L /usr/lib/python2.7/config-x86_64-linux-gnu -o packages.so -fPIC src/packages/lib1.c src/packages/lib2.c ... src/packages/__init__.c -lpthread -ldl -lpython2.7"
os.system(compile)

By this way, I got the file packages.so. But when I import it in python, I got errors trying to use my functions, python says they're not there.

Using:

import packages
print dir(packages)

Prints:

['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__test__']

Showing that my functions does not exists in the file. But if I add some function in the __init__.py it shows it using dir. I tried importing the others modules (import lib1 and from lib1 import *) in the __init__.py file too and didn't works. I'm out of ideas :c .

What am I doing wrong? Is there a way to put it on one single file or will I have to distribute my program with 10 so files with it?

demongolem
  • 9,474
  • 36
  • 90
  • 105
AngheloAlf
  • 15
  • 2
  • 5
  • Your setup looks questionable...have a look at this site which provides detailed instructions and examples to accomplish this business. http://cython.readthedocs.io/en/latest/src/reference/compilation.html – Dr t Jun 22 '17 at 17:06
  • Possible duplicate of [How can I merge multiple Cython pyx files into a single linked library?](https://stackoverflow.com/questions/8772966/how-can-i-merge-multiple-cython-pyx-files-into-a-single-linked-library) – DavidW Jun 22 '17 at 17:11
  • You don't. The import mechanism relies on each module being a separate .so file – DavidW Jun 22 '17 at 17:12
  • In that case, is there a way to put all of the `.so` into a folder, and in the main import the folder or the files inside the folder? – AngheloAlf Jun 22 '17 at 17:19
  • If I understand correctly (I'm not sure I do though...), use `__init__.py` to (as with standard Python modules) – DavidW Jun 22 '17 at 18:13
  • The `__init__.py` in the folder worked. Its kinda strange it works but `__init__.so` doesn't... – AngheloAlf Jun 22 '17 at 21:36

0 Answers0