[Tutor] Help with sys.argv
Hello, I am trying to learn how to use python with Facebook's Open Graph API. I am getting my feet wet with the following code authored by Matthew A. Russell. I copied it line for line for learning purposes, but I am getting the following error: Traceback (most recent call last): File "C:\Python27\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript exec codeObject in __main__.__dict__ File "C:\Python27\Scripts\facebook__query.py", line 20, in Q = sys.argv[1] IndexError: list index out of range Here is the code that this error is resulting from: #Querying the Open Graph for "programming" groups import sys import json import facebook import urllib2 from facebook__login import login try: ACCESS_TOKEN = open('C:\Users\Jon\Documents\FacebookApps\facebook.access_token').read() except IOError, e: try: #Page 283 in Mining... for notes ACCESS_TOKEN = sys.argv[1] Q = sys.argv[2] except: print >> sys.stderr, \ "Could not either find access token in 'C:\Users\Jon\Documents\FacebookApps\facebook.access_token' or parse args." ACCESS_TOKEN = login() Q = sys.argv[1] LIMIT = 100 gapi = facebook.GraphAPI(ACCESS_TOKEN) ... ... Clearly this has something to do with sys.argv[1], and I think I'm getting an IOError somewhere along the line. I have tried quite hard to figure this out on my own using the Python tutorial, Google, and the book in which the code was found to no avail. Please help! Thanks, Jon ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with sys.argv
On 01/-10/-28163 02:59 PM, monkey...@aim.com wrote: Hello, I am trying to learn how to use python with Facebook's Open Graph API. I am getting my feet wet with the following code authored by Matthew A. Russell. I copied it line for line for learning purposes, but I am getting the following error: Traceback (most recent call last): File "C:\Python27\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript exec codeObject in __main__.__dict__ File "C:\Python27\Scripts\facebook__query.py", line 20, in Q = sys.argv[1] IndexError: list index out of range Here is the code that this error is resulting from: #Querying the Open Graph for "programming" groups import sys import json import facebook import urllib2 from facebook__login import login try: ACCESS_TOKEN = open('C:\Users\Jon\Documents\FacebookApps\facebook.access_token').read() except IOError, e: try: #Page 283 in Mining... for notes ACCESS_TOKEN = sys.argv[1] Q = sys.argv[2] except: print>> sys.stderr, \ "Could not either find access token in 'C:\Users\Jon\Documents\FacebookApps\facebook.access_token' or parse args." ACCESS_TOKEN = login() Q = sys.argv[1] LIMIT = 100 gapi = facebook.GraphAPI(ACCESS_TOKEN) ... ... Clearly this has something to do with sys.argv[1], and I think I'm getting an IOError somewhere along the line. I have tried quite hard to figure this out on my own using the Python tutorial, Google, and the book in which the code was found to no avail. Please help! Thanks, Jon Usually, the first thing to do is to print sys.argv and see what it looks like. If it has only one element in it, then you'd expect that sys.argv[1] would give exactly that error. sys.argv[0] is the only element. Next, look up argv in the Python docs. Seehttp://docs.python.org/library/sys.html?highlight=argv#sys.argv for a description. In particular, if you didn't pass any arguments to the script, then argv will be of size 1, and you'll get the error you saw. Fix is to add a check for len(sys.argv) to be greater than or equal to however many arguments you need, and display an error and exit if not. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python 3.2 Install Not Responding To Python Command!!
Hello Rohan, I can't thank you enough but believe it or not, you guys helped me tremendously on the last posting. It was basically a symbolic link issue that was reeked havoc on my system. After reinstating the symbolic link to where I had it as before and deleting the file that the symbolic link created I was able to update, as I was not able to do this before, as a result of python being broken, and then adding the repository link graphically using the update manager!! Thanks so much for all your help, now I know how to do it through the Terminal!!! Sent from Nevins Duret's Mobile On Apr 28, 2011, at 4:26 AM, Rohan Sachdeva wrote: > I know this was a while ago but you can add the ppa by going to the terminal > and typing: > > sudo add-apt-repository ppa:irie/python3.2 > > then sudo apt-get update and sudo apt-get install python3.2 > > Rohan > > On Sun, Apr 10, 2011 at 1:33 PM, Walter Prins wrote: > > > On 9 April 2011 19:45, Nevins Duret wrote: > I can't thank you enough for your help. Yes, I usually use the Synaptic > Package Manager, however in this case, Python version 3.2 is not on the > Synaptic package Manager. This is why I chose to build it from source. As > far as what I think is causing this when I go in the > > No problem. > > Terminal and type: > > sudo update-alternatives --install /usr/bin/python/python3.2 python > /opt/py32/bin/python3.2 1 > > I get this as an error message: > > update-alternatives: renaming python link from /usr/bin/python to > /usr/bin/python/python3.2. > update-alternatives: warning: forcing reinstallation of alternative > /opt/py32/bin because link group python is broken. > update-alternatives: error: unable to make /usr/bin/python/python3.2.dpkg-tmp > a symlink to /etc/alternatives/python: No such file or directory > > I would expect that "update-alternatives" would have trouble (on the first > line) because /usr/bin/python is not a link anymore (but instead a folder) > based on your previous posts. > > So, please go and inspect the /usr/bin/python folder and see what if > anything's inside. If it's not empty you need to see what is in fact in it > and hopefully get rid of it or move it out of the way. Once it's empty, then > just delete the /usr/bin/python folder (you'll have to be root or use "sudo") > and afterwards either recreate a link to the default python interpreter > and/or try the update-alternatives command again. (To manually restore the > default python link, first execute "which python2.6" which should output > "/usr/bin/python2.6", then execute "sudo ln -s /usr/bin/python2.6 > /usr/bin/python" which should restore the link to the default python. After > doing this "update-alternatives" should have less trouble redirecting the > /usr/bin/python link. > > I however want to reiterate that your problems, especially fixing your OS, > will be better dealt with on the Ubuntu community forums. > > Rohan, thanks for posting about that PPA, wasn't aware of it! > > Walter > > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor