#-*- coding: UTF-8 -*-
#同一个文件夹下可以直接import
#import hello
#from hello import print_hello
#不在同一个文件夹下通过添加路径的方式引用
import sys
sys.path.append('/home/focus/study/python/hello')
from hello import print_hello
print_hello()
~
导入包的原理类似
本文介绍如何在 Python 中实现跨文件夹的模块导入,包括直接导入与通过添加路径方式的导入方法,并提供具体代码示例。
#-*- coding: UTF-8 -*-
#同一个文件夹下可以直接import
#import hello
#from hello import print_hello
#不在同一个文件夹下通过添加路径的方式引用
import sys
sys.path.append('/home/focus/study/python/hello')
from hello import print_hello
print_hello()
~
导入包的原理类似

被折叠的 条评论
为什么被折叠?