LoginSignup
5
4

More than 5 years have passed since last update.

Sublime Text で pythonbrew

Last updated at Posted at 2012-08-27

==========================

Sublime TextのPluginのPythonのバージョンは2.6.7なのでバージョンを3.2や2.7などから切替えて使いたいです。

Macでversion_info.pyをcommand+bでBuildすると2.7.1のバージョンが表示されます。

version_info.py
import sys
print sys.version_info
sys.version_info(major=2, minor=7, micro=1, releaselevel='final', serial=0)

pythonbrewのpathがSublime Textに通っていないのでバージョンの切替が反映されませんので、少しpythonbrewに手をいれて対応させます。

pythonbrewのバージョンの切替でシンボリックリンクを作成

pybrewのswitchとuseでバージョンを切替えたときにカレントバージョンのシンボリックリンクを作ります。
run_commandの最後に追加します。(switchとuseは同じコードです。)

~/.pythonbrew/scripts/pythonbrew/commands/switch.py
def run_command(self, options, args):

  # (略)

  path = os.path.abspath(os.path.join(PATH_PYTHONS, '..', 'current'))
  if os.path.isdir(path):
      os.unlink(path)
  os.symlink(os.path.abspath(os.path.join(PATH_PYTHONS, pkgname)), path)
~/.pythonbrew/scripts/pythonbrew/commands/use.py
def run_command(self, options, args):

  # (略)

  path = os.path.abspath(os.path.join(PATH_PYTHONS, '..', 'current'))
  if os.path.isdir(path):
      os.unlink(path)
  os.symlink(os.path.abspath(os.path.join(PATH_PYTHONS, pkgname)), path)

Sublime Textにpathを設定

PackagesのPythonのPython.sublime-buildにpathを追加します。
(userは任意に変更して下さい。)

Python.sublime-build
{
  "cmd": ["python", "-u", "$file"],
  "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
  "selector": "source.python",
  "path": "/Users/user/.pythonbrew/current/bin:/usr/bin:/bin:/usr/sbin:/sbin"
}

pythonbrewでバージョンを切替えてバージョンを確認

まずはuseで確認

$ pybrew use 2.7.2

command+bでバージョンを確認すると2.7.2が表示されます。

sys.version_info(major=2, minor=7, micro=2, releaselevel='final', serial=0)

つぎにswitchで確認

$ pybrew switch 2.6.7

command+bでバージョンを確認すると2.6.7が表示されます。

(2, 6, 7, 'final', 0)

バージョンの切替がSublime Textに反映されるようになりました。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4