Trouble setting Python version in Sublime Text2

随声附和 提交于 2019-12-17 15:59:23

问题


I'm having trouble setting the build environment in Sublime Text2. I'm using Macports for Python and package installation.

My python.sublime-build file looks like this:

{
    "cmd": ["python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

I think (from searching) that I need to modify the "cmd" line to point to the Macports version. Anyone done this successfully?

From Terminal, everything builds/runs fine, it's only Sublime Text2's build that's grabbing the system version still.

Additional info:

which python
/opt/local/bin/python

Thanks for any help.


回答1:


Your Sublime Text 2 environment is different from your shell environment; the $PATH variable is probably not pointing to the same directories and the wrong executable is selected.

You have several options to work around that:

  1. Set a "path" option that includes /opt/local/bin need to use an absolute path for your python executable, as Sublime Text 2 doesn't share the same environment PATH variable your shell uses. Inspect echo $PATH and use that as a template:

    {
        "cmd": ["python", "-u", "$file"],
        "path": "/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/Current/bin:/Users/yourloginname/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin",
        "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
        "selector": "source.python"
    }
    

    That'll use /bin/bash to run the command, which I think would have the same $PATH setting as your terminal.

  2. Run the command through the shell instead by setting the "shell" option:

    {
        "cmd": ["python", "-u", "$file"],
        "shell": true,
        "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
        "selector": "source.python"
    }
    
  3. Use the full path for the python command:

    {
        "cmd": ["/opt/local/bin/python", "-u", "$file"],
        "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
        "selector": "source.python"
    }
    


来源:https://stackoverflow.com/questions/12342004/trouble-setting-python-version-in-sublime-text2

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