ImportSYSImportOSdefPysource (FilePath): MyFile=os.path.basename (filePath) dir=os.path.dirname (filePath) fileName=Os.path.splitext (MyFile) [0]if(Os.path.exists (dir)): Paths=Sys.path Pathfound=0 forPathinchpaths:if(dir==path): Pathfound=1if notpathfound:sys.path.append (dir)exec('Import'+filename)inchGlobals ()exec('Reload ('+filename+')')inchGlobals ()returnFilenamepysource ('c:/users/xs/documents/maya/2012/zh_cn/prefs/scripteditortemp/mypy.py')
Import SYSImport OSdef pysource (filePath):#去掉文件目录路径, return file name with file suffixmyFile = Os.path.basename (FilePath)#去掉文件名, return directory pathdir = os.path.dirname (FilePath)#将文件名和扩展后缀名分离后取文件名fileName = Os.path.splitext (MyFile) [0]#上面步骤中, the editor categorizes only the paths and filenames provided, and does not detect the actual existence#检查路径是否存在, if present, go to the next stepif (os.path.exists (dir)):#sys. Path is the collection of addresses that load Python-related modules after Maya startspaths = Sys.pathpathfound = 0#遍历paths这个地址集合中的名称, if there is the same address as the provided file path, except for the variable Pathfound value change to 1, no other action is takenFor path in paths:if (dir = = path):Pathfound = 1#如果路径不是地址集合中的地址, add it (this is the case where multiple startup scripts are considered)if not pathfound:sys.path.append (dir)#以上的语句, the external python file is loaded into the current process as a module and can be called in real time#注意: This is a dynamic load, and the user's external files will disappear from the process the next time the python restarts#exec方法用于执行存储在字符串或者文件中的python语句, like the eval function in Mel#不过这里需要添加in Globals (), because we're going to execute our external script in the entire global variableexec (' import ' +filename) in Globals ()#因为是后导入的模块, so you need to reload the file to ensure that the data is updatedexec (' Reload (' + fileName + ') ') in Globals ()#返回输入文件的命名空间return FileName# Enter a file to provide the full path--do not use Chinese path#注意python中路径是用斜杠 "/" instead of the backslash "\" in the folder path in Windows#如果是MacOS系统, because there is no partition concept for Windows, the path is usually:/users/ ...pysource (' c:/users/mypy.py ')
Python Loading external files