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:
the main file and the error:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.