I am not able to run a system call using PyCharm and can't figure out what variables or environment settings to change.
Given this simple script:
import os
cmd = 'ifconfig -a'
os.system(cmd)
...which runs fine at the command line in terminal, yields the following error:
sh: ifconfig: command not found
This is happening with really any process I'm trying to run such as CSVSQL, PSQL, etc.
I have tried: Displaying my python interpreter paths dispayed at the command line, I tried adding them to the PyCharm interpreter paths, to no avail.
There are several other threads out there describing similar problems, but there doesn't seem to be a good solution that I have come across.
I'm running Linux Mint 19, though this works on my Windows installation (PATH output is much different).
My apologies if this is really simple... Thank you!
Run printenv
on both Python and terminal, and check the PATH
variable. Use os.environ['PATH'] = 'My path'
to set it to what you saw on the terminal.
For future issues (That I've run into):
A quick way to check if it's an exported environment variable is to run os.system("/bin/sh -c \"MYCMD\"")
, and then run the same "/bin/sh -c \"MYCMD\""
string in your terminal. If there's still a problem, then it must be an export (And this is the likely issue).
To resolve this, try printenv
in both python and the terminal to see the list of exports. You should see a discrepancy. The format is simple as you can simple copy the output of the terminal's printenv
(Which should be a series of declare
s), and paste it into python so python will get the same variables. Then your "/bin/sh CMD" calls should align.
The wrapped /bin/sh
is in case they're running different shells or have different local variables. echo $SHELL
can confirm this, at which point you can compare set
s and printenv
s and copy paste in the same way. Wrapped you only have to compare exports, as that's what get passed to child processes.