Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing PYTHONPATH in shell

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!

like image 307
user2152303 Avatar asked Mar 09 '13 20:03

user2152303


1 Answers

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.

like image 178
miikkas Avatar answered Oct 14 '22 03:10

miikkas