0

Hi I don't understand why python doesn't let me import files from the same folder, I tried to mark the folder as a source root but it didn't work, you can see better in the image.enter image description here

Mortz
  • 4,654
  • 1
  • 19
  • 35
Samuele
  • 3
  • 4

2 Answers2

0

create __init__.py in your project directory so that it becomes package.

Please check this What is __init__.py for? for more information.

Ramu
  • 13
  • 6
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 23 '22 at 08:15
0

The path you're using is neither absolute nor does it seem to be relative to your IDE's root (excuse me if this is not the case).

One solution is to import every file individually using something like:

from File.Bullet import myfunc

or

from File.Bullet import *

Please note that this assumes that the root of your IDE is spaceInvadersGUI.

This approach is fine for individual files, but since you have a whole directory it would be better to place a __init__.py file along with your .py files which you want to import. Usually you can just create the file and let it be empty, but to make sure that it works as intended we can set __all__ to look for the relevant files:

__all__ = ["Bullet", "Meteor", "Player", "spaceInv..."]

You can now import the files specified in __init__.py using:

from File import *

Now you should be able to run the functions inside Bullet.py by calling them with:

Bullet.myfunction()

Some relevant reading can be found in the official documentation.

Decclo
  • 71
  • 4