Pyinstaller onedir option – exe file outside the directory

Question:

Recently I was experimenting with pyinstaller to create an executable file from my Python script. Everything works as expected.
I tested two options: –onefile, which takes quite a long time (like 20-30sec) to start because it depacks everything into a temporary directory.
The –onedir option is much faster (4sec) to start but it’s not very comfortable to use. When I move exe file outside this directory program no longer works.
My question is: is there a possibility to make the exe file point to this directory location? I want to keep all the pyinstaller files in one place and allow users to have the exe file in any location they want.
Thanks for help.

Asked By: cornisto

||

Answers:

Shortcut. Create .exe shortcut. This way original .exe will be still in parent directory but shortcut can be placed anywhere

Answered By: w8eight

I think you should have some other files which is being required by that exe file & hence when you move exe file outside of directory it’s giving you error. One of the example can be that the exe program require chrome driver & you have placed it within that directory. If you move exe program outside then you need to place the chrom driver also in the new position. I hope it will help you to detect , otherwise we can use exe program anywhere if it does not require any dependency of other files.

Answered By: Muhammad Adeel

Let’s just see a real-life production case. Whenever you download say a pirated game, or and original copy of software, generally they are compressed together. When you unzip them, a new folder is extracted and inside that folder there are a lot of other folders. What you do to run the software is you simply double click the .exe file.

Your situation is the same. If you move the exe file outside the original extracted folder then it simply doesn’t work. So, the work around way is to create a shortcut to the exe file.

Hope this clarifies your doubt 🙂

Answered By: Debdut Goswami

Most of the answers are about creating shortcuts, but its not the true solution. What we want is a clean folder having one dir and the exe outside that dir.

Unfortunately this is not possible at the moment. This issue is there since 2010 and was not fixed till date. Here is the link to that issue:

https://github.com/pyinstaller/pyinstaller/issues/1048

All they say is to create your own bootloader.
Nobody was able to give the PR for that.

I also found a blog on separating the exe from onedir with a hook file. I tried this but was unsuccessful with the latest version of Pyinstaller.

At last you have to do something hacky.
I found a way to do this:

  1. Make a folder Modules in the same directory where executable is present.
  2. Copy and paste all the heavy modules inside that folder.
  3. Add the search path for that module folder inside your program:
# add this code in the top (before imports)
if getattr(sys, 'frozen', False):
    app_path = os.path.join(os.path.dirname(sys.executable),"Modules")
    sys.path.append(app_path) # search this path for modules
  1. Under pyinstaller option --exclude-module, write the names of all those modules you excluded.
  2. Use the one-file option but don’t pack any other assets like images. Add all those external assets/folders outside.
  3. Add this option: --runtime-tmpdir "Temp". With this, the executable will unpack the required files in the same directory under a new folder "Temp".

That’s all, now you will get a very small sized exe file with mainly two required folders "modules" and the "temp". The booting time will also increase, and it will look a lot cleaner.

Answered By: Akascape

A workaround allowing a clean folder structure for the user that only contains the main exe and the libs folder is to create a second Python program containing only one instruction : call the executable of the main Python program within the over-crowed folder with libs. The main program being distributed without the --onefile option, the execution remains faster.

The principle is simple : create a new Python project with a single script your_program_launcher.py with this content :

import os

if __name__ == '__main__':
    os.chdir(".{0}your_program_folder".format(os.sep))
    os.system("your_program.exe")

The script is very simple and limit itself to move on the main program folder (to avoid resource access problems) and call the main program executable.

You just have to distribute this launch program with --onefile and possibly an icon (pyinstaller -i icon.png --onefile your_program_launcher.py) but this script doesn’t use any libs (except os) so this executable will be very light and its execution immediate. Then you will have to put this program in the parent folder so you get a clean folder with this launch executable and the folder containing libraries and main program exe, without using a .bat file which is less natural.

dist
|    your_program_launcher.exe
|
|____your_program_folder
|       |_____lib1
|       |_____lib2
|       |_____libn
|       |     your_program.exe

Since the file is still an executable that needs the files in the subfolder, the user won’t be able to move this executable anywhere he wants, but at least the user won’t be lost in the folder containing all the libraries.

Answered By: nick