programing

Cython을 사용하여 주요 Python 프로그램 컴파일

nasanasas 2020. 10. 29. 08:13
반응형

Cython을 사용하여 주요 Python 프로그램 컴파일


Cython을 사용하여 .so 파일로 컴파일 된 Python 모듈을로드 할 수있는 Python2.6 프로그램이 있습니다. Cython을 사용하여 .py 모듈을 .so 파일로 컴파일했으며 모든 것이 잘 작동합니다.

이것은 Cython과 함께 사용하는 setup.py 파일입니다.

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [
    Extension("ldap", ["ldap.pyx"]),
    Extension("checker", ["checker.pyx"]),
    Extension("finder", ["finder.pyx"]),
    Extension("utils", ["utils.pyx"]),
]

setup(
  name = 'bchecker',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules
)

그래서 저는 Cython을 사용하여 Python 모듈을 컴파일 할 수 있다는 것을 알고 있습니다 (Cython은 제 Python 파일에서 'C'파일을 생성 한 다음 컴파일합니다).하지만 주 Python 프로그램을 Linux 플랫폼에서 실행할 수있는 것으로 컴파일 할 수 있습니까? 그렇다면 Cython 명령 줄 예제를 주시면 감사하겠습니다. 감사.


Adam Matan과 다른 사람들이 주장하는 것과는 달리, 실제로 순수한 Python (.py) 파일에서 Cython을 사용하여 단일 실행 바이너리 파일을 만들 수 있습니다 .

예, Cython은 CPython python 런타임을위한 C / C ++ 확장 모듈 작성을 단순화하는 방법으로 명시된대로 사용하도록되어 있습니다.

그러나 nudzo가이 주석 에서 언급했듯이 --embed명령 줄 프롬프트에서 스위치를 사용할 수 있습니다 .

다음은 매우 간단한 예입니다. python3 및 cython3을 사용하여 Debian Sid 워크 스테이션에서이 작업을 수행하고 있습니다.

python-dev 또는 python3-dev 패키지가 미리 설치되어 있는지 확인하십시오 .

1) hello.py 라는 매우 간단한 Python 프로그램을 만듭니다.

$ cat hello.py

print ( "안녕하세요!")

2) Cython을 사용하여 파이썬 프로그램을 C로 컴파일하십시오.

cython3 --embed -o hello.c hello.py

3) GCC를 사용하여 hello.c를 hello 라는 실행 파일로 컴파일합니다 .

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

4) 당신은 hello 라는 파일로 끝납니다 ...

$ 파일 안녕하세요

hello : ELF 64 비트 LSB 실행 파일, x86-64, 버전 1 (SYSV), 동적 연결 (공유 라이브러리 사용), GNU / Linux 2.6.32 용, BuildID [sha1] = 006f45195a26f1949c6ed051df9cbd4433e1ac23, 제거되지 않음

$ ldd hello
linux-vdso.so.1 (0x00007fff273fe000)
libpython3.3m.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython3.3m.so.1.0 (0x00007fc61dc2c000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fc61da0f000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fc61d70b000)
libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007fc61d508000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fc61d304000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc61cf5a000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fc61cd52000)
libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007fc61cb28000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fc61c90f000)
/lib64/ld-linux-x86-64.so.2 (0x00007fc61e280000)

이 경우 실행 파일은 내 Debian 시스템의 Python 3.3에 동적으로 연결됩니다.

5) 안녕하세요 실행 ...

$ ./ 안녕하세요

안녕하세요!

보시다시피,이 방법을 사용하면 기본적으로 Cython을 사용하여 순수 Python 응용 프로그램을 실행 가능한 컴파일 된 개체 코드로 변환 할 수 있습니다.

이 방법은 훨씬 더 복잡한 응용 프로그램 (예 : 완전한 Python / PySide / Qt 응용 프로그램)에 사용하고 있습니다.

다른 버전의 Python의 경우 gcc -I-l스위치를 적절하게 조정합니다 .

You can then package the executable as a distribution (.deb, etc.) file, without having to package the Python/PySide/Qt files - the advantage being that your application should still be able to run even after a distribution update to the same versions of Python, etc. on that distribution.


Take a look at the answers to Can Cython compile to an EXE? which, contrary to all the other answers here say that yes, it is possible to compile to an executable.

The links at Embedding Cython seem to be a good place to start, but it's not Cython's primary purpose so I don't know how straightforward it would be.


I don't know if this will help or not but Nudzo is correct. You can get it with cython --embed -o main.o main.py and then I try to compile the result with cl/EHsc


You can't, Cython is not made to compile Python nor turn it into an executable.

To produce an .exe file, use py2exe.

To produce a package for Mac or Linux, use the regulard packaging system process as there is nothing specific about a script language program in Unix env.

참고URL : https://stackoverflow.com/questions/5105482/compile-main-python-program-using-cython

반응형