python脚本有什么问题?

时间:2011-08-09 13:45:56

标签: python windows-xp main indentation

python脚本有什么问题?

代码:

import os    
import shutil
import getpass    
os.mkdir("C:\\dtmp") 

shutil.copy("C:\\path\\to\\bb-freeze-script.py","C:\\dtmp")
os.chdir("C:\\dtmp")
shutil.copy("C:\\path\\to\\main.py","C:\\dtmp")
os.system("python bb-freeze-script.py main.py")

os.mkdir("C:\\Program Files\\Directories v0.6")
os.chdir("C:\\")
shutil.rmtree("C:\\dtmp")

print getpass.getuser()

错误:

Traceback (most recent call last):
  File "bb-freeze-script.py", line 8, in <module>
    load_entry_point('bbfreeze==0.97.3', 'console_scripts', 'bb-freeze')()
  File "C:\Python27\lib\site-packages\bbfreeze-0.97.3-py2.7-win32.egg\bbfreeze\__init__.py", line 24, in main
    f.addScript(x)
  File "C:\Python27\lib\site-packages\bbfreeze-0.97.3-py2.7-win32.egg\bbfreeze\freezer.py", line 410, in addScript
    s = self.mf.run_script(path)
  File "C:\Python27\lib\site-packages\bbfreeze-0.97.3-py2.7-win32.egg\bbfreeze\modulegraph\modulegraph.py", line 241, in run_script
    co = compile(file(pathname, READ_MODE).read()+'\n', pathname, 'exec')
  File "C:\dtmp\main.py", line 14
    ^
IndentationError: expected an indented block

操作系统 - Windows XP

1 个答案:

答案 0 :(得分:11)

这是关于如何阅读回溯的快速演练。这很容易。

  • 查看代码,所有内容都是调用Python内置模块。可以肯定地说它们没有导致错误,所以唯一剩下的就是os.system调用。果然,你通过这个电话打电话给python(为什么你不能只导入你要打电话的模块?)。

  • 回溯确认错误发生在您正在调用的其他Python中:

    Traceback (most recent call last):
      File "bb-freeze-script.py", line 8, in <module>
        load_entry_point('bbfreeze==0.97.3', 'console_scripts', 'bb-freeze')()
    
  • 接下来,通读转录本的行来挖掘调用堆栈,找出错误发生的确切位置。

    File "C:\Python27\lib\site-packages\bbfreeze-0.97.3-py2.7-win32.egg\bbfreeze\__init__.py", line 24, in main
        f.addScript(x)
    File "C:\Python27\lib\site-packages\bbfreeze-0.97.3-py2.7-win32.egg\bbfreeze\freezer.py", line 410, in addScript
        s = self.mf.run_script(path)
    File "C:\Python27\lib\site-packages\bbfreeze-0.97.3-py2.7-win32.egg\bbfreeze\modulegraph\modulegraph.py", line 241, in run_script
        co = compile(file(pathname, READ_MODE).read()+'\n', pathname, 'exec')
    
  • 直到你到达

    File "C:\dtmp\main.py", line 14
    IndentationError: expected an indented block
    

你去了,错误在main.py的第14行,你应该有一个缩进但没有。