Mac系统Python虚拟环境使用Google API Client遇导入错误求助
Hey there, let's get this sorted out for you—it's a super common hiccup when working with virtual environments, so don't stress!
The Root Cause
This error almost always boils down to one of two things:
- You installed the
google-api-python-clientpackage in your global Python setup instead of yourproject_envvirtual environment, or - You're running your
youtube.pyscript with your global Python interpreter instead of the one tied to your virtual environment.
Step-by-Step Fixes
Activate your virtual environment first
You need to make sureproject_envis active so all your package installs and script runs use the environment's isolated Python:- On Windows:
project_env\Scripts\activate - On macOS/Linux:
source project_env/bin/activate
You'll know it's working when you see
(project_env)at the start of your terminal prompt.- On Windows:
Reinstall the Google API Client in the activated environment
With the virtual environment active, run this command to install the package directly intoproject_env:pip install google-api-python-clientWait for the installation to finish—you should see a success message once it's done.
Run your script using the virtual environment's Python
Still with the environment activated, launch your script with:python youtube.py(If you normally use
python3for your global Python, use that here too:python3 youtube.py)
Bonus: Fix a Tiny Bug in Your Code
I spotted one small issue that will cause another error once the import is fixed—you wrote developerKeys (plural) instead of developerKey (singular). Update that line to:
youtube = build('youtube', 'v3', developerKey=api_key)
Double-Check (If Issues Linger)
If you still see the import error, run pip list in the activated environment and look for google-api-python-client in the list. If it's missing, repeat step 2 to make sure the install completed without issues.
内容的提问来源于stack exchange,提问作者newtocode

