Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用sys.modules。_Python_Module_Sys - Fatal编程技术网

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

在我看来,这是一种毫无必要的复杂表达方式。你从哪里得到这段代码的?