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

Question:

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

B:ProgrammingPythonRaceDashsrcUIModulesMenus.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:ProgrammingPythonRaceDashsrcUIModules__init__.py

my main python file is here

B:ProgrammingPythonRaceDashsrcMain.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:ProgrammingPythonRaceDashsrcMain.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?

Asked By: Adrian Edelen

||

Answers:

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

export PYTHONPATH="${PYTHONPATH}:B:ProgrammingPythonRaceDashsrc
python3 Main.py
Answered By: Luu Duc Anh

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.

Answered By: Haseeb
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.