0

I have a rails 4 app on AWS EC2 Ubuntu. It will call a python program using backticks:

    pyresult = `python /path/to/abc.py`

The python programe works perfect on my local dev. But it failed on the production server, with no error. (pyresult is empty)

I've debugged for long time, and found a possible cause: $PYTHONPATH

On local, i have the follow in my .bashrc, as the python program asked to add:

    export PYTHONPATH="$HOME/.local/lib/python2.7/site-packages/:$PYTHONPATH"

On server, i also added them in .bashrc on server. But run echo $PYTHONPATH in my app gives me empty string.

Then I added explicitly in my rails app,

    `export PYTHONPATH="$HOME/.local/lib/python2.7/site-packages/:$PYTHONPATH"`
    pyresult = `python /path/to/abc.py`

but $PYTHONPATH is still empty. and calling python program still failed with no error.

Is it correct to guess the missing $PYTHONPATH caused the issue? if yes, how to solve the problem? Thanks for helping.(sorry i'm new to linux and not familiar with python)

Narco
  • 36
  • 8
  • This may be answered in [another](http://stackoverflow.com/q/11648620/1563512) stackoverflow answer. Did you try `ENV['PYTHONPATH'] = "/some/path:/some/otherpath";`? – zerodiff Sep 04 '14 at 16:18

2 Answers2

0

Linux doesn't have system variables, only environment variables, which are only inherited by child processes. This line

`export PYTHONPATH="$HOME/.local/lib/python2.7/site-packages/:$PYTHONPATH"`

only sets PYTHONPATH for the subshell started by the backquotes (and any processes started from that subshell). Once the shell exists, the setting for PYTHONPATH is gone.

You need to use whatever facility Ruby has for modifying the environment of the current process, which I gather (as someone who doesn't use Ruby) is

ENV['PYTHONPATH'] = '...' 

Then the shell started by

pyresult = `python /path/to/abc.py`

will inherit its PYTHONPATH value from the current Ruby process.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thank you for the reply. I've tried but it's not working. ENV[] is for ruby app but not the subshell started by the backticks. – Narco Sep 05 '14 at 09:50
  • In the shell, you should simply be able to access `$PYTHONPATH` as a variable. If you expect to change `PYTHONPATH` in the subshell *and* see that change in the Ruby app, that's impossible. – chepner Sep 05 '14 at 13:10
  • Hi Chepner, I've used spawn({"PYTHONPATH"=>"/my/path"},command}) to include PYTHONPATH in the subshell, and it will have PYTHONPATH as the environment variable. But somehow it's still not working. Looks some other issue =,= Tks for helping. – Narco Sep 07 '14 at 06:29
0

After using Open4 gem to run the command, I found the rootcause is paython programe can't find the lib, which is installed via setup.py. So it's another issue.

Narco
  • 36
  • 8