2

Alright, so I managed to use PyInstaller to build a homework assignment I made with Pygame. Cool. The executable works fine and everything.

Problem is, alongside the executable, there is so much clutter. So many files, like pyds and dlls accompany the exe in the same directory, making it look so ugly.

Now, I know that these files are important; the modules I used, such as Pygame, need them to work. Still, how do I make PyInstaller build my game, so that it puts the clutter into its own folder? I could just manually make a folder and move the files in there, but it stops the exe from working.

If this info would help any, I used Python 3.4.3 and am on Windows.

Tespy
  • 567
  • 1
  • 4
  • 15
  • I ran into the same issue some time ago and "solved" it by creating a shortcut of the `.exe` one level higher. – Repiklis Sep 14 '16 at 09:14
  • @Repiklis How do you do that, so the end user won't have to do anything fancy? When I make a shortcut, it only works for the particular path on my computer; it won't work if I send the program to someone else, since they're on a different computer. – Tespy Sep 16 '16 at 18:23
  • The issue is the program folders declared in the shortcut. I am not at a windows pc at the moment and my memory is a bit foggy. If I remember correctly, you have to go into the shortcut properties and remove the "starts in" folder. I'll give it a try tomorrow. If you beat me to it, let me know. – Repiklis Sep 18 '16 at 20:01
  • Have you considered packaging in the one-file mode instead of one-dir? This would keep all of the 'ugly' file dependencies relatively out of sight (living in a temp directory during the execution instead). Does the teacher/professor want or need to look directly at the source while actively running the exe? – tabbek Jan 19 '17 at 00:08
  • I have. Also, the assignment was from a book, so I don't really have a teacher looking to get something turned in. The problem with one-file mode is how it keeps glitching out. That, and it looks nicer to me to have some files and folders alongside the exe, so long as it's not too much (the pyinstaller clutter is WAY too much). – Tespy Jan 21 '17 at 16:04

1 Answers1

2

Apparently this is an open request for pyinstaller, but hasn't happened in the past two years.

My workaround for this one was to create a shortcut one folder higher than the .exe folder with all the files.

The difficult part here is to set up the shortcut to work in all PCs. I did two things in the shortcut properties.

  1. Delete the "Starts in:" path
  2. Set as the "Target": "%windir%\system32\cmd.exe" /c start "" "%CD%\YourFolder\YourEXE.exe"

The second one calls a command line and launches your exe with a relative path. I have only tested it with windows 7. The downside is that this becomes a shortcut to the command line and you get a console window.

A different option is to create a batch file in the one folder higher than the .exe and call it. This shows only briefly the console window, but won't allow you to set your own icon. A sample code that launches your code:

@echo off
setlocal enabledelayedexpansion enableextensions
set CDir=%~dp0
set EXEF=%CDir%MyEXEFolder\
cd %EXEF%
start "MyCode" "MyCode.exe"
exit

Just open a notepad, add the code and save it as a .bat file.

This answer also describes a workaround with py2exe, but a similar approach can be used in pyinstaller. However, I find this quite "ugly" and I am not sure if it's that easy to collect all dependencies in one folder.

There is also Relative, but I didn't want to use another program.

Community
  • 1
  • 1
Repiklis
  • 399
  • 6
  • 20
  • About step 2, what is the "%CD" part supposed to be? – Tespy Sep 21 '16 at 15:40
  • [%CD% is the current working directory](http://stackoverflow.com/questions/4419868/what-is-the-current-directory-in-a-batch-file). It is the folder that contains the shortcut if you double click it. – Repiklis Sep 22 '16 at 08:38
  • Tried it, and it works. But, isn't there another way that doesn't bring up the command window? – Tespy Sep 23 '16 at 04:08
  • Unfortunately, there is no option that I have tried that doesn't show the console even briefly. Maybe Relative will do what you want. I expanded the answer with all the approaches I know of. – Repiklis Sep 23 '16 at 09:52
  • What is the CDir in the batch file example you gave? – Tespy Oct 02 '16 at 21:41
  • Have a look [here](http://stackoverflow.com/questions/5034076/what-does-dp0-mean-and-how-does-it-work). – Repiklis Oct 04 '16 at 12:53
  • Nothing there explains what CDir is :/ – Tespy Oct 13 '16 at 22:09
  • It is just a variable name. You can use whatever you want. I didn't get that you were asking that. The link explains %~dp0, which assigns a value to the variable. – Repiklis Oct 16 '16 at 21:45