在另一个文件中移动代码时,Python静默失败

时间:2018-06-30 07:02:14

标签: python python-2.7 spyder

我是python的新手,我有一个奇怪的问题,我自己无法理解,我确定它很愚蠢,但是我可以看到它是什么,以前从未遇到过,甚至连好几根带有大量子文件的python脚本

为了记录,我正在Windows上使用Spyder(Python 3.6版本)进行编码和启动脚本,但在每个文件的开头设置了#!/ usr / lib / python2.7 /

我的主脚本是一个大文件,我想通过外部化一些其他文件中的代码来重构它

主要是这样的:

if __name__ == "__main__":
    configuration = Conf.loadConf(os.path.join(scriptDir,confFile))
    print(configuration)    
    loadFavs(configuration,bioses,setDict)

在主脚本中使用loadFavs时,一切正常

我将其移至fav.py文件中的级别与添加import fav并修改fav.loadFavs(configuration,bioses,setDict)的主脚本相同的级别后,它便停止工作,Spyder却无缘无故地无言以对:

In [1]: runfile('C:/DevZone/workspaceFX/scripts4recalbox/BestArcade/fav.py', wdir='C:/DevZone/workspaceFX/scripts4recalbox/BestArcade')

In [2] runfile('C:/DevZone/workspaceFX/scripts4recalbox/BestArcade/fav.py', wdir='C:/DevZone/workspaceFX/scripts4recalbox/BestArcade')

第一行configuration = Conf.loadConf(os.path.join(scriptDir,confFile))应该在屏幕上打印内容,甚至不显示

一旦我将代码放回主脚本中,我的代码就会再次起作用

这种情况发生在我尝试放入不同文件的脚本的几个不同部分

我在这里茫然不知所措:

  • 每个文件的开头

    #!/ usr / lib / python2.7 /

    #--编码:utf-8--

  • 始终以空行结束脚本

  • 在Spyder中而不是外部创建每个文件

我认为要移动的代码不是问题,可以在主脚本中正常工作,并且我遇到了几段代码的问题,但这是:

def parseSetFile(setFile, setDict) :
    file = open(setFile,'r')
    genre = None
    # Parse iniFile in iniFile dir    
    for line in file.readlines() :
        line = line.rstrip('\n\r ')
        if (line.startswith('[') and not line == '[FOLDER_SETTINGS]' and not line == '[ROOT_FOLDER]') :            
            genre = line
            if genre not in setDict :                
                setDict[genre] = []
        else :
            if (genre is not None and not line == '' ) :
                setDict[genre].append(line)

def loadFavs(configuration, bioses, setDict) :
    print("Load favs small set")
    parseSetFile(os.path.join(configuration['scriptDir'],dataDir,smallSetFile),setDict)
    print("Load favs big set")
    parseSetFile(os.path.join(configuration['scriptDir'],dataDir,bigSetFile),setDict)

    print('Nb Genre : %s' %len(setDict))    
    sumGames = 0
    for key in setDict.keys() :
        # print(key)
        # print(setDict[key])
        sumGames = sumGames + len(setDict[key])

    print('Nb Games : %s' %sumGames)
    print('Nb Bios : %s' %len(bioses))

1 个答案:

答案 0 :(得分:2)

好的,我实际上是非常愚蠢的:

In [1]: runfile('C:/DevZone/workspaceFX/scripts4recalbox/BestArcade/fav.py', wdir='C:/DevZone/workspaceFX/scripts4recalbox/BestArcade')

我启动我的fav.py下标不是主要的,当然,它没有任何主要的......