Fix FileNotFoundError: while running *exe file of python source code generated by pyinstaller

43 Views Asked by At

When trying to convert python file to exe using pyinstaller or auto-py-to-exe, one ERROR appear repeatedly to me which is .exe can'f find file in

{_AbsolutePath_}\tmp_MEI******\base_library.zip\{_FileName_}

when using

os.path.join(sys.path[0])

These are my arguments for pyinstaller: --noconfirm --onefile --runtime-tmpdir "./tmp"

To fix that, use helper funtion when you ever specify path in your code:

def remove_last_three_dirs(path):
    for _ in range(3):
        path = os.path.dirname(path)
    return path

And replace

os.path.join(sys.path[0])

with

os.path.join(remove_last_three_dirs(sys.path[0]))

Why is that? BC converted exe sys.path[0] is

{_AbsolutePath_}\tmp_MEI******\base_library.zip

but not as you expected relative path.

0

There are 0 best solutions below