Mac终端运行python run.py报错:找不到flask_restful模块
Hey there, let's work through this frustrating error together. The issue almost always boils down to a mismatch between which Python interpreter you're using to run your script and which one you installed flask_restful into. Here's how to troubleshoot step by step:
1. Verify Python and pip version alignment
Macs come pre-installed with Python 2.7 by default, but if you've installed Python 3 (which you probably have), you might be mixing up python/pip (Python 2) and python3/pip3 (Python 3).
- First, check which Python you're running:
python --version - Then check which pip is associated with it:
pip --version
If the versions don't match (e.g., Python 3.9 but pip points to Python 2.7), that's the problem. Try running your script with python3 instead:
python3 run.py
And if you haven't installed flask_restful for Python 3 yet, use:
pip3 install flask-restful
2. Confirm the package is installed in your active environment
Run this command to list all packages installed for your target Python interpreter:
# For Python 2 python -m pip list # For Python 3 python3 -m pip list
Look for flask-restful (note the hyphen here — pip uses hyphens, but imports use underscores). If it's not listed, install it using the -m pip method to ensure it goes to the right interpreter:
python3 -m pip install flask-restful
3. Check for virtual environment issues
If your project uses a virtual environment (like venv, virtualenv, or Conda), you need to activate it before installing packages or running your script.
- For a standard
venvsetup:source /path/to/your/venv/bin/activate
Once activated, install the package again (even if you think you did before) and run your script:
pip install flask-restful python run.py
4. Use the full path to your Python interpreter
If all else fails, find the exact path to your Python 3 interpreter:
which python3
You'll get something like /usr/local/bin/python3. Use this full path to install the package and run your script:
/usr/local/bin/python3 -m pip install flask-restful /usr/local/bin/python3 run.py
To recap, your error looks like this:
$ python run.py
Traceback (most recent call last):
File "run.py", line 17, in
app = create_app("config")
File "run.py", line 7, in create_app
from app import api_bp
File "/Users/userName/Backend/app.py", line 2, in
from flask_restful import Api
ModuleNotFoundError: No module named 'flask_restful'
This is almost certainly a version/environment mismatch — the steps above should get you sorted.
内容的提问来源于stack exchange,提问作者Vivek Bhadula

