'Python cx-freeze and missing files due to exe path
I did an application in Python using Pygame, then I did the exe using cx-freeze with build_msi option. The result is that the folder where I install the application contains in its root all files needed to run (apart the libs created by cx-freeze). So running my app from the folder where is installed or clicking its link on the desktop it works. Instead if I tr to start the application from a different folder using the console it does not start because it does not find some files, but it does not search for them in the folder where they are installed... so... the problem is that I need to start the application also from a different folder and I can not use a bat file where I could change to the folder and then run the application. Is there a way so that the Python application may know where are the files without fix their path on it (because in this case I would have a lot of problems during development)? I tryed to access them trough something like os.getcwd() + fileName but it does not work anyway.
Solution 1:[1]
The problem here is with the working directory because if you open the file from it's folder then the working directory will be correct but if you open the file from another place the working directory will be different and you will be looking for something that doesn't exist. os.getcwd() returns the working directory. but os.path.dirname(__file__) returns the directory to the folder that the running script is in. so you can do that:
import os
projectFolder = os.path.dirname(__file__)
os.path.join(projectFolder, fileName)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ZiyadCodes |
