You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Python 3设为默认应用?解决脚本无法在解释器运行问题

Hey there! Let's work through this Python 2 and 3 coexistence problem together—super common issue, so we’ve got this.

1. Fix the .py File Association First

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 .py script 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.exe file in your Python installation folder (e.g., C:\Python27\python.exe or C:\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.

2. Get Your Environment Variables Right (No More "Not Working" Issues)

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 Path variable 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 where pip3 lives)
  • Important: If you want Python 3 to be the default when you type python in 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.exe to python2.exe
    • Go to your Python 3 folder, rename python.exe to python3.exe
      Now you can type python2 or python3 in the command line to explicitly run the version you want—no more guessing!
3. Run Scripts with a Specific Version On-Demand

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 (or py -2 your_script.py if you have the launcher)
  • For Python 3: python3 your_script.py (or py -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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:39:08