简体   繁体   English

将模块从子文件夹导入到父文件夹

[英]Import module from child folder to parent

So I looked at various similar problems asked here but so far nothing worked for me.所以我查看了这里提出的各种类似问题,但到目前为止没有任何对我有用。

I have the following file architecture:我有以下文件架构:

\folder
    __init__.py
    supClass.py
    script1.py
    \sub
        __init__.py
        script2.py

So in script2.py I try to import supClass.所以在 script2.py 中我尝试导入 supClass。 If I understood well what I read on related subjects, I have to specify that \\folder is part of the PYTHONPATH.如果我很好地理解了我在相关主题上阅读的内容,我必须指定\\folder是 PYTHONPATH 的一部分。

So following examples I read, I ended up with this piece of code :因此,我阅读了以下示例,最终得到了这段代码:

if __name__ == '__main__' and __package__ is None:
    from os import sys, path
    sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))

from test_package import supClass

    a = supClass()
    a.print_sup()

But I get the following error :但我收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
    execfile(filename, namespace)
  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)
  File "C:/Donnees/Programmes_Python/Developpement/Tests/test_package/sub/script.py", line 18, in <module>
    from test_package import supClass
ImportError: No module named test_package

I checked that \\folder is now part of the PYTHONPATH by doing我检查了 \\folder 现在是 PYTHONPATH 的一部分

import sys
print sys.path

in my console and it's ok.在我的控制台中,没关系。 However, the __package__ variable stays set to None.但是, __package__变量保持设置为 None。

The error I get seems to say that my \\folder is not a module.我得到的错误似乎是说我的\\folder不是一个模块。 I don't see why, maybe I am confusing things between "package" and "module".我不明白为什么,也许我在“包”和“模块”之间混淆了。

Anyway, if anyone has an idea, it'd be much appreciated!无论如何,如果有人有想法,将不胜感激!

The parent of \\folder should be on PYTHONPATH . \\folder父级应该在PYTHONPATH You can then do然后你可以做

from folder import subClass 

and

from folder.sub import script2

The reason the parent of folder should be on PYTHONPATH (and not folder itself), is because folder is your package, and to import folder Python needs to look in the directory containing folder . folderfolder应该在PYTHONPATH (而不是folder本身)的原因是因为folder是你的包, import folder Python 需要查看包含folder的目录。

Note that executing scripts from sub-folders is problematic, but easy if you write a setup.py file.请注意,从子文件夹执行脚本是有问题的,但如果您编写 setup.py 文件则很容易。 See my answer here stackoverflow.com/a/41201868/75103 for more info.有关更多信息,请参阅我在此处的回答 stackoverflow.com/a/41201868/75103。

Did you tried to import just supClass?您是否尝试仅导入 supClass?

import supClass

Or try this:或者试试这个:

sys.path.append("../")
import supClass

I tried to reproduce your hierarchy and this does the trick:我试图重现你的层次结构,这是诀窍:

if __name__ == '__main__':
   from os import sys, path
   sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))

   from supClass import supClass
   a = supClass()
   a.print_sup()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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