I’m currently working on Pycharm with remote python Interpreter(miniconda3/bin/python).
So when I type echo $PATH
in remote server, it prints
/home/woosung/bin:/home/woosung/.local/bin:/home/woosung/miniconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
I created project in Pycharm and set remote python Interpreter as miniconda3 python, it works well when I just run some *.py
files.
But when I typed some os.system()
lines, weird things happened.
For instance, in test.py
from Pycharm project
import os os.system('echo $PATH') os.system('python --version')
Output is
ssh://woosung@xxx.xxx.xxx.xxx:xx/home/woosung/miniconda3/bin/python -u /tmp/pycharm_project_203/test.py /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games Python 2.7.12 Process finished with exit code 0
I tried same command in remote server,
woosung@test-pc:~$ echo $PATH /home/woosung/bin:/home/woosung/.local/bin:/home/woosung/miniconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin woosung@test-pc:~$ python --version Python 3.6.6 :: Anaconda, Inc.
PATH and the version of python are totally different! How can I fix this?
I’ve already tried add os.system('export PATH="$PATH:$HOME/miniconda3/bin"')
to test.py
. But it still gives same $PATH
.(/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
)
EDIT
Thanks to the comment of @Dietrich Epp, I successfully add interpreter path to the shell $PATH.
(os.environ["PATH"] += ":/home/woosung/miniconda3/bin"
)
But I stuck the more basic problem. When I add the path and execute command the some *.py
file including import
library which is only in miniconda3, the shell gives ImportError
.
For instance, in test.py
import matplotlib os.environ["PATH"] += ":/home/woosung/miniconda3/bin" os.system("python import_test.py")
and import_test.py
import matplotlib
And when I run test.py
,
Traceback (most recent call last): File "import_test.py", line 1, in <module> import matplotlib ImportError: No module named matplotlib
Looks like the shell doesn’t understand how to utilize modified $PATH.
Advertisement
Answer
I find the solution.
It is not direct but quite simple.
I changed os.system("python import_test.py")
to os.system(sys.executable + ' import_test.py')
.
This makes the shell uses the Pycharm remote interpreter(miniconda3), not original.