1

Trying to get the following script working on OSX using Eclipse and PyDev (Debug):

#------------------------------------------------------
import os, subprocess
from os.path import join as join_path

def cmd(command):
    print('$ ' + command)

    process = subprocess.Popen(command, shell=True, executable="/bin/bash", stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    return process.communicate() + (process.returncode,)


stdout, stderr, error_code = cmd('echo $PATH')  
print(stdout, stderr, error_code)

stdout, stderr, error_code = cmd('echo $PYTHONPATH')  
print(stdout, stderr, error_code)

stdout, stderr, error_code = cmd('which python')  
print(stdout, stderr, error_code)

stdout, stderr, error_code = cmd('which apt-get')  
print(stdout, stderr, error_code)
#------------------------------------------------------

but $PATH is not what I have set in .bashrc or .profile, and I cannot run apt-get, which is in sw/bin. It seems that my $PATH is getting overwritten or not set correctly when running subprocess.Popen.

Here is my output from the above script:

$ echo $PATH 
('/usr/bin:/bin:/usr/sbin:/sbin\n', '', 0) 
$ echo $PYTHONPATH ('/Applications/eclipse/plugins/org.python.pydev_2.6.0.2012062515/pysrc/pydev_sitecustomize:/Users/bryancdickson/Development/Lootsie/_repos/ap/ap:/Users/bryancdickson/Development/Lootsie/_repos/ap/ap/ap:/sw/bin:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages:/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload:/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC:/Library/Python/2.7/site-packages\n', '', 0) 
$ which python 
('/usr/bin/python\n', '', 0) 
$ which apt-get
> ('', '', 1)
codeape
  • 97,830
  • 24
  • 159
  • 188
dixkin
  • 811
  • 2
  • 11
  • 20

4 Answers4

2

PyDev probably sets up $PATH independent of the settings in your bash configuration files.

I suggest you either:

Start PyDev from a bash shell that has the correct PATH settings (I assume Eclipse will the inherit the environment from the shell).

or

Explicitly configure PATH within Eclipse (I don't know the details on how to do that, search the docs for "environment variables").

codeape
  • 97,830
  • 24
  • 159
  • 188
1

OK, figured out a simple solution to the problem. Found a number of good notes here: Environment variables in Mac OS X

Starting Eclipse from a terminal - though annoying - works fine. From my terminal type /Applications/eclipse/eclipse and my environment variables get picked up:

$ echo $PATH ('/sw/bin:/sw/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/mysql/bin:sw/bin:/usr/local/mysql/bin:sw/bin:/usr/X11R6/bin\n', '', 0) $ echo $PYTHONPATH ('/Applications/eclipse/plugins/org.python.pydev_2.6.0.2012062515/pysrc/pydev_sitecustomize:/Users/bryancdickson/Development/Lootsie/_repos/ap/ap:/Users/bryancdickson/Development/Lootsie/_repos/ap/ap/ap:/sw/bin:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages:/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload:/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC:/Library/Python/2.7/site-packages:/sw/bin\n', '', 0) $ which python ('/usr/bin/python\n', '', 0) $ which apt-get ('/sw/bin/apt-get\n', '', 0)

Community
  • 1
  • 1
dixkin
  • 811
  • 2
  • 11
  • 20
0

You can find the internal PyDev preferences under:

Window:Preferences:PyDev:Interpreter - Python

This includes the system PYTHONPATH.

Conor
  • 1,028
  • 1
  • 8
  • 15
  • I found Eclipse->Preferences->PyDev->Interpreter - Python. Is that what you mean? I added /sw/bin to the Libraries->System PYTHONPATH list, but it didn't change anything. And if you look closely at the echo command above, /sw/bin is actually there, which has me very confused. – dixkin Aug 21 '12 at 17:41
0

Launch Eclipse from the Terminal worked for me with Eclipse 4.3, but it doesn't work anymore with Eclise 4.5 (Mars)

The problem is that the PATH variable is not correctly set when calling subprocess.Popen

A workaround is to add the real PATH into the environment using os.environ

import os
os.environ['PATH'] = os.environ['PATH']+':'+os.getenv('PATH')

This works for me (I just had to add the variable PATH and its value in the Environment of the Python Interpreter, see the Preferences (Preferences -> PyDev -> Interpreters -> Python Interpreter -> Environment)

ThibThib
  • 8,010
  • 3
  • 30
  • 37