【初心者向け】Python2系から3系に自由に変更する方法(Macユーザー用)
- 2020.10.18
- Python

本記事ではPython初心者用に向けてPythonのバージョン変更の方法を記載してます。
また2系から3系だけでなく、3系から2系にも変更可能ですので、ぜひ真似してみてください。
Python2系はサポートが終了しており、参考書や、ネットに載ってるソースコードのほとんどは3系で記載されております。
3系のコードを2系のPythonのバージョンで起動するとエラーも発生するので、初心者の方は必ず、Pythonのバージョンを確認することをお勧めします。
Python2系と3系とは?
Pythonは今から20年以上前に誕生したプログラミング言語です。今では、Youtubeや、Instagramなど有名なWebアプリもPythonで作成されています。
実はそんなPythonですが、2系と3系というバージョンがあります。
簡単にいうとiPhoneを使ってる人であれば簡単にイメージがつくと思うのですが、iOS14や13などのバージョンだと思ってもらうと想像できやすいかもしれません。
2系とはPythonコマンドプロントやエディタツールでpython –versionと入力した時に、最初に出てくる数字が2から始まる場合は2系、3系から始まる場合が3系と言われています
Pythonバージョン確認方法
1 2 |
python --version ###入力コマンド Python 3.7.8 ###バージョン記載 |
上記の1行目(###は不要)のコマンドを入力するとPythonのバージョンがわかるようになります。上記の場合はPython 3.7.8と表示されてますので3系になります。
Pythonバージョン変更
①Homebrewをインストール
Homebrewはソフトやライブラリを簡単にインストール&アンインストールできるmacOS用パッケージマネージャーです。
1 |
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" |
下記のようにパスワードが求められるので、Macのパスワードを入力
1 2 3 4 5 |
Warning: The Ruby Homebrew installer is now deprecated and has been rewritten in Bash. Please migrate to the following command: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" Password: |
②インストールされたか確認
下記コマンドを入力して確認
1 |
brew -v |
バージョンが記載されていてばインストールが確認できます
1 |
Homebrew 2.5.5 ###バージョン Homebrew/homebrew-core (git revision 537b0; last commit 2020-10-10) |
③pynevのインストール
pynevはPythonのバージョン管理ツールです。
これがあればバージョンを自由に切り替えることができるようになります。
下記コマンドを入力
1 |
brew install pyenv |
④bash_profileの編集
下記コマンドを入力
「.bash_profile」はbashシェル実行時に読まれる環境設定ファイルです。
これを編集しておかないと、Python3.xを入れても動いてくれません。
.bash_profileがあるか確認します。
1 |
ls -la |
下記のように色んなファイルが出てきますが、その中から確認します、
(アルファベッド順に並んでるので、上の方にあるはず。)
今回は10行目にあるのがわかります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
drwxr-xr-x+ 82 ★ユーザー名★ staff 2624 10 17 23:54 . drwxr-xr-x 6 root admin 192 11 24 2019 .. -r-------- 1 ★ユーザー名★ staff 7 1 8 2018 .CFUserTextEncoding -rw-r--r--@ 1 ★ユーザー名★ staff 22532 10 17 17:11 .DS_Store drwx------ 2 ★ユーザー名★ staff 64 10 17 23:54 .Trash drwxr-x--x 3 ★ユーザー名★ staff 96 11 17 2019 .adobe drwxr-xr-x 3 ★ユーザー名★ staff 96 10 11 15:46 .anaconda drwxr-xr-x 3 ★ユーザー名★ staff 96 10 5 23:21 .anaconda_backup -rw------- 1 ★ユーザー名★ staff 312 10 18 09:49 .bash_history -rw-r--r-- 1 ★ユーザー名★ staff 6591 10 18 09:49 .bash_profile -rw-r--r-- 1 ★ユーザー名★ staff 831 7 26 00:27 .bash_profile.pysave -rw-r--r-- 1 ★ユーザー名★ staff 12288 10 12 10:48 .bash_profile.swl -rw-r--r-- 1 ★ユーザー名★ staff 12288 10 11 02:07 .bash_profile.swm -rw-r--r-- 1 ★ユーザー名★ staff 12288 10 11 02:03 .bash_profile.swn -rw-r--r-- 1 ★ユーザー名★ staff 12288 10 11 01:59 .bash_profile.swo -rw-r--r-- 1 ★ユーザー名★ staff 12288 10 11 01:02 .bash_profile.swp -rw------- 1 ★ユーザー名★ staff 12288 10 11 02:08 .bash_profilevi.swp drwx------ 99 ★ユーザー名★ staff 3168 10 15 03:38 .bash_sessions |
下記コマンドを入力して編集モードに切り替え
1 |
vi .bash_profile |
編集モードに切り替えてから、の編集方法は下記です。
よく見ると最終行に (E)dit anyway, と記載があるので「E」で編集可能になります。
1 |
E325: ATTENTION Found a swap file by the name ".bash_profile.swp" owned by: ★ユーザー名★ dated: Sun Oct 11 01:02:57 2020 file name: ★ユーザー名★/.bash_profile modified: YES user name: ★ユーザー名★ host name: ★ユーザー名★.local process ID: 23236 While opening file ".bash_profile" dated: Sun Oct 18 09:49:41 2020 NEWER than swap file! (1) Another program may be editing the same file. If this is the case, be careful not to end up with two different instances of the same file when making changes. Quit, or continue with caution. (2) An edit session for this file crashed. If this is the case, use ":recover" or "vim -r .bash_profile" to recover the changes (see ":help recovery"). If you did this already, delete the swap file ".bash_profile.swp" to avoid this message. Swap file ".bash_profile.swp" already exists! [O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort: |
編集できるようにするためには「a」を入力すると編集できるようになるので下記の記述を先頭に入力。
1 |
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile echo 'eval "$(pyenv init -)"' >> ~/.bash_profile export PATH="$HOME/.pyenv/shims:$PATH" |
編集モードを終了するためは「esc」ボタンを押しその後「:wq」と入力で編集完了!!!
※この辺がまとめられてないので、初心者は必ず苦労します。
あとは編集内容をPCに反映させるために下記コマンド入力
1 |
source .bash_profile |
⑤ pyenvでPython3系をインストール
下記コマンドを入力してインストール可能なPythonのバージョンを見ます。
1 |
pyenv install -l |
下記のように大量の数字が出ます。これがPythonのバージョンリストです。
1 |
Available versions: 2.1.3 2.2.3 2.3.7 2.4.0 2.4.1 2.4.2 2.4.3 3.7.8 |
例えば、バージョン3.5.1を入れたい場合は下記を入力します
1 |
pyenv install 3.7.8 |
インスートールが終了したらPython3.7.8を実際に使える状態にします。
1 |
pyenv global 3.7.8 |
⑥最後に確認
最初に入力したPythonのバージョン確認コマンドを入力してバージョンを確認
1 |
python --version |
⑤でインストールしたPythonが出てこればバージョン変更完了です。
1 |
Python 3.7.8 |
お疲れ様でした〜
-
前の記事
投資報告(2020年8月分) 2020.09.20
-
次の記事
投資報告(2020年9月分) 2020.10.25
コメントを書く