I started playing around with Pygame today, and while following a guide that instructed me to change the game icon I ran into an issue with the line:
img = pygame.image.load("icon.bmp")
My code works fine without this line, but with it the program instantly crashes.
I also noticed that before the crash the icon starts as "icon.bmp", but then changes into the default one. I have a block of code afterwards that stops the program from exiting automatically after running the code, and the window remains on screen when I comment the problematic line.
My guess is maybe a problem with the file path. The image is in the same folder as the script. I am not sure how to make a relative file path, but earlier when I used an absolute path and the issue persisted.
I can't find anyone else with the same problem, and I am getting frustrated at getting stuck on such a simple issue, so in my desperation, I've come here for help. I am sorry for the newbie question, I've never written on stackoverflow.com before.
The file path has to be relative to the current working directory. The working directory is possibly different to the directory of the python file.
It is not enough to put the files in the same directory or sub directory. You also need to set the working directory.
The name and path of the file can be get by __file__. The current working directory can be retrieved with os.getcwd() and can be changed with os.chdir(path):
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
Related
Before anyone says "there are already solutions for this" or "check the path, relative or absolute", I have already done all of that. I am working on my final uni project and this is driving me nuts.
I'm trying to import some molecules using pandas:
df_chlorophenol = pd.read_csv(r'Neural_Network_V1/Simulation_Dataset/Chlorophenol.csv', index_col=False)
The path for the .csv file is:
Simulation_Dataset/Chlorophenol.csv
And my main file is, of course, in Neural_Network_V1. I keep
getting "No such file or directory" and it is driving me crazy because I've tried everything and it won't change. The funniest part is that it worked yesterday.
Any help would be appreciated.
This file has been working perfectly, no changes on my code, and suddenly it decided to stop working, I've made a new file and still, pandas can't read any .csv
If the script is in Neural_Network_V1, and you use the path Neural_Network_V1/Simulation_Dataset/Chlorophenol.csv, then it is a relative path, and the script will look for that path relative to where it is run from, so effectively it’ll try Neural_Network_V1/ Neural_Network_V1/Simulation_Dataset/Chlorophenol.csv.
A few things I would try:
Give an absolute path, regardless of where your application or the file is. For example C:/something/anything/Neural_Network_V1/Simulation_Dataset/Chlorophenol.csv on Windows, or /something/anything/Neural_Network_V1/Simulation_Dataset/Chlorophenol.csv on Linux.
Check the permissions on the file if you are on Linux.
Run the applicaton as Administrator on Windows. (It shouldn’t be necessary, but worth a try if the path is correct and it doesn’t work.)
You can also try copying the file into the same path where the script is, and just use the filename without any path information.
P.S. I'm a very newbie programmer, I don't even use Python in any of my work, that said, I'm required to create this code regardless of whether I want to or not, I apparently offended some in my last post because of how absolutely categorically awful it was, if this happens to you with this post, here's your apology, sorry. Moving on...
The portion of the code where the problem arises from:
#window_layout
path = directory + '/unknown.png'
layout = [[sg.Button("Browse")], [sg.Text("Face Detector")],[sg.Image(filename=path)]]
As said, the code runs fine in Python, but when I create an executable, it gives me an error stating "Your window has an image element with a problem" and states at another part of the window "Couldn't open "C:\users\USER\AppData\Local\Temp_ME1227922/unknown.png": no such file or directory exists."
While creating the executable, this is what I write:
pyinstaller Recognition.py --onefile -w
Error appearing upon starting the .exe file:
def hmsbookings() :
os.system('python hmsbookings.py')
root.after(60000, hmsbookings)
I tried these both lines, but for some reasons, it's not working out.
It shows error :
/System/.../MacOS/Python: can't open file 'hmsbookings.py': [Errno 2] No such file or directory.
The execution path could be different where the file is located.
In your case, the execution is in /System/.../MacOS/Python however your file is somewhere else. This misbehavior could be resolved if you use the full path of the file.
Let me assume, that your file is located in your Desktop. Then this is the modified code that uses absolute path.
import os
def hmsbookings() :
os.system('python os.path.expanduser("~/Desktop/hmsbookings.py")')
root.after(60000, hmsbookings)
I'm not familiar with macOS, I based on this question. If you need more information about absolute path on mac see this question.
I've been trying to set the default working directory in VS Code, when a workspace is not open, to the directory where the file being executed is.
This is the normal behaviour in other IDEs, like Python IDlE.
I need this so my students can run a program from whatever folder they have it in, and it can open files referred to by their program using relative reference. They always have the file in the same directory as the running file (for example an MP3 that they want to open during their program).
I've read for hours lots of documentation, both in VS Code and Stackoverflow, without success.
I know that setting a workspace to that folder will solve it, but is not a viable solution for us, as they will be opening files from different locations all of the time.
I've tried changing the terminal.integrated.cwd in the settings.json file to take by default the directory where the file being executed is (like in IDlE), without success. I can't find the string that I need to include in terminal.integrated.cwd for this.
I've tried the following strings ".", ".", ".\", ""
I've also tried this:
"terminal.integrated.cwd": "${fileDirname}"
But when I run the following piece of code to see if the working directory has changes, after reset of VS code:
import os
cwd = os.getcwd()
print("Current working directory: {0}".format(cwd))
It still shows me the working directory as c:\users\my_user_name
And not the one where the file running this code is.
Could someone please tell me what can I do next? Thank you.
Murphy's law, 5 minutes after posting the question (and after hours of research) I come across this post with a solution that works perfectly.
Settings--> Python>Terminal>Execute In File Dir
I think, os.chdir(path) can be a solution in your case.
https://docs.python.org/3/library/os.html#os.chdir
I am using sqlite3 and python for a new website I am creating. The problem is, the "files_storage.db" file I am trying to create will not appear in any Windows 10 Folder Window, PyCharm's Directory View, nor will it appear via the command line interface.
The catch to this is, if I execute my python script multiple times, I get an error that states the database file already exists... So this file is somewhere, I guess it is a game of cat and mouse to find it.
I have ran into this problem before, but I have ALWAYS been able to find the file via the command line. Usually, I wouldn't both yall with such a question but, this is really irking me and I am going to run into serious issues when it comes time to put this baby on a server. :(
thanks in advance, and here's some screenshots I suppose.
You are using a relative file path.
Relative paths are converted to absolute ones by something like os.path.join(os.getcwd(), <relative path>). So they depend on the current working directory of the process.
Try to open it with an absolute path (starting with drive letter) to avoid any ambiguities.
If you use just a filename without a path, the file will be saved in whatever the current working directory of the Python interpreter is.
To see where the current working directory is, add the following code to the beginning of your program:
import os
print(os.getcwd())
You should then see the working directory in the output.
There is a setting for the current working directory in your IDE somewhere. See e.g. the answers to this question.
You can also do something like:
import os
path = os.path.expanduser("~") + '/Documents'
print(path)
This will allow you to access the directories for the current user. For me, this prints:
'/Users/thomasweeks/Documents'