I wrote a simple application in python and compiled it with cython, which generated .so files as shown below:

$ ls -l
total 2040 -rw-r--r-- 1 groot groot 486 Jun 14 15:50 compile.py
-rwxr-xr-x 1 groot groot 349232 Jun 14 17:12 CopyDebugThread.cpython-36m-x86_64-linux-gnu.so
-rwxr-xr-x 1 groot groot 491040 Jun 14 17:12 CopyDialog.cpython-36m-x86_64-linux-gnu.so
drwxrwxr-x 2 groot groot 4096 Jun 10 21:09 images
-rwxr-xr-x 1 groot groot 84224 Jun 14 17:12 Main.cpython-36m-x86_64-linux-gnu.so
-rwxr-xr-x 1 groot groot 403424 Jun 14 17:12 MainWindow.cpython-36m-x86_64-linux-gnu.so
-rw-r--r-- 1 groot groot 12 Jun 14 17:43 run.py
-rwxr-xr-x 1 groot groot 739760 Jun 14 17:13 UiMainWindow.cpython-36m-x86_64-linux-gnu.so

  1. How can I run this project as a real application, installed in my Ubuntu 18.04 ?
  2. Is it possible ?
  3. Or do I import it into another python file, then run the python file?

.so files are dynamic libraries, they do not run on their own, but get loaded from other programs. You can either load them from your local folder, or put it into a folder listed in your PATH variable and load it from another program in a different directory.

  • While this is both correct and useful information, this doesn't actually answer the question. – Philip Couling Jun 14 at 14:01

As Stephen Kitt points out, what you have is a shared library (equivalent to Windows .dll). It is generally accepted that Cython is intended to create C modules for Python rather than whole programs (equivalent to Windows .exe). You will therefore find some resistance on the internet to the idea that Cython can be used in this way.

However you should review this answer: https://stackoverflow.com/a/22040484/453851

You can write an executable using Cython as long as you go via a C compiler. That is you can instruct Cyton to write C code using something like:

cython3 --embed -o run.c run.py

And then compile it:

gcc -I /usr/include/python3.3m -o run run.c -lpython3.3m -lpthread -lm -lutil -ldl

In case you are unfamiliar with a C compiler I should mention that the -l switches link your program to other .so libraries. The -lpython3.3m links to python3.3m.so and is important but may vary depending on which version of python you wish to link to.

You will therefore still need some python libraries installed on your system to run a program compiled in this way.

I note your question was also about how to "install" this. You can either package up your built files as a .dpkg and then install with dpkg --install my-program.dpkg. There is a how-to guide on this subject here: https://ubuntuforums.org/showthread.php?t=910717

Alternatively you can just put the files in the right place on your system. Un-packaged programs generally belong in /usr/local/bin and shared objects in /usr/local/lib.

  • In run.py file i just added import Main commend $ cat run.py import Main . Main file is entry point of my project That i compiled by this post: medium.com/@xpl/… . Now i just run cython3 --embed -o run.c run.py or your mentioned is other file? @philipcouling – sayres kabir Jun 14 at 14:32
  • I got error gcc -I /usr/include/python3.3m -o run run.c -lpython3.3m -lpthread -lm -lutil -ldl run.c:4:10: fatal error: Python.h: No such file or directory #include "Python.h" ^~~~~~~~~~ compilation terminated. @philipcouling – sayres kabir Jun 14 at 14:37
  • If Main.py is the entry-point then that is the one which needs to be compiled, not run.py. I'm a little unclear on what happens if your program is split into many python. The gcc error tells you there's a missing C header file (used to tell C which functions exist). In this case you need the python3-dev package installed. – Philip Couling Jun 14 at 14:47
  • I have to usae cython3 --embed -o run.c Main.py to compile python file ? I used python compile.py build_ext --inplace to compile all python file together as a compyle file @philipcouling – sayres kabir Jun 14 at 14:55
  • Let me run some tests when I'm sat behind my Ubuntu laptop and I'll edit to give a more comprehensive answer. – Philip Couling Jun 14 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.