關於 Python 的幾種安裝與路徑(Python Install & PATH)
在幫助公司建立 data monorepo 的過程,決定把 Python 的一些相關安裝和路徑筆記起來。

< 一. Python on MacOS Docs >:
Using Python on a Mac Python Docs
< 二. 安裝及路徑 >:
- 作業系統本身就有的 Python(Apple-provided build of Python):一種
1. Apple-provided build of Python
-
Python.frameworkfolder:- Path:
/System/Library/Frameworks/Python.framework
- Path:
-
一些 Symlink file 和 一個 Regular file
python3(就我的查看結果)- Path:
/usr/bin/python
- Path:
By Default, 一般會是 Python 2.7 已經被 Apple 先安裝了,所以上面的檔案才會看起來都是 2 的東西
根據 python.org
但目前開發大都是用 Python3 了, Python2 也不再被 Support,Python3.6 也只 Support 到 2021 年底,所以底下分別歸類一些 Python developer 大多會使用的安裝方式。
注意:
根據 Python 官方說法,Apple-provided build of Python 是內建 Python 不該去移動修改它,因為它有可能被 MacOS 系統及其他 3 party 所需要。
- User 自行安裝(User build of Python):好幾種
1. Python 官網下載(Universal Binary build of Python)python.org
下載之後會有的東西:
-
Python 3.9folder:-
Path:
/Applications/ -
裡面有:
- IDLE(Integrated Development and Learning Environment): Python 的整合開發與學習環境,簡單來說就是撰寫 Python 程式的工具,打開後可以在 shell window 寫 Python code
- Python Launcher: 處理執行 python script 的作業
Refer to 5.1.1: How to run a Python script
> To run your script from the Finder you have two options:
1. Drag it to PythonLauncher
2. Select PythonLauncher as the default application to open your script (or any .py script) through the finder Info window and double-click it. PythonLauncher has various preferences to control how your script is launched. Option-dragging allows you to change these for one invocation, or use its Preferences menu to change things globally.
-
-
Python.frameworkfolder:- Path:
/Library/Frameworks/Python.framework - 裡面有:
- Python executable code 和 libraries.
- Path:
-
Symlink file:
- Path: 在
/usr/local/bin/ - 指向
/Library/Frameworks/Python.framework - 裡面有(假設install 的是 python3.9):
- 2to3:
../../../Library/Frameworks/Python.framework/Versions/3.9/bin/2to3 - 2to3-3.9:
../../../Library/Frameworks/Python.framework/Versions/3.9/bin/2to3-3.9 - idle3:
../../../Library/Frameworks/Python.framework/Versions/3.9/bin/idle3 - idle3.9:
../../../Library/Frameworks/Python.framework/Versions/3.9/bin/idle3.9 - pip3:
../../../Library/Frameworks/Python.framework/Versions/3.9/bin/pip3 - pip3.9:
../../../Library/Frameworks/Python.framework/Versions/3.9/bin/pip3.9 - pydoc3:
../../../Library/Frameworks/Python.framework/Versions/3.9/bin/pydoc3 - pydoc3.9:
../../../Library/Frameworks/Python.framework/Versions/3.9/bin/pydoc3.9 - python3:
../../../Library/Frameworks/Python.framework/Versions/3.9/bin/python3 - python3-config:
../../../Library/Frameworks/Python.framework/Versions/3.9/bin/python3-config - python3.9:
../../../Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9 - python3.9-config:
../../../Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9-config
- 2to3:
- Path: 在
-
確認版本:
- 只要去
Python 3.9folder 打開 IDLE 就可以看出來是 3.x.x。
- 只要去
-
移除官方下載的 Python:
- 同理,移除三樣東西就行,
- 移除 folder
/Applications/Python 3.9/ - 移除 folder
/Library/Frameworks/Python.framework/ - 移除
/usr/local/bin/底下的所有 Symlink (數個)
- 移除 folder
- 同理,移除三樣東西就行,
2. Homebrew 安裝
ex: 試著安裝最近 release 的 python3.10:
|
|
Homebrew 會裝在 /usr/local/Cellar 底下,經過查看發現多了很多東西🥲:
- ca-certificates
- gdbm
- libnghttp2
- mpdecimal
- python@3.10
- python@3.9
- sqlite
同時也會在 /usr/local/bin 建立一些 symlink
3. Pyenv 安裝
如果使用 Homebrew 安裝 Pyenv 的話,本身 Pyenv 的 source code 也是在 usr/local/Cellar/pyenv/ source code
一開始先 init source code:
|
|
- 這裡會修改 PATH 環境變數,加入 ${PYENV_ROOT}/shims/,使得之後指令會先選擇 shims 裡的指令執行,其他詳細的再研讀原始碼。
重新啟動 shell:
exec "$SHELL"
pyenv install -v <想要的版本>
至於使用 Pyenv 安裝 Python 的話,會是在:
~/.pyenv/
底下生出來。
pyenv 切換 python 版本方式:
|
|
查看 pyenv 切換有無成功:
pyenv versions
python --version
查看 python 路徑:
pyenv which python
如果單純使用 which python 是看不出來的。會得到 ~/.pyenv/shims,那 shims 裡面的內容其實就是一個接收的人,負責轉換。
shims folder 主要就是負責接收輸入 pyenv command 時的參數,所以在一開始 init 的時候就有 config 了。
pyenv exec: shims 會把該帶的參數丟給 pyenv exec 執行。source code
|
|
4. Conda
可以透過 Anaconda 或是 Miniconda ,我自己是使用 Miniconda
第一種. Default PATH 安裝:
|
|
- 如此 Python 就會被安裝在
~/opt/miniconda3/envs/底下:- PATH:
~/opt/miniconda3/envs/python3.7/bin/python
- PATH:
第二種. 在自己目前所在的 folder 裡安裝 Python:
|
|
這時就會在當前目錄生出包含 python3.8 的環境:
`root/`
|___`conda_env/`
|___`bin/`
|___`conda-meta/`
|___`include/`
|___`lib/`
|___`share/`
|___`ssl/`
進入環境:
conda activate ./conda_env
|
|
|
|
把目前環境打包成 yml 檔:
conda env export --prefix ./conda_env > environment.yml
依照當前 yml 檔,把環境建立在當前相同路徑下:
conda env create --prefix ./conda_env --file environment.yml
依照當前 yml 檔,把環境建立在 Conda 的 Default Path:
|
|
但是記得 yml 的 name 要修改一下,這會是之後裝在 opt/miniconda3/envs 底下的 folder 名字。

< The End >:
Self Checklist:
- How to install Python through different ways.
- What is Environment Variable
$PATH. - The meaning of
echo $PATH. - What is the difference between
pyenv pythonandApple-provided build of Python. - Understand all the path where python will be placed when download python.
- Understand what is
symlink. - Understand what is
IDLE. - Understand what is
Python Launcher.
