Why is $PATH in remote deployment path different from $PATH in remote system?

Why is $PATH in remote deployment path different from $PATH in remote system?



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


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



I created project in Pycharm and set remote python Interpreter as miniconda3 python, it works well when I just run some *.py files.


*.py



But when I typed some os.system() lines, weird things happened.

For instance, in test.py from Pycharm project


os.system()


test.py


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)


os.system('export PATH="$PATH:$HOME/miniconda3/bin"')


test.py


$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.


os.environ["PATH"] += ":/home/woosung/miniconda3/bin"


*.py


import


ImportError



For instance, in test.py


test.py


import matplotlib

os.environ["PATH"] += ":/home/woosung/miniconda3/bin"
os.system("python import_test.py")



and import_test.py


import_test.py


import matplotlib



And when I run test.py,


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.





os.system('export PATH="$PATH:$HOME/miniconda3/bin"') doesn't actually do anything at all, because it runs a shell process, the shell sets its own environment variable, and when the shell exits the path of the parent process is unmodified. If you want to adjust the environment in a Python script, see os.environ.
– Dietrich Epp
Aug 25 at 2:01


os.system('export PATH="$PATH:$HOME/miniconda3/bin"')


os.environ




1 Answer
1



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').


os.system("python import_test.py")


os.system(sys.executable + ' import_test.py')



This makes the shell uses the Pycharm remote interpreter(miniconda3), not original.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)