LoginSignup
7
8

More than 3 years have passed since last update.

anyenv、pyenvでPythonバージョン管理、仮想環境作成、仮想環境でIDLE起動(Mac)

Last updated at Posted at 2019-08-11

anyenv、pyenvのインストール

参照:anyenv と nodenv で node.js バージョン管理、設定後に gulp の設定を修正(Mac)

anyenvでpyenvをインストールし、シェルを再起動

$ anyenv install pyenv
$ exec $SHELL -l

anyenvのバージョンを確認

$ pyenv -v
pyenv 1.2.11

pyenvでインストールできるPythonのバージョンを確認

$ pyenv install --list
Available versions:
  2.1.3
  2.2.3
  2.3.7
  2.4.0
  2.4.1
  2.4.2
  2.4.3
  2.4.4
  2.4.5
  ...

pyenvで Python 3.6.8をインストール

$ pyenv install 3.6.8

インストールしたPythonのバージョンを確認

$ pyenv versions

バージョンの切替 グローバル

$ pyenv global 3.6.8

バージョンの切替 ローカル

$ pyenv local 3.6.8

Pythonのバージョンを確認

$ python --version

Python仮想環境の作成

仮想環境を作成するディレクトリに移動し、venvで仮想環境を作成

$ cd <仮想環境を作成するディレクトリ>
$ python3 -m venv <仮想環境名> 

例: 仮想環境名「testenv」の場合

$ python3 -m venv testenv 

venv は、公式にPython3.3以上に追加されている仮想環境作成サポートのモジュール

作成した仮想環境の中に入る

$ cd <仮想環境名>
$ cd testenv

仮想環境の有効化

$ source bin/activate

Terminalの先頭に (testenv) のように仮想環境名が表示される

仮想環境でIDLEを起動

IDLE はPythonの統合開発環境、学習用環境

$ python -m idlelib.idle

IDLEを閉じる

(ショートカット ⌘+W)

IDLE起動時に以下警告が表示される場合

WARNING: The version of Tcl/Tk (8.5.9) in use may be unstable. Visit
http://www.python.org/download/mac/tcltk/ for current information.
WARNING: The system preference "Prefer tabs when opening documents" is set to
"Always". This will cause various problems with IDLE. For the best experience,
change this setting when running IDLE (via System Preferences -> Dock).

メッセージのとおり
- Macの「システム環境設定」で「Dock」を開き、「書類を開くときはタブで開く:常に」 になっていたら他の項目に変更する
- the ActiveState web site から ActiveTcl 8.6 をインストール(インストール後にPython の再インストールが必要)する

Python を再インストール

$ pyenv uninstall 3.6.8
$ brew info tcl-tk #バージョンが8.6以上か確認
$ pyenv install 3.6.8

仮想環境を無効化

$ deactivate

参考:
How to launch python Idle from a virtual environment (virtualenv)

7
8
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
7
8