Ubuntu16.04+PyCharm2018.1.1+Python3.5下numpy导入错误求助
Fixing "ImportError: No module named 'numpy'" in PyCharm on Ubuntu
Hey there, I get how frustrating it is to hit this error even after repeating the setup dozens of times. Let's cut to the chase—this almost always boils down to a mismatch between the Python interpreter PyCharm is using and the one where you installed numpy. Let's walk through the fixes step by step:
1. Confirm PyCharm's Active Python Interpreter
First, let's make sure we know which Python version PyCharm is trying to use for your project:
- Open your project in PyCharm.
- Navigate to
File > Settings > Project: [Your Project Name] > Python Interpreter(on macOS, it'sPyCharm > Preferences > Project: [Your Project Name] > Python Interpreter). - Take note of the interpreter path at the top (it might look like
/usr/bin/python3.10or a path pointing to a virtual environment folder).
2. Check Where Your numpy Installation Lives
Now let's verify which Python version actually has numpy installed:
- Open your Ubuntu terminal and run this command:
Look for thepip3 show numpyLocationline in the output—it'll tell you which Python'ssite-packagesordist-packagesfolder numpy is in (e.g.,/usr/lib/python3.10/dist-packages). Compare this Python version to the one PyCharm is using. If they don't match, that's exactly why the import fails.
3. Fix the Mismatch (Two Easy Options)
Option 1: Point PyCharm to the System Python with numpy
- Go back to PyCharm's Python Interpreter settings.
- Click the gear icon >
Add. - Select
System Interpreter, then pick the Python path that matches where numpy is installed (you can find the exact path withwhich python3in terminal). - Save the settings, then try importing numpy again in your project.
Option 2: Install numpy Directly in PyCharm's Interpreter
If you'd rather keep using the interpreter PyCharm has selected:
- In the Python Interpreter settings, click the
+button to add a new package. - Search for
numpy, select it from the list, and clickInstall Package. PyCharm will handle installing it directly to the interpreter your project is using.
4. Double-Check for Version Confusion
- Always use
pip3(notpip) when installing packages for Python 3—sometimespipdefaults to Python 2 on Ubuntu, which would install numpy in the wrong place. Run this to ensure numpy is up-to-date for Python 3:sudo pip3 install --upgrade numpy - Also, check if your project has a hidden virtual environment folder (like
venvorenv). If it does, PyCharm might be using that instead of the system Python—you'll need to activate the venv in terminal and install numpy there, or switch PyCharm to use the system interpreter.
Give these steps a try—this should resolve the import error once and for all.
内容的提问来源于stack exchange,提问作者JStryker
相关产品推荐
相关产品推荐

