Home > Mobile >  VsCode don't run any image in python
VsCode don't run any image in python

Time:03-16

My VsCode isn't running any image, i don't know why, and this happens with jpg too. Here is my code:

from turtle import Turtle, Screen

screen=Screen()
screen.setup(600,600)
screen.bgpic(picname="olho.gif")


screen.exitonclick()

And the error is:

 $ c:/Users/User/Desktop/ProgramasPython/venv/Scripts/python.exe c:/Users/User/Desktop/ProgramasPython/Cursopython/Day20/Day20.py
Traceback (most recent call last):
  File "c:\Users\User\Desktop\ProgramasPython\Cursopython\Day20\Day20.py", line 5, in <module>
    screen.bgpic(picname="olho.gif")
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\turtle.py", line
1482, in bgpic
    self._bgpics[picname] = self._image(picname)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\turtle.py", line
478, in _image
    return TK.PhotoImage(file=filename, master=self.cv)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 4064, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 4009, in __init__
    self.tk.call(('image', 'create', imgtype, name,)   options)
_tkinter.TclError: couldn't open "olho.gif": no such file or directory

CodePudding user response:

I tried as you wrote, and the problem you said will happen when I remove the file from the workspace.

Make sure that file name is consistent with the file name in the code, and whether the file is in the workspace.

CodePudding user response:

import os
cwd = os.getcwd()   //Return a string representing the current working directory.

You can through the above code to get the search location of the file in the python interpreter.

The default value of the cwd in the VSCode was the workspace folder(the folder you opened in the VSCode), if the olho.gif does not under the workspace folder, the python interpreter can not find it. Such as it under the Day20 folder.

But you can add this in the settings.json file to modify the cwd to the parent folder of your executed python script.

"python.terminal.executeInFileDir": true,

Then if the olho.gif under the Day20 will work.

  • Related