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

求助:PyCharm阻止从桌面及命令提示符运行.py文件的解决办法

Fixing .py Files Opening in PyCharm Instead of Running Directly on Windows 10

Hey there! I totally get your frustration—PyCharm is a lifesaver for coding, but it’s a hassle when you need to share scripts with non-technical folks who don’t want to mess with an IDE. Let’s sort this out so your .py files run straight from the desktop or command prompt, without forcing anyone to open PyCharm. Here’s what to do:

1. Make Sure Python is Added to Your System PATH

First, we need to confirm Windows can find Python without relying on PyCharm.

  • Open Command Prompt and type python --version (or py --version if you have multiple Python versions installed). If it shows your Python version, great—PATH is set up. If you get an error like "‘python’ is not recognized as an internal or external command", we need to fix that:
    • Find your Python installation folder (usually something like C:\Users\<YourUsername>\AppData\Local\Programs\Python\Python3X, where 3X is your version number).
    • Right-click "This PC" → "Properties" → "Advanced System Settings" → "Environment Variables".
    • Under "System Variables", find the Path variable, click "Edit", then "New".
    • Paste in your Python folder path, and also add the Scripts subfolder (e.g., C:\Users\<YourUsername>\AppData\Local\Programs\Python\Python3X\Scripts).
    • Click "OK" all the way through, then restart Command Prompt to apply the changes.

2. Reset the Default Program for .py Files

PyCharm might have taken over as the default app for .py files. Let’s change that back to Python:

  • Right-click any .py file on your desktop → "Open with" → "Choose another app".
  • Click "More apps" → scroll to the bottom and select "Look for another app on this PC".
  • Navigate to your Python installation folder and select python.exe (not pythonw.exe—that’s for running scripts without a console window, which you don’t want if your script prints output).
  • Check the box that says "Always use this app to open .py files", then click "OK".

3. Test It Out

Double-click your .py file, or type its full path into Command Prompt (e.g., C:\Users\You\Desktop\my_script.py). It should now run directly in a console window, no PyCharm required.

Bonus: Package Your Script as an EXE for Non-Technical Users

If you want to share your script with someone who doesn’t even have Python installed, use pyinstaller to turn it into a standalone EXE:

  • Open Command Prompt and install pyinstaller with pip install pyinstaller.
  • Navigate to the folder where your script is saved (use cd C:\Path\To\Your\Script).
  • Run pyinstaller --onefile your_script.py.
  • A dist folder will be created with your EXE file inside. Anyone can double-click this EXE to run your script without needing Python or PyCharm installed.

内容的提问来源于stack exchange,提问作者user9825338

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:51:28