When you run a script that imports Google's AI tools and hit the ModuleNotFoundError: No module named 'google.generativeai', it means Python can't find the required package in your current environment. This typically happens because you haven't installed the SDK yet, you're running the script in a different virtual environment than the one where you installed it, or there's a typo in your import statement.
Fixing this issue takes just a few terminal commands once you verify your Python interpreter and package path. Let's look at why this error happens and how to get your code running cleanly.
Why Python Throws This Error
Python raises a ModuleNotFoundError whenever an import statement fails to locate the corresponding module directory or file in the current module search path (sys.path). For the Google Generative AI SDK, this happens for three common reasons:
- Missing installation: You haven't run the
pipinstallation command for the package in your active project environment. - Environment mismatch: You installed the package globally or in one virtual environment (
venv,conda), but you're executing your script using a different Python interpreter. - Incorrect package name: You tried installing a similarly named package or made a typo in your import syntax.
Let's check your active setup to see exactly what's going wrong. Observers at Bloomberg have shared their thoughts on this matter.
Step 1: Diagnose Your Python and Pip Environment
Before installing anything, verify which Python interpreter and environment are active in your terminal. Mixing up system Python and virtual environments is the number one cause of import failures.
Run these commands in your terminal:
python -c "import sys; print(sys.executable)"
pip list | grep -i generative
If pip list doesn't show google-generativeai, the package isn't present in the environment tied to that specific python executable.
Step 2: Install the Correct Package
The official Google Generative AI Python client library is distributed under a specific package name on PyPI. Make sure you install the exact package name rather than a guessed variation:
pip install google-generativeai
If you manage your dependencies with a requirements.txt file, add this exact line to your file:
google-generativeai>=0.8.0
Then install it by running:
pip install -r requirements.txt
For more details on setting up credentials and API keys, check out the Google AI for Developers documentation.
Step 3: Verify Your Virtual Environment
If you work on multiple projects, you're likely using virtual environments. If you install the package in your global environment but run your script inside an isolated virtual environment (or vice versa), Python won't see it.
Activate your virtual environment before installing or running your scripts:
- macOS / Linux:
source venv/bin/activate - Windows (Command Prompt):
venv\Scripts\activate.bat - Windows (PowerShell):
venv\Scripts\Activate.ps1
Once activated, run pip install google-generativeai again to ensure the library lives inside your isolated project workspace.
Step 4: Correct Import Syntax in Your Code
Ensure your Python file imports the module using the exact syntax expected by the library. A common mistake is leaving out parts of the namespace or misspelling the module name.
Here is the correct import structure for the Google Generative AI SDK:
import os
import google.generativeai as genai
# Configure your API key
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
# Initialize the model
model = genai.GenerativeModel("gemini-1.5-flash")
# Generate content
response = model.generate_content("Explain how module import paths work in Python.")
print(response.text)
If you wrote import google_generativeai or import generativeai, Python will throw an import error because those are not the canonical module paths defined by the package.
Common Mistakes to Avoid
Even experienced developers run into subtle traps when configuring third-party AI SDKs. Watch out for these pitfalls:
- Running Jupyter Notebooks with the wrong kernel: If you installed
google-generativeaiin your terminal but your Jupyter notebook runs on a different kernel, you'll get an import error. Select the correct kernel in your notebook interface or run!pip install google-generativeaidirectly inside a notebook cell. - Using outdated pip versions: Old versions of pip can fail to resolve modern package dependencies or wheel formats. Always update pip first using
python -m pip install --upgrade pip. - Confusing package name with import name: The PyPI installation name uses a hyphen (
google-generativeai), but the Python import statement uses a dot (import google.generativeai).
To read more about managing package dependencies cleanly across different developer machines, refer to the Python Packaging User Guide.
Frequently Asked Questions
Why does pip say the requirement is already satisfied, but Python still throws an error?
This happens when you have multiple versions of Python installed on your machine. The pip command you ran belongs to one Python installation, while the script you executed is running on a different one. Use python -m pip install google-generativeai to guarantee that pip installs the package into the exact Python environment you are currently executing.
Do I need a special API key to use this module?
Yes. While installing the package doesn't require a key, actually calling the generative models requires a Gemini API key. You can get one from Google AI Studio and set it as an environment variable named GEMINI_API_KEY.
Can I use this library inside a Docker container?
Yes, but you must ensure your Dockerfile includes a step that runs pip install google-generativeai or copies a requirements.txt file and runs pip install -r requirements.txt during the image build process.