OSX + Eclipse + PyDev - PATH isn't correct

坚强是说给别人听的谎言 提交于 2019-12-08 05:41:51

问题


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)

回答1:


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




回答2:


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)




回答3:


You can find the internal PyDev preferences under:

Window:Preferences:PyDev:Interpreter - Python

This includes the system PYTHONPATH.




回答4:


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)



来源:https://stackoverflow.com/questions/12050073/osx-eclipse-pydev-path-isnt-correct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!