The*_*nse 6 python import python-2.7
我有这样的文件夹结构
\n\nmain_folder\n|\n|--done\n| |\n| |--test1\n| |--__init__.py\n|\n|---check.py\nRun Code Online (Sandbox Code Playgroud)\n\n__init__.py:
\n\nclass Tries(object):\n def __init__(self):\n print "Test"\nRun Code Online (Sandbox Code Playgroud)\n\n检查.py:
\n\nfrom done.test1 import Tries\nTries()\nRun Code Online (Sandbox Code Playgroud)\n\n错误:
\n\n---------------------------------------------------------------------------\nImportError Traceback (most recent call last)\n<ipython-input-8-10953298e1df> in <module>()\n----> 1 from done.test1 import Tries\n\nImportError: No module named done.test1 \nRun Code Online (Sandbox Code Playgroud)\n\n我无法从嵌套文件夹导入模块。有什么办法可以做到这一点吗?
\n\n编辑:
\n\n在萨尔瓦的回答之后,我改变了我的结构,如下所示
\n\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 check.py\n|--__init__.py(no content)\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 done\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py(no content)\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test1\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 __init__.py <-- this files contains your Tries class\nRun Code Online (Sandbox Code Playgroud)\n\n现在也抛出同样的错误。
\n您需要在每个目录中都有一个文件,__init__.py您希望它被视为一个包,因此您在两个目录中都需要它:
.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 check.py\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 done\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test1\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 __init__.py <-- this files contains your Tries class\nRun Code Online (Sandbox Code Playgroud)\n