Forums

Gspread

Is it possible to import and use the gspread module while running a console in pythonanywhere?

You should be able to install gspread to your user account using:

pip install --user gspread

I am unfamiliar with the pip install. Is there any type of walkthrough I can read? Or where would I write this?

Never mind, i figured it out. Thank you so much!

But now, my program appears to have trouble logging into my google drive account. Any help there?

What's the error?

The error message says this:

Traceback (most recent call last):
File "/home/hopkinscj/drqtest.py", line 534, in <module>
main()
File "/home/hopkinscj/drqtest.py", line 496, in main
gc = gspread.login('Username', 'Password')
File "/home/hopkinscj/.local/lib/python2.7/site-packages/gspread/client.py", line 278, in login
client.login()
File "/home/hopkinscj/.local/lib/python2.7/site-packages/gspread/client.py", line 99, in login
raise AuthenticationError("Unable to authenticate. %s code" % ex.code)
gspread.exceptions.AuthenticationError: Unable to authenticate. 403 code

Do you have 2-factor authentication activated on your Google account? Are you using your full email address or just the username portion? Have you successfully authenticated to your account with gspread on your home machine or elsewhere?

Also, it looks like gspread uses the ClientLogin API which is deprecated as of April 2012 - looks like the author is keen to switch to OAuth2 but hasn't done yet.

I am using my full email login and i have successfully authenticated on my home machine. How does one go about activating 2-factor authentication on a google account?

You can read more about Google's 2-factor authentication on their site. The reason I mentioned it was that if you use it then you need to generate application-specific passwords for applications which haven't been updated to support Google's 2-factory auth. This generally includes older applications and those for which it's not reasonable to pop up Google's web page to request the verification code.

Personally I use it for security, but you need to live with using application-specific passwords for quite a lot of things (such as external email and calendar clients). Since you haven't currently activated it on your account, however, that can't be the cause of this issue.

The only possibility I can think of is if the login process is sending a redirect back for a URL which isn't in the PA whitelist, and that request is being refused. I'd be quite surprised if that was resulting in a 403, however.

A 403 error is no longer occurring, but now i receive this error message:

Traceback (most recent call last):
File "/home/hopkinscj/DRQInput.py", line 569, in <module>
main()
File "/home/hopkinscj/DRQInput.py", line 540, in main
cell_list = wks.range('A2:H200')
File "/home/hopkinscj/.local/lib/python2.7/site-packages/gspread/models.py", line 304, in range
'return-empty': 'true'})
File "/home/hopkinscj/.local/lib/python2.7/site-packages/gspread/client.py", line 217, in get_cells_feed
r = self.session.get(url)
File "/home/hopkinscj/.local/lib/python2.7/site-packages/gspread/httpsession.py", line 65, in get
return self.request('get', url, **kwargs)
File "/home/hopkinscj/.local/lib/python2.7/site-packages/gspread/httpsession.py", line 62, in request
raise e
urllib2.HTTPError: HTTP Error 400: Bad Request

how can i fix this?

Hmm, according to this page a 400 error normally means that you're not logged in (or the spreadsheet is not public, but presumably if you are logged in then you should be able to access your own spreadsheets even if they're not public.

So perhaps you're not really getting logged in correctly?

That might be the case, but when i run this exact same program from Idle using Python 2.7, nothing goes wrong at all. I also just logged in to my google docs and tried again, but the same error occurred.

You can see recent security-related activity on your Google account by logging in to Google and then visiting here and here There may be some clues there.

When i checked the first link, there was one suspicious sign in attempt, but that was from here, so i flagged it as me. I tried running the program from here again, and i got the same error. Is there anyway i can allow access from this site?

Is there possibly another way to access my google docs spreadsheets?

What happens if you make one of your spreadsheets public? Would that skip the authentication code? That would at least tell us whether the problem really is with authentication...

After I looked into it a little more, I figured out the problem. The cell_list i was assigning to it was greater than the amount of cells in the spreadsheet, so after I added more cells in the spreadsheet, the program began running fine. Thanks for the help though!

Excellent, glad you worked it out and thanks for sharing the solution!

I am also wanting to use gspread but I'm stuck at the pip install. I tried it from my home directory and my web2py directory but am getting the following output in the bash console: 17:17 ~ $ pip install SIRI gspread Downloading/unpacking SIRI Cannot fetch index base URL https://pypi.python.org/simple/ Could not find any downloads that satisfy the requirement SIRI Cleaning up... No distributions at all found for SIRI Storing complete log in /home/SIRI/.pip/pip.log

thanks in advance for the help.

You've included your username in the install. The --user is supposed to be a literal --user. If you type the install line from earlier in the thread, you may have more luck.

Ok, I copied and pasted the line from above and got the same message..

nevermind, I was still in my web2py directory. Tried it from home and it worked. Thanks.

Ok, sorry ran into another issue. It installed successfully but now says <type 'exceptions.ImportError'> Cannot import module 'gspread'

I'm using web2py and I put the import statement (import gspread) in my db.py model. I am testing gdata in the same model and that import is working properly. I'm kind of a noob so I'm not sure what's wrong with my import.

I guess it just took a few minutes to update or something because it is working without a problem now. Sorry for the multiple posts.

No problem, glad it's working now!

No module named gspread. ModuleNotFound error. I did the install in the home dir, requirement already fulfilled or something. Unable to import gpread

Make sure that you're trying to import it in the same version of Python/virtualenv as where you installed it.