求助:在CMD中调用Python 3.6却默认启动Python 2.7的问题
Hey there, I’ve dealt with this exact headache before—when PATH edits don’t seem to stick, it’s usually something deeper than just environment variables. Let’s walk through the most reliable fixes:
1. Use the Official Python Launcher (py.exe)
Windows comes with a built-in Python launcher designed to manage multiple versions. It’s probably already installed with your Python 3.6 setup, so try these commands first:
- Launch Python 2.7:
py -2 - Launch Python 3.6:
py -3 - To set Python 3.6 as the default when you type
py(or evenpythonif configured), create apy.inifile:- Open Notepad and paste this:
[defaults] python=3.6 - Save it as
py.ini(make sure it’s not saved aspy.ini.txt) in either:C:\Windows(system-wide default)%USERPROFILE%\AppData\Local\Programs\Python\Launcher(user-specific default)
- Open Notepad and paste this:
2. Fix Windows Registry Entries
Sometimes Windows caches the Python path in the registry, overriding your PATH settings. Here’s how to update it:
- Open the Registry Editor by pressing Win+R, typing
regedit, and hitting Enter. - Navigate to
HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command - Double-click the Default value and replace the path with your Python 3.6 executable (e.g.,
C:\Python36\python.exe "%1" %*). - Next, go to
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\python.exe - Update the Default value to your Python 3.6 path as well.
Note: Always back up the registry before making changes—right-click the key and select "Export" to save a backup file.
3. Disable Windows App Execution Aliases
Windows has a feature that can hijack python commands even if your PATH is correct. Let’s turn those off:
- Open Windows Settings (Win+I) → Apps → App execution aliases.
- Look for entries like "Python.exe" or "Python3.exe" and toggle them Off. This prevents Windows from overriding your actual Python installations.
4. Restart Your CMD Session
Old CMD windows cache the environment variables from when they were opened. Even if you updated PATH, you need to close all existing CMD windows and open a new one for changes to take effect.
5. Quick Fix: Rename Executables
If you need an immediate workaround, you can rename the Python executables to avoid confusion:
- Go to your Python 3.6 install folder (e.g.,
C:\Python36), renamepython.exetopython3.exe. - Now you can launch Python 3.6 with
python3and Python 2.7 withpythonin CMD.
Most of the time, using the py launcher is the cleanest long-term solution—it’s made for exactly this scenario of managing multiple Python versions on Windows.
内容的提问来源于stack exchange,提问作者Tissuebox

