You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

PyCharm中Flask服务器启动异常求助:显示运行状态但浏览器报Internal Server Error且端口无法修改

Troubleshooting Flask Internal Server Error in PyCharm (Across All Projects)

Since this issue pops up for every Flask project you try—even a brand new Hello World one—it’s almost certainly not a problem with your code. Let’s walk through the most likely fixes, focused on PyCharm configurations and your Python environment:

1. Verify Your Python Interpreter & Flask Installation

First, double-check that PyCharm is using the correct Python environment where Flask is properly installed. IDEs can sometimes default to a global interpreter or stale virtual environment without you noticing:

  • Go to File > Settings > Project: [Your Project Name] > Python Interpreter
  • Look for flask in the list of installed packages. If it’s missing, click the + button, search for Flask, and install it.
  • If it’s present, try upgrading/downgrading to a stable version (like 2.0.x) in case there’s a compatibility issue with your Python version.

2. Fix PyCharm’s Run/Debug Configuration

You mentioned you can’t modify the server port—this suggests your run configuration might be locked or misconfigured:

  • Click the run configuration dropdown in the top-right corner of PyCharm, then select Edit Configurations...
  • Find your Flask configuration (or create a new one if needed)
  • Check the Parameters field—make sure there’s no forced --port 5000 entry overriding your code. To test a different port, add --port 5001 here, or modify your code to app.run(port=5001)
  • Also verify the FLASK_APP environment variable is set correctly (it should point to your main script, like app.py)

3. Clear PyCharm’s Cache & Restart

PyCharm’s cached files can get corrupted and cause weird, inconsistent behavior. A quick reset often fixes this:

  • Go to File > Invalidate Caches...
  • Check the box for Clear file system cache and local history, then click Invalidate and Restart
  • Once PyCharm reboots, try running your Hello World project again.

4. Test Outside PyCharm (Terminal Run)

To rule out PyCharm as the culprit, run your Flask project directly from the terminal:

  • Navigate to your project directory in a terminal
  • Activate your virtual environment (if using one: source venv/bin/activate on Mac/Linux, venv\Scripts\activate on Windows)
  • Run either python app.py or flask run
  • If this works without errors, the problem is definitely in PyCharm’s settings. If it still throws an internal server error:
    • Check if port 5000 is occupied by another process: use lsof -i :5000 (Mac/Linux) or netstat -ano | findstr :5000 (Windows) to see what’s using the port, then kill that process.
    • Temporarily disable your firewall/antivirus—sometimes these tools block Flask’s server requests unexpectedly.

5. Reinstall Flask & Dependencies

If all else fails, a clean reinstall of Flask can fix hidden corruption:

  • In your terminal (with the virtual environment activated), run pip uninstall flask -y
  • Then run pip install flask to get the latest stable version, or specify a known-good version like pip install flask==2.0.3

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

火山引擎 最新活动