简体   繁体   English

仅当超级文件夹中存在重复项时,才从本地文件导入功能

[英]Import functions from local file only when duplicates exist in super folder

I am trying to use functions from a local file. 我正在尝试使用本地文件中的函数。 I had conflicts when I just use 我刚使用时有冲突

import myfunc3 as my

I had the same file name in the super directory, so I am using the following 我在超级目录中具有相同的文件名,因此我正在使用以下文件

from . import myfunc3 as my

This results in an error 这会导致错误

ImportError: cannot import name 'myfunc3' from '__main__'

myfunc3.py had "if name == ' main ':" and removing this line doesn't change the above error. myfunc3.py具有“ if name ==' main ':”,并且删除此行不会更改上述错误。

Add the parent directory of myfunc3 to sys.path and then use import myfunc3 as my . myfunc3的父目录添加到sys.path ,然后使用import myfunc3 as my

from . import myfun3 as my from . import myfun3 as my works because you're explicitly saying "import from the parent directory of the current module" whereas import myfunc3 as my implicitly imports from available PATHs", ie sys.path . from . import myfun3 as my作品,因为您明确地说“从当前模块的父目录import myfunc3 as my ”,而import myfunc3 as my从可用import myfunc3 as my隐式导入”,即sys.path

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

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