我想转换为一个具有模块结构的可执行的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
在第一阶段我只是在做:
我构建了一个可执行文件:
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构建的,因此另一个选择是使用--embed
Cython选项构建一个可执行的嵌入式模块。在
我的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
选项来消除项目中的所有依赖关系。在
我将把带有
setup.py
文件的根包目录称为包目录,在您的例子中是indic-trans
。我将第一级源目录称为模块目录,在您的例子中是indic-trans/indictrans
。如果我正确地理解了您的设置,您就遇到了一个问题,因为您试图在模块目录中而不是在包目录中的脚本上创建
.exe
。这将使当前目录成为内部模块目录,而相关引用将不起作用。在我在类似情况下解决这个问题的方法是在主package目录(与
setup.py
相同的文件夹)中创建一个run.py
脚本,该脚本导入包,然后在模块目录中运行您想要运行的任何脚本。在您没有发布
__init__.py
文件(顺便说一句,在__init.py__
中包含任何真正的代码通常是不允许的,因此我假设您希望在base.py
脚本中运行main()
函数。在这种情况下,使run.py
类似于:现在您只需从命令行运行
run.py
,或者将其用作调试目标来运行包。在请注意,使用PyInstaller,您可以将
^{pr2}$run.py
(或run.spec
)作为目标,并使用name
参数将名称更改为其他名称。在它将在
dist
目录中创建indictrans.exe
。在将软件包作为模块运行
请注意,在模块目录中,您还可以创建一个
__main__.py
文件,该文件基本上是run.py
的副本,并执行相同的操作,但如果Python可执行文件在本地安装,它将作为模块运行。在即,
python -m indictrans
将使用__main__.py
作为入口点运行您的包。在相关问题 更多 >
编程相关推荐