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

Pygame安装异常求助:命令行可用但PyCharm无法导入

Fixing Pygame Module Not Found in PyCharm (When Command Line Works)

Hey there, let's work through this Pygame issue step by step—sounds like a classic environment mismatch problem, which is super common when switching between command line and IDEs like PyCharm. Here's how to sort it out:

1. Fix the PyCharm Module Not Found Error

The root cause here is that the Python interpreter PyCharm is using for your project doesn't have Pygame installed, while the one your command line uses (py) does. Let's align them:

  • First, confirm which Python version your command line is using:
    Open Command Prompt and run:

    py --version
    

    Note down the version number (e.g., Python 3.10.12).

  • Next, switch PyCharm's project interpreter to match this version:

    1. Open PyCharm, go to File > Settings (or File > Preferences on Mac).
    2. Navigate to Project: [Your Project Name] > Python Interpreter.
    3. Click the gear icon in the top-right corner, select Add.
    4. Choose System Interpreter, then find the Python executable that matches the version you noted earlier. On Windows, this is usually in a path like C:\Users\YourUsername\AppData\Local\Programs\Python\Python310\python.exe.
    5. Click OK to set this as your project's interpreter.
  • Test it out:
    Create a new Python file in your PyCharm project with this code:

    import pygame
    print(pygame.__version__)
    pygame.init()
    pygame.display.set_mode((400, 300))
    pygame.quit()
    

    Run the file—if it prints the Pygame version without errors, you're good to go!

2. Fix the Desktop Lib/Include Folders Issue

Those folders on your desktop mean Pygame was installed to the wrong location (likely because you used a --target flag in your pip command or selected the desktop as the install path by accident). Here's how to clean this up:

  • Delete the Lib and Include folders from your desktop (double-check they're only Pygame-related first—if you're unsure, you can skip this step and just reinstall correctly).
  • Reinstall Pygame to the correct location using Command Prompt:
    py -m pip install pygame --upgrade
    
    This will install Pygame directly into the site-packages directory of the Python interpreter your py command uses, which is where PyCharm will look once you've switched the interpreter.

Extra Troubleshooting Tip

If you're still having issues, run this command in Command Prompt to confirm where Pygame is installed:

py -m pip show pygame

Look for the Location field—this is the path to Pygame's installation directory. In PyCharm's interpreter settings, you can check that this path is listed under the interpreter's Packages tab (search for pygame to verify it's present).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 07:56:10