PyCharm中已安装openpyxl却提示ModuleNotFoundError问题求助
Hey there, no need to stress—this is one of the most common hiccups when you're getting started with PyCharm and Python packages. Let's walk through the fixes step by step:
Check which Python interpreter PyCharm is using
PyCharm often uses a dedicated virtual environment for each project, which might not have the packages you installed globally. Here's how to verify:- Go to
File > Settings(orPyCharm > Preferenceson Mac) - Navigate to
Project: [Your Project Name] > Python Interpreter - Look through the list of installed packages—if
openpyxlisn't there, that's the root of the problem.
- Go to
Install openpyxl directly in PyCharm's interpreter
If it's missing from the interpreter list, you can install it right from the settings:- In the Python Interpreter page, click the
+icon in the top-right corner - Search for
openpyxlin the package search bar - Select it and click
Install Package—wait for the installation to finish, then try importing again.
- In the Python Interpreter page, click the
Use PyCharm's Terminal to install the package
Sometimes using the built-in Terminal is more straightforward, as it automatically uses the project's active interpreter:- Open the Terminal tab at the bottom of PyCharm
- Run the command:
(If you're explicitly using Python 3, you might needpip install openpyxlpip3 install openpyxlinstead)
Double-check your global vs virtual environment setup
If you installed openpyxl usingpipoutside of PyCharm, it's probably in your global Python installation. But if your PyCharm project is using a virtual environment, that global package won't be available. The fixes above will ensure the package is installed in the correct environment for your project.
Once you've done one of these steps, go ahead and try importing openpyxl again—it should work! This is just a matter of making sure PyCharm is looking in the right place for your installed packages, and it gets easier once you get familiar with how PyCharm manages interpreters.
内容的提问来源于stack exchange,提问作者Maarten

