同一操作系统中安装多个版本Python解释器时。
在*nix操作系统中,通过shabang行,可以指定运行程序的Python解释器。
在Windows操作系统中,大致有两种方法指定运行程序的Python解释器。
如果不指定解释器,直接运行Python启动器(py.exe),则会运行Python启动器默认的解释器。默认解释器默认是由Python启动器自动获取的,可以通过在program data
目录或py.exe
所在目录添加py.ini
手工设置默认解释器。
C:\Users\yy>py -0p
Installed Pythons found by py Launcher for Windows
-3.8-64 C:\Users\yy\AppData\Local\Programs\Python\Python38\python.exe *
-3.7-64 C:\Users\yy\AppData\Local\Programs\Python\Python37\python.exe
-3.5-64 C:\Program Files\Python35\python.exe
-2.7-64 E:\Python27\python.exe
如果不指定解释器,直接运行python.exe,系统会自动搜索系统目录(即环境变量path
),执行第一个搜索的python.exe
。也就是说python.exe
的确定是受环境变量的顺序影响的。通过where
命令即可确认默认的python.exe
(注意!powershell没有where
命令)。
C:\Users\yy>where python.exe
C:\Users\yy\AppData\Local\Programs\Python\Python37\python.exe
C:\Program Files\Python35\python.exe
E:\Python27\python.exe
d:\ProgramData\Anaconda3\python.exe
C:\Users\yy\AppData\Local\Programs\Python\Python38\python.exe
C:\Users\yy>path
PATH=C:\Users\yy\AppData\Local\Programs\Python\Python37\Scripts\;C:\Users\yy\App
Data\Local\Programs\Python\Python37\;C:\Microsoft VS Code\bin;C:\Python33\Lib\si
te-packages\PyQt5;d:\ProgramData\Anaconda3\Library\mingw-w64\bin;d:\ProgramData\
Anaconda3\Library\usr\bin;d:\ProgramData\Anaconda3\Library\bin;d:\ProgramData\An
aconda3\Scripts;C:\Program Files\VanDyke Software\Clients\;C:\Program Files\Pyth
on35\Scripts\;C:\Program Files\Python35\;C:\ProgramData\Oracle\Java\javapath;E:\
Python27\;E:\Python27\Scripts;C:\Program Files (x86)\Common Files\NetSarang;C:\P
rogram Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Win
dows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPow
erShell\v1.0\;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Fil
es (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files\Intel\Intel(R) Management
Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Compone
nts\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C
:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program
Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL
Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\P
rogram Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;C:\P
rogram Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\;C:
\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;H:\geckodriver-v0.23.0-w
in64;C:\Program Files (x86)\Tesseract-OCR;C:\Program Files (x86)\MySQL\MySQL Ser
ver 5.0\bin;C:\Program Files (x86)\Egret\Egret Wing 3\bin;C:\Microsoft VS Code\b
in;d:\ProgramData\Anaconda3;C:\Users\yy\AppData\Local\Programs\Python\Python38\S
cripts\;C:\Users\yy\AppData\Local\Programs\Python\Python38\;C:\Program Files\Jet
Brains\PyCharm Community Edition 2020.3.3\bin;