从Python中的其他文件夹导入文件

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值