Fix Python ModuleNotFoundError: No module named 'google.cloud'

The ModuleNotFoundError: No module named 'google.cloud' error in Python occurs when you try to use a module from the Google Cloud Platform (GCP) API, but the module is not available in your Python environment.

To solve this issue, you need to make sure that the google-cloud packages are installed and accessible from your system. This article will show you how.

Installing Google Cloud Platform packages in Python

Suppose you try to import Google Speech API with the code below:

from google.cloud import speech

Without having Google Cloud API packages installed, Python will respond with a ModuleNotFoundError as follows:

On June 18, 2018, Google also split the Python GCP library into multiple independent packages named google-cloud-*, so you need to check if you have the package you need.

When you need the Cloud Speech API, you need to install google-cloud-speech.

When you need the Cloud Storage API, install google-cloud-storage as shown below:

# installs Cloud Speech
pip install google-cloud-speech

# installs Cloud Storage
pip install google-cloud-storage

# installs AI Platform
pip install google-cloud-aiplatform

# installs BigQuery
pip install google-cloud-bigquery

# installs Cloud Domains
pip install google-cloud-domains

You can see the README file to see the full list of Python Cloud APIs that Google has.

Once you installed the correct package, you should be able to import the module into your code.

If you have the package but still see the error, then you might have one of the following scenarios:

  1. You have multiple versions of Python installed on your system, and you are using a different version of Python than the one where Google Cloud API is installed.
  2. You might have Google Cloud API installed in a virtual environment, and you are not activating the virtual environment before running your code.
  3. Your IDE uses a different version of Python from the one that has Google Cloud API

Let’s see how to fix these errors.

Issue with multiple versions of Python

If you have multiple versions of Python installed on your system, you need to make sure that you are using the specific version where the Google Cloud API package is installed.

You can test this by running the which -a python or which -a python3 command from the terminal:

$ which -a python3
/opt/homebrew/bin/python3
/usr/bin/python3

In the example above, there are two versions of Python installed on /opt/homebrew/bin/python3 and /usr/bin/python3.

Suppose you run the following steps in your project:

  1. Install Google Cloud API with pip using /usr/bin/ Python version
  2. Install Python using Homebrew, now you have Python in /opt/homebrew/
  3. Then add from google.cloud import ... in your code

The steps above will cause the error because Google Cloud API is installed in /usr/bin/, and your code is probably executed using Python from /opt/homebrew/ path.

To solve this error, you need to run pip install google-cloud-* command again so that the packages are installed and accessible by the new Python version.

Next, you can also have Google Cloud API installed in a virtual environment.

Issue with Python virtual environment

Python venv package allows you to create a virtual environment where you can install different versions of packages required by your project.

If you are installing google-cloud-* inside a virtual environment, then the module won’t be accessible outside of that environment.

You can see if a virtual environment is activated or not by looking at your command prompt.

When a virtual environment is activated, the name of that environment will be shown inside parentheses as shown below:

In the picture above, the name of the virtual environment (base) appears when the Conda virtual environment is activated.

You need to turn off the virtual environment so that pip installs to your computer.

When your virtual environment is created by Conda, run the conda deactivate command. Otherwise, running the deactivate command should work.

For more detail on creating, running, and deactivating Python virtual environment, you can read this venv introduction article.

Issue with IDE using a different Python version

Finally, the IDE from where you run your Python code may use a different Python version when you have multiple versions installed.

For example, you can check the Python interpreter used in VSCode by opening the command palette (CTRL + Shift + P for Windows and ⌘ + Shift + P for Mac) then run the Python: Select Interpreter command.

You should see all available Python versions listed as follows:

You need to use the same version where you installed Google Cloud API so that the module can be found when you run the code from VSCode.

Once done, you should be able to import Google Cloud API into your code.

Conclusion

In summary, the ModuleNotFoundError: No module named 'google.cloud' error occurs when the Google Cloud API packages are not available in your Python environment.

To fix this error, you need to install Google Cloud API using pip.

If you already have the module installed, make sure you are using the correct version of Python, deactivate the virtual environment if you have one, and check for the Python version used by your IDE.

By following these steps, you should be able to import Google Cloud API modules in your code successfully.

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.