[英]How to import a module in Python in a file that is both used as a __main__ and is imported by a file in a different directory?
Say I am using Python 3 (and hence absolute imports) and my directory structure looks like this: 假设我使用的是Python 3(因此是绝对导入),并且我的目录结构如下所示:
> package:
> sub_directory
__init__.py
sub_dir_file.py
sub_dir_file2.py
__init__.py
main_dir_file.py
In the file sub_dir_file.py
I wish to import a function from sub_dir_file2.py
. 我希望在文件sub_dir_file.py
从sub_dir_file2.py
导入一个函数。 The catch is, I want to be able to run sub_dir_file.py
with __name__ == '__main__'
, as well as import it in main_dir_file.py
. 问题是,我希望能够使用__name__ == '__main__'
sub_dir_file.py
__name__ == '__main__'
运行sub_dir_file.py
, __name__ == '__main__'
其导入main_dir_file.py
。 Hence, if in sub_dir_file.py
I use a relative import: 因此,如果在sub_dir_file.py
我使用相对导入:
from .sub_dir_file2 import some_function
the module executes perfectly fine when run from main_dir_file.py
, but throws an error when executed directly (as the relative import cannot be executed when __name__ == '__main__'
. If I however use a normal absolute import, sub_dir_file.py
will execute as a main, but cannot be imported from main_dir_file.py
. 从main_dir_file.py
运行时,该模块执行得很好,但直接执行时会引发错误(因为当__name__ == '__main__'
时无法执行相对导入。但是,如果我使用常规的绝对导入,则sub_dir_file.py
将以一个main,但是不能从main_dir_file.py
导入。
What would be the most elegant way of solving this problem? 解决这个问题的最优雅的方法是什么? One obvious solution seems to be: 一种明显的解决方案似乎是:
if __name__ == '__main__':
from sub_dir_file2 import some_function
else:
from .sub_dir_file2 import some_function
However, it doesn't seem very pythonic. 但是,它似乎不是很pythonic。
I would suggest using a main()
function invoked if name is __main__
. 我建议如果名称为__main__
则使用main()
函数。 It's a good habit anyway, as far as I'm aware. 据我所知,这是个好习惯。
That way you can just call the imported module's main()
yourself. 这样,您可以自己调用导入的模块的main()
。 It has other benefits, too, like allowing you to test or re-invoke the module without necessarily executing the file every time. 它还具有其他好处,例如允许您测试或重新调用模块,而不必每次都执行文件。
You should use the relative import syntax from .sub_dir_file2 import some_function
or eventually the absolute syntax from package.sub_directory.sub_dir_file2 import some_function
. 您应该使用from .sub_dir_file2 import some_function
的相对导入语法from .sub_dir_file2 import some_function
或者最终使用from package.sub_directory.sub_dir_file2 import some_function
的绝对from package.sub_directory.sub_dir_file2 import some_function
语法。
Then, in order to call one of the package sub module, it is simpler to use the -m option of the python interpreter to execute its content as the __main__
module. 然后,为了调用package子模块之一,使用python解释器的-m选项将其内容作为__main__
模块执行会更简单。
Search sys.path for the named module and execute its contents as the main module. 在sys.path中搜索指定的模块,并将其内容作为主模块执行。
Since the argument is a module name, you must not give a file extension (.py). 由于参数是模块名称,因此您不能指定文件扩展名(.py)。 The module name should be a valid absolute Python module name, but the implementation may not always enforce this (eg it may allow you to use a name that includes a hyphen). 模块名称应该是有效的绝对Python模块名称,但是实现可能并不总是强制执行此操作(例如,它可能允许您使用包含连字符的名称)。
For example: 例如:
> python -m package.main_dir_file
> python -m package.sub_directory.sub_dir_file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.