5

I am using pyinstaller to create EXE from a script. The location of script and exe are as follows:

Script: root-dir\subdir1\subdir2\src\scriptabc.py

Exe: root-dir\subdir1\subdir2\exe\scriptabc.exe

To create Exe I run cmd commands from Exe directory.

pyinstaller -F -i ABC.ico "..\src\scriptabc.py" 

The resulting files are correctly created in Exe folder with the executable in dist folder. I then copy the executable in Exe folder, such that it is at the same depth level as src\scriptabc.py file with reference to root.

When I run the exe, all code items where I used relative paths, such as

wb_assets = load_workbook(filename = '..//input//01_assets.xlsx')

run just fine.

However, when the code comes to absolute path evaluation using pathlib, it fails with the following error:

File "pathlib.py", line 587, in __getitem__
IndexError: 3
[8920] Failed to execute script scriptabc

I am using pathlib to establish absolute path of the root-dir as follows:

from pathlib import Path

abspath = Path(__file__).resolve() # resolve to relative path to absolute
rootpath = abspath.parents[3] # root-dir

There is a lot of code which then creates paths for other sub directories once I establish rootpath.

So this index number works fine when working with src*.py folder Why does it then give an error for an exe sitting in exe*.exe folder?

Is the file option only looking at the script filename? Because if it is the depth, the exe and src folders are exactly same depth.

The exe works just fine if I copy paste in src folder and run from there. In my exe distribution, I just don't want to have src folder at all, just let the code run from exe folder if possible.

Can someone please help with that?

The second part of the question is that I am looking for spec script which thoroughly includes all dll files (I want them bundled in the exe instead of getting redist package installed on every computer. I have the SDK and all required dll files in my computer). Do I need to ask another question for it?

MakJ
  • 119
  • 1
  • 8
  • Did anyone get a chance to look into this problem? The runtime hook is an alternative but is there a simpler way of solving it? – MakJ Feb 14 '19 at 12:10
  • 1
    please refer to this answer, it solved my problem: https://stackoverflow.com/questions/50302229/pyinstaller-not-finding-file-path-inside-program – bactone Apr 19 '21 at 08:24
  • Path(__file__).resolve() will resolve to the root on the extracted installer, NOT the place where your exe is located. See here: https://pyinstaller.org/en/stable/runtime-information.html#using-file – bvan Jul 05 '22 at 06:49
  • To get the correct path you can use `path = Path(".").resolve()` – bvan Jul 05 '22 at 06:54

0 Answers0