[英]Python import from __init__.py file
I have a module with __main__
file and __init__
file in a package. 我有一个包含__main__
文件和__init__
文件的模块。
I want to import a function from the __init__
file: 我想从__init__
文件导入一个函数:
import <package name>
<package name>.run_main()
and it failed with: 它失败了:
ImportError: No module named <package name>
I tried to add: 我尝试添加:
sys.path.append(os.path.dirname(__file__))
But it didn't help. 但这没有帮助。
Does anyone have a idea how can I solve it? 有谁知道我该如何解决?
The most elegant way, currently supported by PEP8 is to make your main code as follows: PEP8当前支持的最优雅的方法是使您的主要代码如下:
if __name__ == '__main__':
main()
and of course, making the main a function with no arguments: 当然,使main为无参数的函数:
def main():
therefore, if you'd like to import that main function if could be as follows: 因此,如果您要导入该主要功能,则可能如下所示:
from <filename> import *
<filename>.main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.