Python 如何使用sys.modules。
这段代码将做什么?这里的Python 如何使用sys.modules。,python,module,sys,Python,Module,Sys,这段代码将做什么?这里的sys.modules的作用是什么 this_dir = os.path.dirname(__file__) dir_list = (x for x in os.listdir(this_dir) if os.path.isdir(os.path.join(this_dir, x))) for dirpath in dir_list: if dirpath not in project_path: project_path.append(os.
sys.modules
的作用是什么
this_dir = os.path.dirname(__file__)
dir_list = (x for x in os.listdir(this_dir) if os.path.isdir(os.path.join(this_dir, x)))
for dirpath in dir_list:
if dirpath not in project_path:
project_path.append(os.path.join(this_dir, dirpath))
setattr(sys.modules[__name__], '__path__', project_path)
此代码将当前目录下的所有子目录(脚本运行的位置)添加到路径中,以便可以从其子目录加载任何模块
import os,sys
this_dir = os.path.dirname(__file__) #get the current directory of running script
dir_list = (x for x in os.listdir(this_dir) if os.path.isdir(os.path.join(this_dir, x))) #get the list of directories under current directory
project_path = []
for dirpath in dir_list:
if dirpath not in project_path:
project_path.append(os.path.join(this_dir, dirpath))
setattr(sys.modules[__name__], '__path__', project_path) #add to sys modules so any modules can be imported from thsi directories
在我看来,这是一种毫无必要的复杂表达方式。你从哪里得到这段代码的?