This question already has answers here:
pygame.error "couldn't open image.png" only in command prompt
(3 answers)
Closed 2 years ago.
I found a tutorial online (on Youtube) and it showed me the basics of creating a game with pygame. i found images of the kind he said and what he had. he didn't say where to save them to, i think that may be my problem. here, this is an example of what i have in my program and what shows up on the error
bif="bg.jpg"
mif="images.png"
import pygame, sys
from pygame.locals import *
pygame.init()
screen=pygame.display.set_mode((420,315),0,32)
backround=pygame.image.load(bif).convert()
mouse_c=pygame.image.load(mif).convert_alpha()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(backround, (0,0))
x,y = pygame.mouse.get_pos()
x -= mouse_c.get_width()/2
y -+ mouse_c.get_height()/2
screen.blit(mouse_c, (x,y))
pygame.display.update()
the error reads:
Traceback (most recent call last):
File "C:/Python26/pygame.games/pygame
1", line 10, in <module>
backround=pygame.image.load(bif).convert()
error: Couldn't open bg.jpg
please respond as soon as possible.
Based on that code, your image files need to be saved in the same directory as your game's .py file. Try moving them there.
As a sanity check (to make sure the images will load and it is in fact an issue with supplying the correct path) you could temporarily specify an absolute path to the image in your code, so instead of
bif = "bg.jpg"
You would have
bif = "C:/My_game/images/bg.jpg"
But obviously change the second bit to point to where your image is actually stored. Once you're sure that the image can be loaded using an absolute path, you should use a relative path, and if you're likely to have many assets you should keep them separate from the code, for example by putting them in an assets folder in the same folder as your game code, or similar.
bif = "assets/bg.jpg"
It would be a good idea to check the current working directory during runtime using os.getcwd(). After finding the current working directory, add the assets relative to that path. For example, You could place bg.jpg at <cwd>/python/game/assets/ and write:
bif=r"python/game/assets/bg.jpg"
You may be able to ignore all of this, or only read part depending on what
print pygame.image.get_extended()
take the output for what it's worth, may help you depending on what you get, and one of the following problems.
Try adding the entire path to the directory/folder with the files. If that works either Python is not on Path or you just needed to find that directory/guide Python to it.
Try saving, reading and/or writing to a file. If when saving it automatically assumes the same drive, then your probably good, and need to just guide python to the folder/directory it is located. Their usually isn't a problem if you are saving the .py file in the same exact directory/file as the images.
Do a test, google reading and writing to files, and try to create a simple .txt file.
Also you can use the file save/open/new dialog in Tkinter. See where it assumes to save or Open files to/from.
If Python isn't part of your PATH then there are tons of tutorials and several ways to add it. Some include using the built in native OS GUI's, command prompt/Bash and there are even tools to make it easier, just google how to add python to PATH. You can do a clean uninstall, meaning removing and deleting everything, and then installing it again.
Having two different drives can make things weird. You should see what drive Python is using based on the tests above. Make sure you are saving the current file to the same directory as well as images.
Having multiple versions of Python installed can make things weird. Even if you uninstall Python, sometimes not everything uninstalls in windows, and specifically the versions on Windows market place can mess things up. I would honestly just uninstall, delete and re-install to path if that's the case.
Sometimes using a 3rd party IDLE can actually make things a lot easier. I think the easiest with the PATH type issues is Pycharm. If I am not mistaken it makes you create virtual environments, witch is another way of fixing things (google venv). If all fails then something weird is going on, and you can always remove and uninstall and delete all traces of Python, install it again making sure no traces are left, and it is added to path on install.
The answer about putting the files in the Python library folder is completely wrong. Don't do that, that's pretty much the same as saving it to root. Also how are you going to release anything if it needs you entire Python Library folder, I would have deleted that comment by now if I were that guy, due to sheer embarrassment.
I have had this problem and it was from downloading a version 3.x from the Windows Market place. Somehow it managed to save the Python .exe in the windows folder directly, and other weird things. This made it so frustrating, and hard to fix as it installed it so weird, and I couldn't remember what version I got from the installer and what version from the Marketplace. I would just avoid the Marketplace, I have gotten some janky Windows Sub System, 'Linux' distros also.
Maybe your image is just too large. Make the image smaller first using other image processing application, and try again.
I was just having the same problem. trying to figure out the same tutorial
The way I fixed it.
Put you images in C:\Python26\Lib\site-packages
This is where pygame is installed, and it didn't work until I put the images there.
when declaring your variable for bif and mif, you should write like this;
bif = "C:/Python26/Lib/site-packages/bg.jpg"
with the full location of the image file. NOTICE that the slashes are forwards like so "/"
I tried it with single and double backslashes, like so "\", and "\", but neither worked.
Try all the different ways, and make sure everything is spelled correctly, and you images are where you said they are, and that you got the correct file name (.jpg, .JPG, or .jpeg) You can just look at the properties of the image file.
Related
I am working on a Python project in VSCode with multiple files containing functions, being called from other files, like I have many other times.
However, I noticed that, sometimes, the following line appears at the top of some files:
from asyncore import read
I removed it, and now once I checked the file after altering others, it's back! It is making me go mad. It even is in the git push I did, although I am sure I removed it before sending it. Does anyone know what this is? Thank you.
You can disable it by adding this line to your user or workspace settings (File>Preferences>Settings, or Code>Preferences>Settings).
"python.analysis.autoImportCompletions": false
This Pylance extension setting is documented here. Turning of this setting is similar to this SO:answer except for Python instead of typescript.
I am currently writing a python script for a project, but to move on the with the project,
I need to know where all the downloaded files/programs[exe] will go to in Windows.
I know that it will go to download folder of the user's, as long as the user did not change the default location for the downloaded folder, or they did not do save as to a certain location but I am asking if there is a way to locate all the files downloaded regardless of where they are saved?
Any help will be a great help, I have googled but haven't found the answer i was looking for so hoping someone here could provide some insight.
The fact that a file was downloaded once-upon-a-time isn't something you can observe from the filesystem. In other words, no, you can't do this.
However, NTFS does store the fact that a file was JUST downloaded in an alternate data stream (ADS) which you can read. This is how Windows warns you that a file was downloaded from the internet and might be dangerous.
The problem with that is, if the file is ever opened and the user says the file is safe, that data is removed. You can't know that a file was previously downloaded, only if it was downloaded and has never been opened.
If your python script needs to act upon some other files, you should ask the user where the files are, either on the command line or interactively.
There's a few things to consider.
You can always scan the entire drive to find the latest files. It's slow, but possible. That's your worst case scenario.
By extension, you can leverage the Windows file index - Windows Search. This will speed up searching and allow sorting by date, but is still just a faster version of the first option. In other words, it doesn't tell you anything new.
To get the current user's default downloads folder, consider using Windows environment variables, such as: %USERPROFILE%\downloads. This can greatly simplify programmatically finding the current user's folder without having to know their username.
(Edited) I had stated that there was no way to track the origin, but as pointed out in another answer it is possible to tell if it was downloaded using ADS. Specifically, the Zone.Identifier stream signifies that the file came from a different "security zone", i.e. not this computer. Other than that, it doesn't provide details of where it came from, but perhaps that's not important for your use case.
I just committed a major noob blunder while playing around with basic functionalities of NumPy. I created a small file called "numpy.py" just so that play around with problems related to numpy. I could also later come back for reference - and the name would have helped. Obvious error :-
import numpy
File "C:\Users\USERNAME\workspace\StackOverflow\python\numpy.py", line 25, in <module>
AttributeError: 'module' object has no attribute 'random'
I realized the problem - python was considering my current code as actual numpy and looking for random. I deleted that file and created a new one.
And the problem persists. This image explains it better than my words would.
Now there's a completely new file with a different name - but still it's looking for some methods in the deleted file.
I've already tried:
Cleaning the Project
Restarting Eclipse
Run another .py (successfully) and then this one again
Removed all the conflicting files, created a totally new file. Still same. If the program contains import numpy, it would give same error.
I'd like to know (1) What actually happened wrong here? Why is eclipse so confused (2) How to resolve? (3) What are the best practices in such situations?
I'm the OP. Found a solution after a few minutes of posting this question.
Browsed to the folder containing the code. I noticed that there was a .pyc file for the numpy.pyc I had created earlier. Deleted that one and things are working file now.
However I'd still like to know how to avoid situations like this or resolve directly through eclipse. Though it works now!
This is called an orphaned .pyc file. Eclipse/PyDev now has a setting to automatically handle these (as of Oxygen / v4.7):
PyDev > Builders > "How to handle .pyc/$py.class deletion?" :
Delete any orphaned .pyc file
Knowing Eclipse, presumably that action only gets triggered when you or it refreshes or restarts the workspace, or triggers a rebuild. There are some switches to try to make that automatic too. Or Eclipse > Restart if all else fails.
I'm have difficulty finding any docs describing Python's expected symlink behavior.
I've tried testing it myself but I'm getting weird results - sometimes it appears to work for a while then stops working.
Anyway, I have a situation where MoinMoin keeps it's data files and plugin modules in the same directory but I need to keep the data files outside my git repository (I'm using Openshift PaaS) so it persists. So I want to create this symlink:
~/data/plugins/theme/ -> ~/repo/plugins/theme/
Should I expect this to work?
Should work just fine - symlinks are handled on the OS level, meaning as long as python isn't trying to determine if something is a symlink or not, calling an open on it should just open the file it's pointing to.
Is there anything in particular that isn't working? What are you seeing when it stops working?
I'm experimenting with PyQT, and I was trying to figure out how to get it to work with Firebird. I built the Firebird driver, but couldn't get it to work, so I was thinking maybe I wasn't putting it in the right place. So I tried experimenting with the SQLite driver, since PyQT came with it already installed, with working examples. I figured if I renamed all the qsqlite4.dll driver files I could find, eventually the example program would stop working when I renamed the one it was actually using. That didn't work. So I tried renaming the "site-packages\pyqt4\plugins\sqldrivers" folder to "site-packages\pyqt4\plugins\sqldrivers-old", and that did it. The example program stopped working. So I changed the folder name back, and tried renaming all the files in the folder. But the example program started working again. Then I moved the qsqlite4.dll file to a subdirectory, and it stopped working. So I moved it back, and renamed it to blah.blah.blah. And it worked again. Then I opened up blah.blah.blah with notepad++, and deleted some stuff at the top of the file, and that kept the example program from working. So I'm confused. As far as I can tell, either Python, PyQT, QT, or Windows Vista is finding the dll, no matter what I rename it to, as long as it's in the right folder. I even tried renaming it to the name of one of the other dll's, thinking maybe that would confuse it. But it only confused me. Is this normal?
edit: I'm thinking this has something to do with plugins
Yes, Qt plugin infrastructure is a fairly simple and robust one.
It attempts to load every file in sqldrivers directory. If it is successful, each dll then runs a function that registers all the features such a plugin supports.
Then, you application initalizes. If all the features it needs are available, it works properly, otherwise, some form of error or exception handling occurs.