簡體   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