Python 3.7调用BigQuery时出现ModuleNotFoundError: No module named 'google'错误的求助
ModuleNotFoundError: No module named 'google' for Python 3.7 + BigQuery Hey there, let's work through this frustrating error step by step—this is super common when dealing with Python environments on macOS, so you're not alone! Here are the most likely fixes to get your BigQuery code running:
1. Make sure you're installing the library for the right Python version
macOS comes with Python 2 pre-installed, which often causes confusion. If you ran just pip install google-cloud-bigquery, it might have installed the library for Python 2 instead of your 3.7 version.
First, confirm your Python 3.7 path:
python3.7 --versionYou should see something like
Python 3.7.xprinted.Then install the library specifically for Python 3.7 using its corresponding pip:
pip3.7 install google-cloud-bigquery
2. Use a virtual environment (recommended!)
Virtual environments keep your project dependencies isolated, so you don't run into version conflicts or "wrong environment" issues. Here's how to set one up for your BigQuery project:
Create a virtual environment (name it whatever you want, like
bigquery-env):python3.7 -m venv bigquery-envActivate the environment (you'll see
(bigquery-env)at the start of your terminal prompt when it's active):source bigquery-env/bin/activateNow install the BigQuery library inside this environment:
pip install google-cloud-bigqueryWhen you're done working, you can deactivate the environment with:
deactivate
3. Verify your Python interpreter in your code editor
If you're using an IDE like VS Code or PyCharm, it might be using a different Python interpreter than the one you installed the library for.
For VS Code: Press
Cmd+Shift+P, search for "Python: Select Interpreter", and pick the Python 3.7 version (or the virtual environment you created, which will show up as./bigquery-env/bin/python).For PyCharm: Go to
File > Settings > Project: [your project name] > Python Interpreter, and select the correct Python 3.7 interpreter or virtual environment.
4. Double-check your PATH and installed packages
If the above steps don't work, let's confirm where the library is installed and that your Python can find it:
List all packages installed for Python 3.7:
pip3.7 listLook for
google-cloud-bigqueryin the output—if it's not there, re-run the install command from step 1.Check which Python your terminal is using by default:
which pythonIf this points to
/usr/bin/python(the system Python 2), you'll need to usepython3.7explicitly to run your script:python3.7 your_script_name.py
5. Reinstall the library (clean slate)
If there was a corrupted install, try uninstalling and reinstalling:
pip3.7 uninstall google-cloud-bigquery pip3.7 install --upgrade google-cloud-bigquery
Once you've done one of these steps, test the import in your terminal first to confirm:
python3.7 >>> from google.cloud import bigquery >>> # No error? Great! Now run your sample code.
内容的提问来源于stack exchange,提问作者Jad Edlebi

