1

我正在尝试在使用 oct2py 的简短 python 脚本中重用一些八度音程代码(在 1 .m 文件中)。python 脚本从 cmd 控制台正确运行。我什至可以使用 pyinstaller 将它打包成一个 exe(没有错误,只有几个警告),但是当我运行它时,可执行文件会抛出这个错误并退出:

---

C:\Users\Jason\Desktop\Oct2Py test>readFolder.exe
Traceback (most recent call last):
  File "readFolder.py", line 2, in <module>
    from oct2py import octave
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "c:\python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\oct2py\__init__.py", line 26, in <module>
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "c:\python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\oct2py\core.py", line 14, in <module>
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "c:\python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\octave_kernel\kernel.py", line 49, in <module>
  File "site-packages\octave_kernel\kernel.py", line 54, in OctaveKernel
  File "site-packages\octave_kernel\kernel.py", line 43, in get_kernel_json
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\toshiba\\AppData\\Local\\Temp\\_MEI94962\\octave_kernel\\kernel.json
[5220] Failed to execute script readFolder

我安装了最新版本的python(3.7.1)、oct2py(4.0.6)、pyinstaller等

感觉就像我错过了一些东西或没有在这里连接一些东西,有人可以帮我解决吗?谢谢!

4

1 回答 1

1

我遇到了同样的问题,正如 DNR 所说,我解决了设置 OCTAVE_KERNEL_JSON 变量的问题。要做到这一点:

  1. 在规范文件中添加 kernel.json 文件作为数据文件,此处解释为https://pyinstaller.readthedocs.io/en/v3.4/spec-files.html#adding-files-to-the-bundle

例子:

a = Analysis(...
     datas=[ ('/home/user/miniconda3/envs/env/lib/python3.8/site-packages/octave_kernel/kernel.json', 'octave_kernel') ],
     ...
     )
  1. 通过在导入 oct2py 之前将其添加到我的代码中,将 kernel.json 的新路径分配给 env 变量
     if getattr(sys, 'frozen', False):
        application_path = sys._MEIPASS
        os.environ["OCTAVE_KERNEL_JSON"] = os.path.join(application_path, 'octave_kernel/kernel.json')
于 2020-08-13T00:36:30.153 回答