Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python cannot find module when using ssh

Tags:

python

ssh

I'm using python on servers. When I run a python command which needs numpy module, if I do

ssh <server name> <python command>

that server will complain no module named numpy found.

However, if I first ssh to that server by

ssh <server name>

then run that python command on that server

<python command>

everything will be ok.

This means that server has already been installed numpy module, and it just cannot find the module without my logging on it.

Any guess of what the problem could be?

Thanks

Add:

sorry for forgetting to mention that, the result I got by running

ssh <server name> which python
ssh <server name> echo $PYTHONPATH
ssh <server name> echo $PYTHONUSERBASE
ssh <server name> echo $LD_LIBRARY_PATH

are all the same as when I first ssh to the server

ssh <server name>

then run those commands

which python
echo $PYTHONPATH 
echo $PYTHONUSERBASE
echo $LD_LIBRARY_PATH
like image 473
qingkunl Avatar asked Jul 27 '11 21:07

qingkunl


3 Answers

Yes. It also means that your user's .bashrc has something specific in it which modifies $PATH to allow you to access extra modules. I don't like modifying path on a global level, personally, so I'll suggest the Python approach: call sys.path.append('/path/to/numpy')

like image 117
cwallenpoole Avatar answered Oct 03 '22 10:10

cwallenpoole


I found the problem. It is truly the problem of python path.

And the reason why I didn't find this is that, instead of doing

ssh <server name> echo $PYTHONPATH

to find all the pathes python searches modules

we should do

ssh <server name> 'echo $PYTHONPATH'

we cannot ignore the quote to check variable PYTHONPATH on server

like image 26
qingkunl Avatar answered Oct 03 '22 12:10

qingkunl


When running cmd over ssh in one line, the .profile is not read. To test, try this:

ssh host env

Use this instead to fix this issue (quotes are compulsory):

ssh host '. ~/.profile; cmd'

eg:

ssh <server name> '. ~/.bashrc; <python command>'
like image 21
sp1111 Avatar answered Oct 03 '22 12:10

sp1111