7

I'm a completely new user to Python and shell scripts, and have run into a dead end with this, even after Googling the issue and banging my head against the desk a lot. Any help is appreciated!

I'm running Python 2.7.3 on a shell that I SSH into; I downloaded some code to run a few programs/analyses. When I execute the initial program, I get the following error:

    Traceback (most recent call last):
    File "./[script1].py", line 7, in <module>
    import [script1]
    File "[directory]/[script].py", line 22, in <module>
    import gdata.spreadsheet.service
    ImportError: No module named gdata.spreadsheet.service

[Script 1] refers to a python script in the same folder that came as part of the code package, and it also calls the Google Data python package, which I've downloaded to the same folder and have gunzipped, tar unpacked, and then installed (with ./configure, etc.) Based on looking up the errors, my best guess is that there's something wrong with the PYTHONPATH here, and it's not finding [script1].py and the Gdata folder, even though both are within the same directory as the script I'm running. "Echo $PYTHONPATH" tells me that it's an undefined variable, and there's also a blank init.py file within the directory. There's no files containing the word "bash" or "bashrc" anywhere within that directory. Likewise, I can't seem to find any "sys.path" files, although when I boot up Python and print(sys.path) I get the resulting output:

['', 
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PIL',
'/usr/lib/python2.7/dist-packages/gst-0.10',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
'/usr/lib/python2.7/dist-packages/ubuntuone-client',
'/usr/lib/python2.7/dist-packages/ubuntuone-installer',
'/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol',
'/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']

I've also tried

export PYTHONPATH=[directory]

in my shell, but it spits out "export: command not found".

Please forgive a newcomer to all this - any help on this (whether or not my suspicions are correct, and how to resolve them) would be greatly appreciated!

Morgan
  • 19,934
  • 8
  • 58
  • 84
user2152303
  • 73
  • 1
  • 1
  • 4
  • What shell are you using? The syntax to set variables is different in each shell. PYTHONPATH is normally undefined. – Eric Urban Mar 09 '13 at 20:18
  • That doesn't look like the full output of the program. – Croad Langshan Mar 09 '13 at 20:18
  • Eric, I'm running CygWin to ssh into a cluster on my university's servers - is there a way to find out which shell from there? – user2152303 Mar 09 '13 at 20:23
  • And Croad, that's the full output of the program - it spits out "Traceback: most recent last call:" followed by the errors I pasted on there. Thanks! – user2152303 Mar 09 '13 at 20:24
  • You could modify Python's `sys.path` instead of fighting with an unknown shell. – Apalala Mar 09 '13 at 20:28
  • user2152303: your last comment is self-contradictory. The important information that I suspect is missing is the final line of the traceback, which says what the exception type and exception detail was. – Croad Langshan Mar 09 '13 at 20:30
  • Hi Croad, that's the entire output, copied from my shell: Traceback (most recent call last): File "[file]", line 7, in import [file] File "[directory]/file", line 22, in import gdata.spreadsheet.service ImportError: No module named gdata.spreadsheet.service and then it goes back to the shell prompt. Thanks! – user2152303 Mar 09 '13 at 20:33
  • Apalala, thanks! How do I do that in the shell here? Sorry for being so clueless! – user2152303 Mar 09 '13 at 20:34
  • 1
    Edit your answer to include the full output, don't paste it in a comment because that is hard to read. – Croad Langshan Mar 09 '13 at 20:42
  • OK, sorry - on there now! – user2152303 Mar 09 '13 at 20:52
  • Note that ``sys.path`` is not a file, but "list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default." (from http://docs.python.org/2/library/sys.html?highlight=sys.path#sys.path). I hope that helps clarifying some things :) – Balthazar Rouberol Mar 09 '13 at 21:01
  • 1
    Thanks! So is sys.path equivalent to PYTHONPATH? If so, then I directly modify sys.path[0] and set it equal to my current directory with all the scripts? – user2152303 Mar 09 '13 at 22:06

1 Answers1

8

Setting PYTHONPATH

By the output of the export command you tried, it looks like the shell you are using is not bash. This post covers some ways on how to find out which shell you are on. After finding out your shell, you can find out how to set environment variables (PYTHONPATH) in that shell.

You might also try these to set the PYTHONPATH for the duration of running your script (the last one should work on (T)CSH):

PYTHONPATH=your_directory python script_name

and

env PYTHONPATH=your_directory python script_name

Testing that the PYTHONPATH you set works

To see that PYTHONPATH really gets set and works within Python, instead of running the script like above with python script_name, use python -c 'import os; print os.getenv("PYTHONPATH")'. It should display the PYTHONPATH you just set.

Likewise, printing sys.path in Python interpreter should output the path in PYTHONPATH as one of the entries.

If PYTHONPATH is set correctly

If you successfully set your PYTHONPATH and the problem persists, try running the Python interpreter from the path you have gdata in.

cd path_which_has_subdirectory_gdata
python

In Python interpreter, try importing the gdata module:

import gdata

If that works, try also importing the module that causes the ImportError:

import gdata.spreadsheet.service

If these imports work from Python interpreter, there's probably something wrong with your [script1]. If not, try to confirm that gdata module really is where you think it is; the correct directory for the module should contain a file named __init__.py and PYTHONPATH should be set to point to the directory above the module in hierarchy.

Community
  • 1
  • 1
miikkas
  • 818
  • 1
  • 8
  • 25
  • 1
    Hi Miikkas, Wow, thank you so much! That helps a lot. Based on your response, I found that I'm on TCSH. Using 'env PYTHONPATH' I set PYTHONPATH, and confirmed it with the suggested code above and 'echo env $PYTHONPATH'. However, when I run the program it returns the same error. Right now I'm directing PYTHONPATH to the folder which contains 'gdata'. When I 'cd directory_with_gdata_and_other_python_module', boot up python, and type 'import gdata' or 'import [module2]', I still get 'ImportError: No module named gdata' or [module2]. Am I setting the PYTHONPATH to the wrong thing now? Thanks!!! – user2152303 Mar 10 '13 at 00:01
  • Hi, glad to have been of some help. :) Seems like you are setting the PYTHONPATH correctly now and there's some kind of problem with your gdata module. What are the contents of the gdata directory? Does it contain `__init___.py`? Python detects modules on the basis of the module directories containing an `__init___.py` file. – miikkas Mar 10 '13 at 00:28
  • There's a (blank) __init__.py file located in the folder, but it's a couple folders deep within gdata. I've tried setting the PYTHONPATH to that subfolder too within gdata, with the same result. Thanks! – user2152303 Mar 10 '13 at 00:50
  • 2
    PYTHONPATH should point to the path just above the one containing `__init__.py`, i.e. if the `__init__.py` file is in /foo/dir1/dir2, PYTHONPATH should be set to /foo/dir1 accordingly. Hope this helps! – miikkas Mar 10 '13 at 01:59
  • OK, that seems to have solved the issue with gdata - running into other issues, but this is fixed. Thanks so much!!! – user2152303 Mar 10 '13 at 20:36