Adrian Edelen
Adrian Edelen

Reputation: 50

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?

Upvotes: 0

Views: 531

Answers (2)

Haseeb
Haseeb

Reputation: 1

My issue is solved by going to the folder of my program and changing the extension. My file were not register as py file because I have created them in my VScode folder.

Upvotes: 0

Luu Duc Anh
Luu Duc Anh

Reputation: 24

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

Upvotes: 1

Related Questions