简体   繁体   中英

Python: Unable to import class from another file in different directory

I have following directory structure: ProjectRoot is root project directory. It contains directory 'a' which has mypython.py, which in turn contains a class MyPython. At same level as 'a', there is another directory 'b', which contains file named another.py.

在此处输入图像描述

I am trying to import MyPython class in another.py as:

from a.mypython import MyPython

But during execution this gives error as:

ModuleNotFoundError: No module named 'a'

I event tried to put __init__.py' in a as suggested by many, but still no success.

How to get it working? Its Python 3.7.

If you're doing python b/another.py , this cannot work. Please try python -m b.another instead (after touch a/__init__.py ).

You can read this question for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM