Search code examples
python-3.10

Unable to import from file in higher/lower directory?


When I try to import my Function from a lower directory it doesn't work. It works if both file's are in the same directory but if they are not, it fails saying there is no Module named "GameFunctions"

So this would work
./game.py
./GameFunctions.py
But this wouldn't
./etc/game.py
./GameFunctions.py

I tried using the code from GameFunctions import * when both were in the same directory. it worked

Tried the same thing while they were in different directories but I got the error

Traceback (most recent call last):
  File "d:\Projects\MyScripts\PYTHON\Test\tes\game.py", line 1, in <module>
    from GameFunctions import *
ModuleNotFoundError: No module named 'GameFunctions' ```

It can apparently read the file though, since it causes a problem if I rename a def in GameFunctions, it says the def is not defined in game.py

Solution

  • Ohk a much easier solution do one thing write your input statement like this

    from parentdirectory.GameFunctions import *
    

    This should solve your error