Search code examples
pythonimportpython-module

importing class from another file gives error when working and no error when not working


I am attempting to import a class named MainMenus from this file

B:\Programming\Python\RaceDash\src\UIModules\Menus.py

here is the code for the class

class MainMenus:
    def StartUp():
        #do stuff
    def MainMenu():
        #doing other stuff

I also have the _init_.py file in this path

B:\Programming\Python\RaceDash\src\UIModules\__init__.py

my main python file is here

B:\Programming\Python\RaceDash\src\Main.Py

and looks like this

from .UIModules.Menus import MainMenus

def Main():

    MainMenus.StartUp()

    while True:
        MainMenus.MainMenu()
        userSelect = input(": ")

Main()

pylance gives no errors when but when I attempt to run the program I get this error:

ile "b:\Programming\Python\RaceDash\src\Main.Py", line 1, in <module>
from .UIModules.Menus import MainMenus
ImportError: attempted relative import with no known parent package

When I remove the leading period pylance shows this error

Import "UIModules.Menus" could not be resolved

The application runs fine but I lose intellisense for any function from the other class.

What could be causing this issue?


Solution

  • You should move the package folder to a directory that is already in PATH

    export PYTHONPATH="${PYTHONPATH}:B:\Programming\Python\RaceDash\src\
    python3 Main.py