繁体   English   中英

在Linux中运行的Python脚本

[英]Python script running in linux

我在尝试使此脚本正常工作时遇到了麻烦。 当我调试此代码时,它将不会读入类或函数。 该代码将无法正确执行。 有谁知道这里的问题,谢谢

#!/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+" "  
###############################################################

几件事来检查您的代码

  • call应该是subprocess.call
  • 例如,在调用/usr/bin/si createsandbox ,最好使用完整路径,可以在shell中检查使用which si
  • 而不是连接命令"si createsandbox" + "--no ..." ,请使用列表["/usr/bin/si","createsandbox --no ..."]
  • 您没有导入sys ,而是使用它
  • sandbox应该是self.sandboxdef mks_create_sandbox():应该是def mks_create_sandbox(self):
  • 以IDE为例,例如Ulipad

尝试将put作为第一行:

#!/usr/bin/env python

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

可能的问题:

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

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

  • 向我们展示您如何执行代码? 您是否更改了权限以能够运行脚本?

    chmod +x filename.py

  • 还是您尝试将其启动为:

    python filename.py

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM