In order to learn about importing the files from a different folder, assume that the project is having the below folder structure,
为了了解有关从其他文件夹导入文件的信息,请假定项目具有以下文件夹结构,
Project_name
__init__.py
modules
__init__.py
file.py
sub_module_1
__init__.py
file1.py
In the above structure, there is an __init__.py file added to all the folders. This file ensures that the folder is included as a python package. This file is typically empty, however, we could also add the generic module import statement in this file.
在上述结构中,有一个__init__.py文件添加到所有文件夹。 此文件确保该文件夹作为python软件包包含在内。 该文件通常为空,但是,我们也可以在该文件中添加通用模块import语句。
Import files:
导入文件:
Import the files using the syntax from <folder>.<filename> import <module>.
使用<folder>。<filename> import <module>中的语法导入文件。
Example:
例:
from modules.file import test_method
from modules.sub_module_1.file1 import test1_method
翻译自: https://www.includehelp.com/python/importing-files-from-different-folder.aspx