0

I have developed an app that is suppose to read a text file from the same directory of the script being run. In Visual Studio, the following code works no issues and responds with the expected folder:

WorkingDirectory = os.path.dirname(os.path.abspath(__file__))
Testing101 = os.chdir(WorkingDirectory)
print("Working Directory is: "+ str(WorkingDirectory))
print("Changing Directory to: "+ str(WorkingDirectory))

However when I convert this to a .exe using pyinstaller FileName.py --onefile in the visual studio terminal then run the .exe it generates, the code above points to a completely different folder than where the script is being run from.

Example (not actual folder paths) Folder 1 - script location "D:/Python Testing" Folder 2 - random folder "C:/user/name/temp"

I have attempted to take hints from (How do I get the full path of the current file's directory?) and other sources but none seem to redirect the script to the expected path.

Below is the part of my code that i am having the issues with:

print("Loading... \nStandby...")
#==============================================
#           Notes
#==============================================
'''
Version Notes:
    Beta 0.1:   GUI Layout
                Basic Functionality
'''
#==============================================
#           Libraries
#==============================================
print("Importing Libraries...")
import os
import pathlib
#Other Libraries
print("Libraries Imported...")
#==============================================
#           Global Variables
#==============================================
(Not used yet)

#==============================================
#           Change Working Directory
#==============================================
WorkingDirectory = os.path.dirname(os.path.abspath(__file__))
Testing101 = os.chdir(WorkingDirectory)
print("Working Directory is: "+ str(WorkingDirectory))
print("Changing Directory to: "+ str(WorkingDirectory))

I have tried following numerous YouTube videos, google searches and stack overflow examples - none worked at redirecting the script to the correct folder.

  • This is normal, correct behavior. The temp directory is where pyinstaller is unpacking things into at runtime, which is not (never was, isn't expected or supposed to be) the directory of the exe file. You can adjust pyinstaller's settings such that it does the unpack in-memory, but that just means that there _is no_ `__file__` referring to a legitimate filesystem location at all. – Charles Duffy Mar 09 '23 at 21:03
  • Anyhow -- we have answered questions covering this topic _specific to pyinstaller_, which is what you want to look for, instead of taking an answer built for Python-in-general that isn't pyinstaller-aware. – Charles Duffy Mar 09 '23 at 21:05
  • @CharlesDuffy I understand that there are a large source of "similar" looking solutions on Stack Overflow (most of which i have tried have not worked). Can you point me towards where you are talking about? – Rhys Flanders Mar 09 '23 at 21:10
  • See the very top of your question, where it says "This question already has answers here:" with a link below it. _That specific link_ goes to a question that's specific to pyinstaller, and that has answers specific to pyinstaller. – Charles Duffy Mar 10 '23 at 16:47

0 Answers0