Skip to main content

How to Solve "ModuleNotFoundError: No module named 'boto3'" in Python

The ModuleNotFoundError: No module named 'boto3' error means that Python can't find the boto3 library, which is the official AWS SDK for Python. This usually happens because:

  • boto3 isn't installed.
  • It's installed in the wrong Python environment.
  • Your IDE/editor is configured to use a different Python interpreter.
  • There is a naming conflict with your files.

This guide explains how to install boto3, to troublehsoot common issues, and to ensure your environment is set up correctly.

Installing boto3 (The Basic Solution)

The standard way to install boto3 is using pip, Python's package installer. Open your terminal or command prompt and run:

pip install boto3
  • If you have multiple Python versions: You might need to use pip3 (or even a more specific version like pip3.9 or pip3.10) to ensure you're installing into the correct Python environment.

     pip3 install boto3
  • If you don't have permissions: Add sudo before the command (macOS/Linux), or run the terminal as an administrator (Windows). Alternatively, use the --user flag to install only for the current user.

  • Using python -m pip: If pip isn't in your PATH, you might need to use:

    python -m pip install boto3  # Or python3 -m pip

    This ensures you're using the pip associated with your current Python interpreter.

  • Conda: if you are using anaconda, you can also use the following command to install boto3:

conda install -c conda-forge boto3

Verifying the Installation

After installing, verify that boto3 is installed correctly by running a simple script:

import boto3

print(boto3.__version__)

If this runs without error and prints a version number, the installation was successful. If you still get the ModuleNotFoundError, proceed to the troubleshooting steps.

Troubleshooting Installation Issues

Multiple Python Versions

If you have multiple Python versions installed, you might have installed boto3 for the wrong one.

  • Check your Python version:
    python --version   # Or python3 --version
  • Use the correct pip: Make sure you use the pip (or pip3, pip3.9, etc.) that corresponds to the Python version you're using in your project.
  • You can also use the python -m pip or python3 -m pip to install the package using a specific python interpreter.

Virtual Environments

Always use virtual environments for your Python projects. This isolates your project's dependencies and prevents conflicts.

  1. Create a virtual environment:

    python3 -m venv venv  # Or python -m venv venv, py -m venv venv
  2. Activate the environment:

    • Linux/macOS: source venv/bin/activate

    • Windows (cmd): venv\Scripts\activate.bat

    • Windows (PowerShell): venv\Scripts\Activate.ps1

      If you see an error message that ps1 can not be loaded because running scripts is disabled on this system, run the following command in powershell, type "yes" when prompted and rerun the activation command.

      Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
  3. Install boto3 inside the activated environment:

    pip install boto3

IDE Configuration (VS Code, PyCharm)

Your IDE (like VS Code or PyCharm) needs to be configured to use the correct Python interpreter (and virtual environment, if applicable).

  • VS Code:

    1. Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P).
    2. Type "Python: Select Interpreter".
    3. Choose the interpreter associated with your project's virtual environment (it will usually have the environment name in parentheses).
  • PyCharm:

    1. Go to "File" -> "Settings" -> "Project" -> "Python Interpreter".
    2. Select the correct interpreter from the dropdown, or click the gear icon to add/configure a new one.

Jupyter Notebook

In Jupyter Notebook, you can install packages directly within a notebook cell:

    !pip install boto3
  • The ! tells Jupyter that the line is supposed to be run in the terminal and not interpreted as Python code.
note

This installs boto3 in the environment that Jupyter itself is running in, which might not be the same as your project's environment. For reliable results, ensure your Jupyter kernel is using the correct environment.

Naming Conflicts

Never name your Python files or directories the same as a module you're trying to import. For example, if you have a file named boto3.py in your project, Python might try to import that instead of the real boto3 library.

Reinstalling the Package

If all else fails, try uninstalling and reinstalling boto3:

pip uninstall boto3
pip install boto3
  • This can sometimes fix corrupted installations.

Handling "Import 'boto3' could not be resolved from source Pylance"

This specific error message from Pylance (the language server for Python in VS Code) usually indicates a configuration issue within VS Code, not a problem with boto3 itself.

  • Check Interpreter: As described in Section 3.3, ensure VS Code is using the correct Python interpreter where boto3 is installed.
  • Restart VS Code: A simple restart can sometimes resolve Pylance issues.
  • Pylance-Specific Settings: In rare cases, you might need to adjust Pylance's settings (search for "Pylance" in VS Code settings). However, this is usually not necessary if your interpreter is set up correctly.
  • Disabling Pylance If you're sure that your code is correct, and only pylance is showing the warning, you can disable it by adding # type: ignore next to the import boto3 line.

Installing boto3 on Specific Operating Systems / Environments

Windows

  • Open the command line by searching cmd and selecting Command Prompt.
  • Use pip install boto3.

macOS/Linux

  • Open your terminal
  • Use pip3 install boto3

Anaconda

  • You can use conda install -c conda-forge boto3.
  • You can also use anaconda navigator to install the package.

Jupyter Notebook

  • You can use !pip install boto3 inside a cell, however make sure that the Jupyter's kernel is the same as your project's virtual environment.

Conclusion

The ModuleNotFoundError: No module named 'boto3' error is almost always due to a missing or misplaced installation.

  • By following the installation steps, verifying your environment, and checking for naming conflicts, you can resolve this error and successfully use the boto3 library.
  • Remember to always use virtual environments to manage your project's dependencies.
  • If you are using an IDE like VS Code or PyCharm, ensure it's configured to use the correct Python interpreter.