Home > Net >  Not able to import a class from a another package in python
Not able to import a class from a another package in python

Time:09-14

* myproject
     getdir
       - somename.py
     pushdir
       - nicename.py

I'm trying to import a class from somename.py into nicename.py. At first I created the the __init__.py file and left it empty. Then I wrote (in nicename.py):

from ..getdir.somename import classnameexample

and I also tried the command without double points at first.

It returns:

ModuleNotFoundError: No module named 'getdir'

CodePudding user response:

Maybe not the most elegant but you can add the path in sys.path before importing getdir:

import sys
sys.path.append('..')
from getdir import somename #Now python will look for getdir in sys.path, including at '../'

CodePudding user response:

Could you try (works for me):

from getdir.somename import classnameexample

  • Related