在 Mac 上用 pyenv 輕鬆安裝 Python3 (可直接支援安裝多種版本)

Install Python with Pyenv

隨著時代的進步, Python 2.7 大概也要跟這個時代說再見了。想當初是給自己筆記用的文章,流量意外的很高,就直接更新在這裡了。若有更好的方法也希望大家留言分享唷!

Table of Contents

Step 1 安裝 Xcode Command Line Tool

打開 terminal 輸入,第一次安裝的時候可能會跳出要同意他的 License Agreement 之類的東西。

xcode-select --install

Step 2 安裝套件管理工具 Homebrew

Homebrew

不想移動的人兒可以直接複製以下指令貼到 terminal 裡面

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

安裝好後可以跑一下

brew doctor

如果出現

Your system is ready to brew.

代表萬事 OK。而有出現 Warning 的話:

Please note that these warnings are just used to help the Homebrew maintainers with debugging if you file an issue. If everything you use Homebrew for is working fine: please don’t worry or file an issue; just ignore this. Thanks!

也不用太擔心,可以按照裡面的步驟去修正就好囉!不過還是建議大家把東西裝好,才不會到時候忘記自己到底什麼東西還沒設定好。

Step 3: 安裝 Pyenv

接下來要正式進入安裝 Python 的步驟了!

首先,在 terminal 裡輸入

python --version

天哪!都還沒開始裝 Python,電腦裡面怎麼已經有了?
這是 Mac 系統要使用的 Python,所以平常沒事不要去亂動比較好唷!
所以現在我們要用 Pyenv 來安裝平常可以使(亂)用(搞)的 Python。

最簡單的方式是使用 Pyenv Installer,在 terminal 輸入以下指令即可安裝

curl https://pyenv.run | bash

跑完之後會出現類似以下的東西:

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

就把他們加到.bashrc裡面,放在最下面即可,不知道如何編輯的朋友們可以在 terminal 輸入

open ~/.bashrc

如果你從來沒使用過 terminal 而且系統告訴你

The file /Users/xxx/.bashrc does not exist.

那就在 terminal 輸入以下指令後,複製貼上剛剛提到的那幾行

touch ~/.bashrc && open ~/.bashrc

接著重開 terminal!就準備好安裝

Step 4: 安裝 Python

在 terminal 輸入以下指令安裝

pyenv install 3.7.4

裝好之後我們可以看看我們總共有哪些版本

pyenv versions

*的為目前的版本,代表我們還在使用系統版本,所以要整個切過去

pyenv global 3.7.4

在跑一次

pyenv versions

Step 5: 完成囉,確認安裝結果

來看看我們的 python 出自於哪裡吧!

which python

/Users/<user-name>/.pyenv/shims/python

確認是不是已經是 3.7.4 了

python --version

Python 3.7.4

大功告成囉!

Share
  • Copied the URL !

Comments

List of comments (20)

Leave a Reply to rainyxiao Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Table of Contents