简体   繁体   English

如何在Python中将模块导入一个既用作__main__又被另一个目录中的文件导入的文件?

[英]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.pysub_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.

相关问题 Python:如何将与__main__相同目录中的模块导入第三方模块? - Python: How to import module that is in the same directory as __main__ to third-party module? 如何将模块导入为 __main__? - How to import a module as __main__? Python 3 - 在同一目录中导入.py文件 - ModuleNotFoundError:没有名为'__main __。char'的模块; '__main__'不是包 - Python 3 - importing .py file in same directory - ModuleNotFoundError: No module named '__main__.char'; '__main__' is not a package 我正在尝试将模块导入到我的main.py python文件中,这两个文件都在同一目录中 - Im trying to import a module into my main.py python file both of which are in the same directory 如何从 python 中的 __main__ 导入相关模块 - How to import relative module from __main__ in python 如何以编程方式以__main__(python -m path.to.file)的形式从Python模块执行文件? - How to execute a file from Python module as __main__ (python -m path.to.file) programatically? 我将一个python文件命名为“__main__.py”,并且我在另一个文件中导入了__main__,无法运行__main__中的函数 - I name a python file as “__main__.py”, and I import __main__ in another file, can't run the functions in __main__ 如何在Python中获取__main__模块的文件名? - How to get filename of the __main__ module in Python? 如何正确导入在主代码和模块中同时使用的模块? - How to import modules that are used in both the main code and a module correctly? 使用 __main__ 方法导入 python - import python with __main__ method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM