Python Forum
exe created by pyinstaller only works in the dist directory
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
exe created by pyinstaller only works in the dist directory
#1
Photo 
I want to create an exe for a user to run the Python program. In Visual Studio Code Terminal I typed PS C:\Users\dee.dee> pyinstaller "C:\Users\dee.dee\desktop\Test.py".

It created the Test directory under C:\Users\dee.dee\dist\ and C:\Users\dee.dee\build\. The Test.exe was created in the Test directory. I clicked on both Test.exe. Only the Test.exe under \dist\ works. The Test.exe under \build\ blinked and closed.

I copied the Test.exe from \dist\ to a shared directory for the user. It didn't work, just blinked and closed.

What did I miss? Thanks.
Reply
#2
(Feb-15-2023, 10:21 PM)dee Wrote: I copied the Test.exe from \dist\ to a shared directory for the user. It didn't work, just blinked and closed.

What did I miss? Thanks.
Most think of if this a console app it will show result for .5 sek then close window.
Can add input('Press Enter to Exit') then it will stay open.
(Feb-15-2023, 10:21 PM)dee Wrote: pyinstaller "C:\Users\dee.dee\desktop\Test.py".
Work from one folder when use Pyinstaller,i would never give Path to file as i am in folder wih file.
This make it easier if eg has icon file that want use,so donæt have to give Path to file.
Example with guess game code,and even this run in loop need to add input('Press Enter to Exit') to show the result.
# Files
G:\div_code\game_env
λ ls
dice.ico  guess_game.py

# Content
G:\div_code\game_env
λ rich guess_game.py
import random

print("Guess a number between 1 and 100")
secret_number = random.randint(1, 100)
guess, tries = 0, 0
while guess != secret_number:
    guess = int(input("Take a guess: "))
    if guess > secret_number:
        print("Lower")
    elif guess < secret_number:
        print("Higher")
    tries += 1
print(f'You guessed it! The number was {guess} in {tries} tries')
input('Press Enter to Exit')

# Make
G:\div_code\game_env
λ pyinstaller --onefile --icon=dice.ico guess_game.py
287 INFO: PyInstaller: 5.8.0
287 INFO: Python: 3.10.5
..... ect
Result.
[Image: GQJt0p.png]
Reply
#3
Thank you snippsat for the respond.

In my code, I have time.sleep(60). The exe in the dist\ directory paused for 60 seconds, but not in another directory and did nothing else in the code.
Reply
#4
(Feb-16-2023, 04:49 PM)dee Wrote: In my code, I have time.sleep(60). The exe in the dist\ directory paused for 60 seconds, but not in another directory and did nothing else in the code.
It's hard to say,best if you can posrt code so can test it out.
Also .sleep(60) may(often) be the wrong apporch,a scheduled task with eg schedule may be better.
Reply
#5
Here is the code. When I clicked the Test.exe in the dist\ directory, the row was inserted to working.report_error table, "complete" was printed and it paused for 60 seconds. It didn't do any of these if I clicked it in the build\ and any other directories. No row was inserted to the table and no pause.

The exes wer created at the same time.

My question was why the exe works in the dist\ directory and not work in another directory. The code worked. Thanks.

cursor.execute("insert into working.report_error select getdate(), 'Test', '" + os.path.abspath(__file__)+ "'")
cnxn.commit()
print("complete")
time.sleep(60)
Reply
#6
I think the exe works in the dist directory because dist has all the dependency files. I don't want to copy the whole directory to the share folder the user. How can I refer to the dependency files from the share folder? Thanks.
Reply
#7
Look at Adding Files to the Bundle.
Every time you run Pyinstaller a spec file will be generated.
From comman line can add data like this.
pyinstaller --onefile --icon=dice.ico --add-data info.txt;. guess_game.py
(Feb-22-2023, 09:41 PM)dee Wrote: I don't want to copy the whole directory to the share folder the user.
It should be no problem to that can zip to an unzipp or use a installer eg Inno Setup(make one setup.exe file).
atomxkai and dee like this post
Reply
#8
Hi snippsat,
I did "pyinstaller --onefile guess_game.py". It works. Smile Thank you.
Dee
atomxkai likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  directory not being created after os.mkdir() CAD79 3 380 Mar-16-2024, 04:00 PM
Last Post: deanhystad
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,388 Jun-27-2023, 01:17 PM
Last Post: diver999
  .exe created with pyinstaller fails to execute iankerr 5 6,820 Apr-20-2020, 03:38 PM
Last Post: steve_shambles
  sys.path miss dist-packages in a virtual env. chomwitt 2 2,586 May-23-2019, 08:08 PM
Last Post: chomwitt
  jira-2.0.0.dist causing error with py2exe johnlawlor 1 2,532 Aug-09-2018, 08:27 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020