用Pyinstaller或Cython从Python模块创建可执行文件

2024-10-02 20:44:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想转换为一个具有模块结构的可执行的thispython 2.7项目:

(.venv) ip-192-168-22-127:indictrans loretoparisi$ tree -L 1
.
├── __init__.py
├── __init__.pyc
├── __init__.spec
├── _decode
├── _utils
├── base.py
├── build
├── mappings
├── models
├── script_transliterate.py
├── tests
├── transliterator.py
└── trunk

我使用pyinstaller在第一阶段我只是在做:

^{pr2}$

我构建了一个可执行文件:

192 INFO: PyInstaller: 3.3.1
192 INFO: Python: 2.7.10
201 INFO: Platform: Darwin-17.7.0-x86_64-i386-64bit
202 INFO: wrote /Users/loretoparisi/Documents/Projects/AI/indic-trans/indictrans/__init__.spec
208 INFO: UPX is not available.
209 INFO: Extending PYTHONPATH with paths
['/Users/loretoparisi/Documents/Projects/AI/indic-trans',
 '/Users/loretoparisi/Documents/Projects/AI/indic-trans/indictrans']
210 INFO: checking Analysis
218 INFO: checking PYZ
223 INFO: checking PKG
224 INFO: Bootloader /Users/loretoparisi/Documents/Projects/AI/indic-trans/.venv/lib/python2.7/site-packages/PyInstaller/bootloader/Darwin-64bit/run
224 INFO: checking EXE
225 INFO: Rebuilding out00-EXE.toc because __init__ missing
225 INFO: Building EXE from out00-EXE.toc
225 INFO: appending archive to EXE /Users/loretoparisi/Documents/Projects/AI/indic-trans/indictrans/dist/__init__
230 INFO: Fixing EXE for code signing /Users/loretoparisi/Documents/Projects/AI/indic-trans/indictrans/dist/__init__
234 INFO: Building EXE from out00-EXE.toc completed successfully.

但是当我运行它时,我得到一个导入错误

Traceback (most recent call last):
  File "indictrans/__init__.py", line 9, in <module>
ValueError: Attempted relative import in non-package
[30629] Failed to execute script __init__

这个库是通过Cython设置使用Cython构建的,因此另一个选择是使用--embedCython选项构建一个可执行的嵌入式模块。在

我的setup.py如下:

#!/usr/bin/env python

import os

from setuptools import setup
from setuptools.extension import Extension
from Cython.Build import cythonize

import numpy


os.environ['PBR_VERSION'] = '1.2.3'
os.environ['SKIP_WRITE_GIT_CHANGELOG'] = '1'
os.environ['SKIP_GENERATE_AUTHORS'] = '1'


extensions = [
    Extension(
        "indictrans._decode.beamsearch",
        [
            "indictrans/_decode/beamsearch.pyx"
        ],
        include_dirs=[numpy.get_include()]
    ),
    Extension(
        "indictrans._decode.viterbi",
        [
            "indictrans/_decode/viterbi.pyx"
        ],
        include_dirs=[numpy.get_include()]
    ),
    Extension(
        "indictrans._utils.ctranxn",
        [
            "indictrans/_utils/ctranxn.pyx"
        ],
        include_dirs=[numpy.get_include()]
    ),
    Extension(
        "indictrans._utils.sparseadd",
        [
            "indictrans/_utils/sparseadd.pyx"
        ],
        include_dirs=[numpy.get_include()]
    )

]

setup(
    setup_requires=['pbr'],
    pbr=True,
    ext_modules=cythonize(extensions)
)

虽然用--embed option编译一个单独的python文件很容易,但是我不知道如何使用setup.py中的--embed选项来消除项目中的所有依赖关系。在


Tags: pyimportinfotransincludeinitexeusers
1条回答
网友
1楼 · 发布于 2024-10-02 20:44:03

我将把带有setup.py文件的根包目录称为目录,在您的例子中是indic-trans。我将第一级源目录称为模块目录,在您的例子中是indic-trans/indictrans


如果我正确地理解了您的设置,您就遇到了一个问题,因为您试图在模块目录中而不是在目录中的脚本上创建.exe。这将使当前目录成为内部模块目录,而相关引用将不起作用。在

我在类似情况下解决这个问题的方法是在主package目录(与setup.py相同的文件夹)中创建一个run.py脚本,该脚本导入包,然后在模块目录中运行您想要运行的任何脚本。在

您没有发布__init__.py文件(顺便说一句,在__init.py__中包含任何真正的代码通常是不允许的,因此我假设您希望在base.py脚本中运行main()函数。在这种情况下,使run.py类似于:

# This will import everything from the module __init__.py based on it's "all" setup
# You likely want to limit it to just what is needed or just the script you are targeting
# Or you can use the "import indictrans" format and let __init__.py handle it
from indictrans import * 

def main():
    indictrans.base.main()

if __name__ == '__main__':
    main()

现在您只需从命令行运行run.py,或者将其用作调试目标来运行包。在

请注意,使用PyInstaller,您可以将run.py(或run.spec)作为目标,并使用 name参数将名称更改为其他名称。在

^{pr2}$

它将在dist目录中创建indictrans.exe。在


将软件包作为模块运行

请注意,在模块目录中,您还可以创建一个__main__.py文件,该文件基本上是run.py的副本,并执行相同的操作,但如果Python可执行文件在本地安装,它将作为模块运行。在

即,python -m indictrans将使用__main__.py作为入口点运行您的包。在

相关问题 更多 >