Last modified: Jun 08, 2026

Install Bandit Python Guide

Bandit is a powerful tool that finds common security issues in Python code. It scans your files for problems like hardcoded passwords or SQL injection risks. This guide shows you how to install Bandit quickly and start using it today.

Security is vital for any project. Bandit helps you catch vulnerabilities early. You don't need to be an expert to use it. Just follow these simple steps.

What Is Bandit?

Bandit is a static analysis tool. It checks your Python code without running it. It looks for dangerous patterns and suggests fixes. Many developers use Bandit to improve code safety.

It works with Python 3 and is easy to set up. You can run it from the command line or integrate it into your workflow. Let's see how to install it.

Prerequisites

Before installing Bandit, make sure you have Python installed. You need Python 3.6 or newer. Check your version with this command:


python --version

If you don't have Python, download it from the official website. Also, you should have pip installed. Pip is the package manager for Python. Verify it with:


pip --version

If pip is missing, install it using your system's package manager or the Python installer.

How to Install Bandit

The easiest way to install Bandit is with pip. Open your terminal and run this command:


pip install bandit

This command downloads Bandit and its dependencies. It takes only a few seconds. You should see output like this:


Collecting bandit
  Downloading bandit-1.7.7-py3-none-any.whl (130 kB)
Installing collected packages: bandit
Successfully installed bandit-1.7.7

If you use Python 3 on some systems, you might need pip3 instead. Try this if the first command fails:


pip3 install bandit

Once installed, verify it works. Run:


bandit --version

You should see the version number. Now Bandit is ready to use.

Installing Bandit in a Virtual Environment

It is good practice to use a virtual environment. This keeps your projects separate. First, create a virtual environment:


python -m venv myenv

Activate it. On Windows, use:


myenv\Scripts\activate

On macOS or Linux, use:


source myenv/bin/activate

Now install Bandit inside the environment:


pip install bandit

This keeps your global Python clean. When you finish, deactivate with:


deactivate

Using virtual environments is a smart habit for any Python developer.

How to Use Bandit

After installation, you can scan a Python file. Create a test file named example.py with this code:


# example.py
import os

password = "secret123"  # Hardcoded password

def connect_db():
    db_password = os.getenv("DB_PASSWORD")
    # Connect to database
    print("Connecting with password:", db_password)

Now run Bandit on this file:


bandit example.py

You will see output like this:


[main]  INFO    profile include tests: None
[main]  INFO    profile exclude tests: None
[main]  INFO    cli include tests: None
[main]  INFO    cli exclude tests: None
[main]  INFO    running on Python 3.11.0
Run started:2024-01-15 10:00:00.123456

Test results:
>> Issue: [B105:hardcoded_password_string] Possible hardcoded password: 'secret123'
   Severity: Medium   Confidence: Medium
   Location: example.py:3
   More Info: https://bandit.readthedocs.io/en/latest/plugins/b105_hardcoded_password_string.html

--------------------------------------------------
Code scanned:
    Total lines of code: 7
    Total lines skipped (#nosec): 0

Run metrics:
    Total issues (by severity):
        Undefined: 0
        Low: 0
        Medium: 1
        High: 0
    Total issues (by confidence):
        Undefined: 0
        Low: 0
        Medium: 1
        High: 0
Files skipped (0):

Bandit found the hardcoded password. It tells you the severity and location. This helps you fix the issue quickly.

You can also scan an entire directory. Use the -r flag for recursive scanning:


bandit -r my_project/

This checks all Python files in the folder. It is useful for larger projects.

Understanding Bandit Results

Bandit gives you clear reports. Each issue has a severity level: Low, Medium, or High. It also shows confidence in the finding. Use this to prioritize fixes.

You can ignore false positives by adding a comment. For example:


password = "test"  # nosec

The # nosec comment tells Bandit to skip that line. Use it carefully only when you are sure the code is safe.

Integrating Bandit into Your Workflow

Bandit works well with continuous integration tools. You can add it to your build pipeline. For example, in a GitHub Actions workflow, add this step:


- name: Run Bandit
  run: pip install bandit && bandit -r . -f json -o bandit-report.json

This runs Bandit on every push. It helps catch security issues before they reach production.

You can also combine Bandit with other tools. For instance, use it alongside pytest for comprehensive testing. This makes your code more robust.

Troubleshooting Installation

Sometimes installation fails. Common issues include network problems or permission errors. If you see a permission error, try installing with --user:


pip install --user bandit

This installs Bandit for your user only. It avoids system-wide changes.

Another issue is an old pip version. Upgrade pip first:


pip install --upgrade pip

Then install Bandit again. If problems persist, check your internet connection or try a different Python version.

Conclusion

Installing Bandit in Python is simple. Use pip or pip3 to get started. Run it on your files to find security issues fast. Remember to use virtual environments for cleaner projects. Bandit is a valuable tool for any Python developer. It helps you write safer code and avoid common mistakes. Start using Bandit today to protect your applications.