简体   繁体   中英

Python script running in linux

I am having trouble trying to get this script to work. When I debug this code it will not read into the class or functions. The code will not execute properly. Has anyone know the problem here, Thanks

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

Few things to check your code

  • call should be subprocess.call
  • better use full path when you call for example, /usr/bin/si createsandbox , you can check with which si in shell
  • instead of concatenating the commands "si createsandbox" + "--no ..." , please use list ["/usr/bin/si","createsandbox --no ..."]
  • you didn't import sys , but using it
  • sandbox should be self.sandbox and def mks_create_sandbox(): should be def mks_create_sandbox(self):
  • Use an IDE for example Ulipad .

Try put as the first line:

#!/usr/bin/env python

If you really need specific version of Python, setup your environment before running.

Possible problems:

  • your code is never executed (it's like you define the class only). Use it in the file (names are misleading):

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

  • show us how are you executing the code? Have you changed the permissions to be able to run the script?

    chmod +x filename.py

  • or are you trying to start it as:

    python filename.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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