I want to convert to an executable this python 2.7 project that has a module structure:
(.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
I'm using pyinstaller
for this at first stage I'm just doing:
pyinstall --onefile __init__.py
and I get a executable built:
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.
But when I run it I get an import error
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__
This library is built using Cython via cythonize setup, so another option would be to build a executable embedded module using the --embed
Cython option.
My setup.py
is the following:
#!/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)
)
While it's easy to compile a singe python file with the --embed option
, see here for more about this, I do not know how to use the --embed
option in the setup.py
in order to get rid of all the dependencies in the project.