-2

I am trying to import from a specific python file (which I wrote) class that in that file and it seems that python just does not recognize my file. add image to represent the situation more deeply: that the file I want to import the Ball class from:

enter image description here

the main file and the error:

enter image description here

mylib the files

Ido
  • 11
  • 4
  • Maybe go read this question https://stackoverflow.com/questions/4383571/importing-files-from-different-folder – zerbene Dec 17 '20 at 16:55
  • That warning is coming from the IDE, do you get an error when you actually run the script? – Barmar Dec 17 '20 at 16:56
  • 3
    welcome to stackoverflow! please take the [tour](http://stackoverflow.com/tour), read up on [how to ask a question](https://stackoverflow.com/help/asking). [please do not post code as an image](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557) [as they are not helpful](http://idownvotedbecau.se/imageofcode). – hiro protagonist Dec 17 '20 at 16:56
  • yes they are... – Ido Dec 17 '20 at 16:58
  • It's hard to help without knowing the project's structure – Tomerikoo Dec 17 '20 at 17:13
  • He seems to have two files one called main_Pygame.py which he calls as the script and one called shapes.py that is in the same directory. btw, I did not notice right away, but he has two links in his question next to each other rather than one. – Andrew Allaire Dec 17 '20 at 17:15

1 Answers1

0

To import from the same directory, put a dot at the front of the module name like:

from .shapes import Ball

But if you are running main_Pygame.py as the script then this will not work. You should create a subdirectory for shapes.py, say mylib and then make an empty file called __init__.py there and mve shapes.py to that directory. Then import like:

from mylib.shapes import Ball

The __init__.py file tells python that the mylib directory (or whatever else you wish to call the directory) is part of the directory tree it should look for modules under.

Andrew Allaire
  • 1,162
  • 1
  • 11
  • 19
  • Traceback (most recent call last): File "C:/Users/idofr/PycharmProjects/FirstPythonProject/Python_3.8_projects/GAME/more_Pygame.py", line 2, in from .shapes import Ball ImportError: attempted relative import with no known parent package – Ido Dec 17 '20 at 17:01
  • Ah so you must be running more_Pygame.py as main script? – Andrew Allaire Dec 17 '20 at 17:02
  • yes, the import seems to work but i got this error – Ido Dec 17 '20 at 17:05
  • That strange.... I am quite sure that I did it right but now python does not recognize the mylib dict – Ido Dec 17 '20 at 17:15
  • added another pic in the question – Ido Dec 17 '20 at 17:15
  • I am seeing pics rather than links in the questions now, but they look like the same way as you had it. Just. as a check list 1) The script you call should be in a directory by itself. 2) This directory should have a subdirectory, lets call it mylib. 3) An empty file named `__init__.py` with two underscore characters before and after the init part should be in our mylib subdirectory. 4) The shapes.py file should be in the subdirectory. 5) The import should then be `from mylib.shapes import Ball` – Andrew Allaire Dec 17 '20 at 17:20
  • yeah i did it, look at the bottom of the question its some link or somthing – Ido Dec 17 '20 at 17:22
  • they called mylib and files – Ido Dec 17 '20 at 17:23
  • Oh I see now. Looks like you have mylib next to the GAME directory. But mylib should be inside the GAME directory as a subdirectory. – Andrew Allaire Dec 17 '20 at 17:24
  • I fixed wording of question to make it more clear I meant mylib to be a subdirectory. – Andrew Allaire Dec 17 '20 at 17:30
  • 1
    yep! now it's all working, thank you very much for your quick answer and patience. – Ido Dec 17 '20 at 17:31