如何将Python 3设为默认应用?解决脚本无法在解释器运行问题
Hey there! Let's work through this Python 2 and 3 coexistence problem together—super common issue, so we’ve got this.
The reason your old scripts started opening in Notepad after installing Python 3 is that the default file association for .py files got messed up (either overwritten or not set correctly by Python 3’s installer). Here’s how to fix it:
- Right-click any
.pyscript file → Select Open with → Choose another app - Scroll down and click More apps → Find your preferred Python interpreter (either Python 2 or 3) from the list. If it’s not there, click Look for another app on this PC and navigate to the
python.exefile in your Python installation folder (e.g.,C:\Python27\python.exeorC:\Python39\python.exe) - Make sure to check the box that says Always use this app to open .py files to lock in the association.
Pro tip: If you installed Python with the "Install launcher for all users" option (recommended!), you can use the py command to switch versions on the fly without changing associations. Just add #! python2 or #! python3 at the very top of your script, and double-clicking will run it with the specified version.
If your Python 3 path isn’t taking effect, chances are you either added the wrong paths or the order in your PATH is causing conflicts. Here’s the correct way to set it up:
- Open your system environment variables (search "Edit the system environment variables" in Windows search → Click Environment Variables)
- Under System variables, find the
Pathvariable and click Edit - Add two entries for Python 3:
- The main installation folder (e.g.,
C:\Python39) - The Scripts folder (e.g.,
C:\Python39\Scripts—this is wherepip3lives)
- The main installation folder (e.g.,
- Important: If you want Python 3 to be the default when you type
pythonin the command line, move these Python 3 entries above any Python 2 entries in the Path list. If you want Python 2 as default, keep Python 2’s paths higher up. - To avoid confusion, you can even rename the executables:
- Go to your Python 2 folder, rename
python.exetopython2.exe - Go to your Python 3 folder, rename
python.exetopython3.exe
Now you can typepython2orpython3in the command line to explicitly run the version you want—no more guessing!
- Go to your Python 2 folder, rename
If you don’t want to change the default association but need to run a script with a specific Python version, just use the command line:
- For Python 2:
python2 your_script.py(orpy -2 your_script.pyif you have the launcher) - For Python 3:
python3 your_script.py(orpy -3 your_script.py)
This is especially useful if you have some scripts that only work with Python 2 and others that need Python 3.
Just remember: Always install Python 2 and 3 in separate folders (never overwrite one with the other) and double-check that both versions’ paths are in your PATH variable. That should eliminate most coexistence headaches!
内容的提问来源于stack exchange,提问作者Tarang Somani

