Python 更新很快,版本又多,有時我們為了使用一個穩定的版本,會以自訂路徑的方式安裝,並利用「add to PATH」選項,將安裝好的 python 路徑掛在環境變數中。
例:安裝 python 3.8.x 版本至 C:\Python38\
利用 command echo %PATH% 查看確實有 C:\Python38\。
這樣一來就可以直接在 command line 中輸入 python。
但…有一天,奇怪的事就發生了:
剛好在我要實驗 git hook 去呼叫 python 時發生狀況:
D:\git_test>git commit
/usr/bin/env: ‘python3’: Permission denied
檢查 python 還在
D:\>python
Python 3.8.13 (default, May 20 2022, 16:23:54) [MSC v.1916 64 bit (AMD64)] :: Intel Corporation on win32
Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated. Libraries may fail to load. To activate this environment
please see https://conda.io/activation
Type "help", "copyright", "credits" or "license" for more information.
Intel(R) Distribution for Python is brought to you by Intel Corporation.
Please check out: https://software.intel.com/en-us/python-distribution
>>>
但 python3 確實不存在
D:\>python3
'python3' is not recognized as an internal or external command,
operable program or batch file.
原來,Windows Store 自己默默(?) 拉了兩條路徑給 python.exe 和 python3.exe,它們默認安裝在%USERPROFILE%\AppData\Local\Microsoft\WindowsApps。
這邊可以透過在 Windows search prompt 裡輸入 manage app execution aliases 看到開關,這邊最好關掉避免干擾。

到這裡,python3 不存在的問題仍未解決。
New-Item -Type SymbolicLink -Path python3.exe -Target c:\<Python3-installation-directory>\python.exe
New-Item -Type SymbolicLink -Path python3.exe -Target c:\python38\python.exe
cmd 下來建立 Symbolic Link: mklink c:\<Python3-installation-directory>\python3.exe c:\<Python3-installation-directory>\python.exe
c:\Windows\system32>mklink c:\python38\python3.exe c:\python38\python.exe
symbolic link created for python3.exe <<===>> c:\python38\python.exe
注意一下,想像一下 Symbolic Link 就像一個叫作 python3.exe 的分身捷徑,它實際連接到 python.exe。 因為 python.exe 本身不是一個完全獨立的 EXE 檔,所以捷徑本身的路徑也必須是正確。
成功的話,可以看到這樣的結果:
c:\Windows\system32>python3
Python 3.8.8 (tags/v3.8.8:024d805, Feb 19 2021, 13:18:16) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
失敗的話,則會看到以下:
c:\Windows\system32>python3
Python path configuration:
PYTHONHOME = (not set)
PYTHONPATH = (not set)
program name = 'python3'
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = 'C:\\Windows\\system32\\python3.exe'
sys.base_prefix = 'C:\\IntelSWTools\\system_debugger\\2234-nda\\tools\\python38'
sys.base_exec_prefix = 'C:\\IntelSWTools\\system_debugger\\2234-nda\\tools\\python38'
sys.executable = 'C:\\Windows\\system32\\python3.exe'
sys.prefix = 'C:\\IntelSWTools\\system_debugger\\2234-nda\\tools\\python38'
sys.exec_prefix = 'C:\\IntelSWTools\\system_debugger\\2234-nda\\tools\\python38'
sys.path = [
'C:\\IntelSWTools\\system_debugger\\2234-nda\\tools\\python38\\python38.zip',
'.\\DLLs',
'.\\lib',
'C:\\Windows\\system32',
]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
Current thread 0x0000dda8 (most recent call first):
<no Python frame>
表示 python3 指到 ‘C:\Windows\system32\python3.exe’ 只要修正路徑後就會正常了。(如果有建立了錯誤的 symbolic link 也要先刪掉)
NOTE
Windows 10 是透過所謂 App Execution Aliases 技術將 python.exe、python3.exe 等執行檔導向 Microsoft Store Python 安裝。其位置在 C:\User*username*\AppData\Local\Microsoft\WindowsApp。