Search code examples
pythonmodulesys

I have coded to import python modules into main.py . But I have problem to import


E:\Karya\Python <-- main.py inside here

E:\Karya\packages\extra< -- iota.py inside here

import sys
sys.path.append('E:\Karya')
sys.path.append('E:\Karya\Python')
    
from ..packages.extra import iota
    
print(iota.FunI())

Question:call function FunI() by import iota.py

ERROR : $ C:/Users/ready/AppData/Local/Microsoft/WindowsApps/python3.9.exe e:/Karya/Python/main.py Traceback (most recent call last): File "e:\Karya\Python\main.py", line 5, in from ..packages.extra import iota ImportError: attempted relative import with no known parent package

detail:

tree directory, main.py inside python folder and iota.py inside extra folder

Error

Code


Solution

  • Just add packages directory to sys.path and import iota from extra

    sys.path.append("E:\\Karya\\packages")
    from extra import iota
    

    Make sure that there is init.py file in extra directory (and I think it is needed in packages directory too)