Python 3 import class from subfolder problem

南笙酒味 提交于 2019-12-13 04:24:37

问题


I have a strange problem regarding import classes from subfolders.

I'm using Python 3.6, therefore an __init__.py should be not required in the subfolders.

I have the following file structure:

root
├── script.py (main)
└── custom
    ├── class1.py
    └── class2.py

This is script.py:

from custom.class1 import Class1
from custom.class2 import Class2

if __name__ == '__main__':
    cl1 = Class1()
    cl2 = Class2()

This is class1.py:

class Class1():
    def __init__(self):
        print('Class1')

if __name__ == '__main__':
    cl1 = Class1()

This is class2.py, which imports also class1:

from class1 import Class1

class Class2():
    def __init__(self):
        cl1 = Class1()
        print('Class2')

if __name__ == '__main__':
    cl2 = Class2()

And now the problem:

It works without error, when i am running python class1.py in the custom subfolder.

It works without error, when i am running python class2.py in the custom subfolder.

But when i am running python script.py in the root folder, i get the following error:

Traceback (most recent call last):
  File .... in <module>
    from custom.class2 import Class2
  File .... line 1, in <module>
    from class1 import Class1
ModuleNotFoundError: No module named 'class1'

How can this be fixed in a way, that the scripts in the custom subfolders can be run on its own and also the script in the root folder works?


回答1:


The problem is that you are running custom.class2 inside of script.py, which means while running custom.class2, you are still in the root directory.

To fix this, you should replace from class1 import Class1 from class2.py with from custom.class1 import Class1.

If you need to be able to run the file from any working directory, you may replace it's contents with this:

import os
import sys

path = os.path.abspath(os.path.dirname(__file__))
if not path in sys.path:
    sys.path.append(path)

from class1 import Class1

class Class2():
    def __init__(self):
        cl1 = Class1()
        print('Class2')

if __name__ == '__main__':
    cl2 = Class2()

The code adds the file's path in to the sys.path list, which holds different paths from which you can import modules.



来源:https://stackoverflow.com/questions/54958196/python-3-import-class-from-subfolder-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!