python cannot find module when using 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 na开发者_JS百科me>
then run those commands
which python
echo $PYTHONPATH
echo $PYTHONUSERBASE
echo $LD_LIBRARY_PATH
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')
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
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>'
Had the same issue and tried everything above.
What worked for me was changing the remote commands from python to the full path.
i.e.
1) SSH onto your remote, find out where your python is located via
2) change your command from python pyscript.py
to remote/path/to/python pyscript
Check and make sure that you are using the same python environment. You may be using one like /usr/bin/python when you log in, and perhaps using a different install such as /usr/local/bin/python when you are trying to run remotely.
精彩评论