1

I am trying to re-use some octave code (in 1 .m file) in a short python script using oct2py. The python script runs correctly from the cmd console. I can even package it into an exe using pyinstaller (no Errors, only a few warnings), but when I run it the executable throws this error and quits:

---

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

I have the latest versions of python(3.7.1), oct2py(4.0.6), pyinstaller, etc. installed

It feels like I'm missing something or not connecting something someplace here, can anyone help me out with it? Thanks!

DNR
  • 31
  • 4

1 Answers1

1

I had the same problem and as DNR said, I solved setting the OCTAVE_KERNEL_JSON variable. To do that:

  1. Adding the kernel.json file as data file in the spec file, that is explained here https://pyinstaller.readthedocs.io/en/v3.4/spec-files.html#adding-files-to-the-bundle.

Example:

a = Analysis(...
     datas=[ ('/home/user/miniconda3/envs/env/lib/python3.8/site-packages/octave_kernel/kernel.json', 'octave_kernel') ],
     ...
     )
  1. Assigning the new path of the kernel.json to the env variable by adding this in my code before importing oct2py
     if getattr(sys, 'frozen', False):
        application_path = sys._MEIPASS
        os.environ["OCTAVE_KERNEL_JSON"] = os.path.join(application_path, 'octave_kernel/kernel.json')
Ahmet
  • 7,527
  • 3
  • 23
  • 47