Trouble setting Python version in Sublime Text2

2019-01-14 10:04发布

问题:

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"
    }