수색…


pyinstaller를 사용하여 Cython 프로그램 묶기

진입 점이있는 Cython 프로그램에서 시작하십시오.

def do_stuff():
    cdef int a,b,c
    a = 1
    b = 2
    c = 3
    print("Hello World!")
    print([a,b,c])
    input("Press Enter to continue.")

같은 폴더에 setup.py 파일을 만듭니다.

from distutils.core import setup
from Cython.Build import cythonize
setup(
    name = "Hello World",
    ext_modules = cythonize('program.pyx'), 
)

python setup.py build_ext --inplace 와 함께 실행하면 하위 폴더에 .pyd 라이브러리가 생성됩니다.

그런 다음 라이브러리 (예 : main.py )를 사용하여 바닐라 Python 스크립트를 만들고 .pyd 파일을 그 옆에 놓습니다.

import program
program.do_stuff()

pyinstaller를 사용하여 pyinstaller --onefile "main.py" 을 묶습니다. 이렇게하면 라이브러리가 포함 된 4MB 크기의 실행 파일과 파이썬 런타임이 포함 된 하위 폴더가 만들어집니다.

빌드 자동화 (Windows)

Windows에서 위의 절차를 자동화하려면 .bat 와 비슷한 내용을 사용하십시오.

del "main.exe"
python setup.py build_ext --inplace
del "*.c"
rmdir /s /q ".\build"
pyinstaller --onefile "main.py"
copy /y ".\dist\main.exe" ".\main.exe"
rmdir /s /q ".\dist"
rmdir /s /q ".\build"
del "*.spec"
del "*.pyd"

Numpy를 번들에 추가하기

Numpy를 번들에 추가하려면 include_dirs 키워드로 setup.py 를 수정하고 wrapper Python 스크립트에서 numpy를 가져와 Pyinstaller에 알린다.

program.pyx :

import numpy as np
cimport numpy as np


def do_stuff():
    print("Hello World!")
    cdef int n
    n = 2
    r = np.random.randint(1,5)
    print("A random number: "+str(r))
    print("A random number multiplied by 2 (made by cdef):"+str(r*n))
    input("Press Enter to continue.")

setup.py :

from distutils.core import setup, Extension
from Cython.Build import cythonize
import numpy

setup(
    ext_modules=cythonize("hello.pyx"),
    include_dirs=[numpy.get_include()]
)

main.py :

import program
import numpy
program.do_stuff()


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow