如何配置PyCharm使其识别已安装的系统Python库?
Hey there! I totally get this confusion—PyCharm's default setup uses isolated virtual environments for each project, which is great for dependency management but can trip up new users when they want to use libraries installed globally via pip. Let's get this sorted step by step:
Why This Happens
When you run python main.py in your macOS terminal, you're using your system-wide Python interpreter (the one where you installed matplotlib/numpy via pip). But PyCharm automatically creates a separate virtual environment for each new project by default—this environment doesn't have access to the libraries you installed globally. That's why you're seeing the ModuleNotFoundError.
Step 1: Access PyCharm's Interpreter Settings
First, open the settings menu:
- On macOS: Go to
PyCharm→Settings(or use the shortcutCmd + ,) - On Windows/Linux: Go to
File→Settings(shortcutCtrl + Alt + S)
In the settings window, navigate to Project: [Your Project Name] → Python Interpreter. Click the gear icon in the top-right corner and select Add.
Step 2: Select Your System-Wide Python Interpreter
In the pop-up window:
- Switch to the
System Interpretertab. - Find the Python interpreter you use in your terminal. On macOS, this is often:
/usr/bin/python3(the default system Python)- Or a version-specific path like
/Library/Frameworks/Python.framework/Versions/3.11/bin/python3(if you installed Python via the official installer)
Pro tip: Runwhich pythonorwhich python3in your terminal to get the exact path of your system interpreter.
- Once you select the correct interpreter, you'll see a list of all libraries installed globally (including matplotlib and numpy) in the pane below. Click
OKto confirm.
Step 3: Set This Interpreter as the Global Default (For All Projects)
If you want every new PyCharm project to use your system interpreter by default (so you don't have to repeat this setup):
- Go back to the
Python Interpretersettings page, click the gear icon again, and selectShow All. - In the list of available interpreters, select the system interpreter you just added.
- Click the star icon in the top-right corner of this window—this marks it as the default interpreter for all future projects.
- Alternatively, go to
File→New Project Settings→Settings for New Projects, then select the system interpreter underPython Interpreter.
Verify the Fix
After setting this up:
- Run your test code again in PyCharm—it should now load matplotlib and numpy without errors and display the plot.
- You can also open PyCharm's built-in terminal (bottom toolbar) and run
pip list—you should see the same libraries listed as when you run the command in your system terminal.
内容的提问来源于stack exchange,提问作者jnemecz

