943,640 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 865
  • Python RSS
Nov 21st, 2008
0

executing scripts via command line

Expand Post »
Hello!

I'm trying to execute a simple script in the command line, and for some reason, it's not working. Shouldn't it be as simple as this?

Python Syntax (Toggle Plain Text)
  1. python myscript.py

And yet when I try that, I get "Syntax Error: invalid syntax"

I've googled around, read through the docs--I have a feeling that this is probably something silly/small. Any help?

Thanks!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
payne_99 is offline Offline
2 posts
since Nov 2008
Nov 21st, 2008
0

Re: executing scripts via command line

The syntax error is somewhere in your script. Can you post the script (or attach it) ?
Reputation Points: 930
Solved Threads: 666
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Nov 21st, 2008
0

Re: executing scripts via command line

Ah, I see. So the actual command I was trying to execute was

Python Syntax (Toggle Plain Text)
  1. python controller.py

I checked controller.py, and got an error message:

Traceback (most recent call last):
File "C:\Users\p\controller.py", line 3, in <module>
exec "from " + sys.argv[1] + " import *"
IndexError: list index out of range

Here's the script:

Python Syntax (Toggle Plain Text)
  1. import sys
  2. from tichu import *
  3. exec "from " + sys.argv[1] + " import *"
  4.  
  5. def exampleFeatureFunction(hand):
  6. fv=[0, 0, 0]
  7. fv[0]=1
  8. if (hand[DRAGON]>0):
  9. fv[1]=1.0
  10. if (hand[PHOENIX]>0):
  11. fv[2]=1.0
  12. return fv
  13.  
  14. def alwaysSayNo(hand, model):
  15. return False
  16.  
  17. if __name__=="__main__":
  18. if (sys.argv[3]=="baseline"):
  19. evaluate(sys.argv[2], None, alwaysSayNo)
  20. elif (sys.argv[3]):
  21. evaluate(sys.argv[2], sys.argv[3], eval(sys.argv[4]))

There are other files (the "tichu" file it's importing from); let me know if you need to see more.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
payne_99 is offline Offline
2 posts
since Nov 2008
Nov 21st, 2008
0

Re: executing scripts via command line

Your problem is that sys.argv is what you supply on the command line after starting the script. So you would go
python Syntax (Toggle Plain Text)
  1. python mycode.py arg1 arg2
For yours to work you need at least 2 arguments for there to be sys.argv[1]
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: PythonFight
Next Thread in Python Forum Timeline: Passing varyables through a text file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC