第一:
我结合了斯蒂芬的答案和我自己的一些挖掘来找到答案。最后,Stephen的第一部分完成了窍门:手动添加/导出
PYTHONPATH变量。您实际上可以
pathex在如下
Entrypoint函数中使用来指定:
a = Entrypoint('myapp-cli', 'console_scripts', 'myapp', pathex=['/some/path/to/myapp-cli/myapp', '/some/path/to/myapp-cli'])
毕竟我根本不需要
myapp.main。
第二: 我仍然遇到PyInstaller 无法 产生单个二进制文件的问题。对我来说,这就是窍门:
- 添加 最新 版本PyInstaller到您
requirements.txt
或您install_requires
在setup.py
:https://github.com/pyinstaller/pyinstaller/archive/develop.zip。 - 此外,你可以让你
.spec
与文件--onefile
选项pyi-makespec
像这样:pyi-makespec --onefile myapp.py
。这将创建一个.spec
文件,以确保您的所有软件包都被编译成二进制文件。
最后,下面的spec文件可以解决问题,并且我能够制作一个完全正常的二进制文件:
# -*- mode: python -*-block_cipher = Nonedef Entrypoint(dist, group, name, scripts=None, pathex=None, hiddenimports=None, hookspath=None, excludes=None, runtime_hooks=None): import pkg_resources # get toplevel packages of distribution from metadata def get_toplevel(dist): distribution = pkg_resources.get_distribution(dist) if distribution.has_metadata('top_level.txt'): return list(distribution.get_metadata('top_level.txt').split()) else: return [] hiddenimports = hiddenimports or [] packages = [] for distribution in hiddenimports: packages += get_toplevel(distribution) scripts = scripts or [] pathex = pathex or [] # get the entry point ep = pkg_resources.get_entry_info(dist, group, name) # insert path of the egg at the verify front of the search path pathex = [ep.dist.location] + pathex # script name must not be a valid module name to avoid name clashes on import script_path = os.path.join(workpath, name + '-script.py') print ("creating script for entry point", dist, group, name) with open(script_path, 'w') as fh: print("import", ep.module_name, file=fh) print("%s.%s()" % (ep.module_name, '.'.join(ep.attrs)), file=fh) for package in packages: print ("import", package, file=fh) return Analysis([script_path] + scripts, pathex, hiddenimports, hookspath, excludes, runtime_hooks)a = Entrypoint('myapp-cli', 'console_scripts', 'myapp', pathex=['/some/path/to/myapp-cli/myapp', '/some/path/to/myapp-cli'])pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)exe = EXE(pyz, a.scripts, a.binaries, a.zipfiles, a.datas, name='myapp', debug=False, strip=False, upx=True, runtime_tmpdir=None, console=True )
我认为最终将像Cobra这样的语言用于Golang会更容易,因为Golang可以直接编译一个文件的二进制文件。但是,如果您喜欢Python,就可以解决问题。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)