python 模块管理(到指定模块)

# -*- coding: utf-8 -*-

import sys
import os

#{副本Id:py名}
MapNameDict = {
              100:'cp_icefield_100', 
              101:'Greece_101', 
              }

#cp_icefield_100.py, Greece_101.py 放在gameSys包中
filePath = os.path.join(os.path.dirname(__file__), 'gameSys')


## 玩家进去副本后C++调用
# @parma *args c++传递的参数(包含哪个副本Id)
## return None
def PlayerEnterFB(*args):

    #取出该副本Id
    id = args[0]
    
    moduleName = MapNameDict.get(id, "")
    
    if not moduleName:
        return
    
    if filePath not in sys.path:
        sys.path.append(filePath)
    
    try:
        #引入对应副本py
        if moduleName not in sys.modules:
            module = __import__(moduleName)
        
        else:
            eval("import %s"%moduleName)
            module = eval('reload(%s)'%moduleName)
        
        #执行对应副本中的PlayerEnterFB()
        module.PlayerEnterFB(*args)
        
    except Exception, e:
        raise ImportError, '%s: '%module + str(e)

    return
            

#PlayerEnterFB(100)
#print sys.path
原文地址:https://www.cnblogs.com/richard403/p/2922623.html