在linux下运行的Python脚本

2024-04-20 10:09:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我很难让这个脚本发挥作用。当我调试这段代码时,它不会读入类或函数中。代码将无法正确执行。有人知道这里的问题吗,谢谢

#!/home/build/test/Python-2.6.4

import os, subprocess

class mks_function:

 sandbox="new_sandbox"

 def mks_create_sandbox():  
  try:  
   retcode=call("si createsandbox" + "--no --hostname=bel --port=70 --user=user --password=1234 --populate --project=e:/project.pj --lineTerminator=lf new_sandbox", shell=True)  
   if retcode < 0:  
    print >>sys.stderr, "Child was terminated by signal", -retcode  
   else:  
    print >>sys.stderr, "Child returned", retcode  
 except OSError, e:  
    print >>sys.stderr, "Execution failed:", e  
    print "sandbox retVal="+retcode  
    print "Creating a new sandbox called "+sandbox+" "  
###############################################################

Tags: 函数代码project脚本childhomenewstderr
2条回答

尝试将put作为第一行:

#!/usr/bin/env python

如果您确实需要特定版本的Python,请在运行之前设置环境。在

可能的问题:

  • 你的代码永远不会被执行(就像你只定义类一样)。在文件中使用它(名称具有误导性):

    if __name__ == '__main__': myObject = mks_function()

  • 告诉我们你是如何执行代码的?您是否更改了运行脚本的权限?在

    chmod +x filename.py

  • 或者你想从以下方面开始:

    python filename.py

很少有东西可以检查你的代码

  • call应该是subprocess.call
  • 当您调用/usr/bin/si createsandbox时最好使用完整路径,您可以使用shell中的which si进行检查
  • 不要连接命令"si createsandbox" + " no ...",请使用list["/usr/bin/si","createsandbox no ..."]
  • 您没有导入sys,而是使用它
  • sandbox应该是self.sandbox,而{}应该是{}
  • 使用IDE例如Ulipad。在

相关问题 更多 >