Skip to main content

Detect Python Shadowed Modules

Detect conflicting directory names across Python's sys.path that may be shadowing your installed packages.

Common cases where this happened:

  • Using an editable package. All folders within the editable package dir (including tests) are added to sys.path and there is no way to define precedence (at least, as of 2026-01-24 when using uv)
  • A package that has been improperly packaged with something like a tests directory.
    • There are two different types of Python package distributions: a source distribution and a built distribution. A source distribution can be downloaded on PyPy using the tar.gzip, where a built distribution is the wheel with the extension whl. These distributions may not use the same distribution script and can contain different contents.

If you like this project, you'll probably enjoy uv-development-toggle

Problem

When Python imports a module, it searches directories in sys.path order. If the same directory name exists in multiple sys.path entries, only the first one is used—potentially shadowing packages you intended to import. This can lead to confusing import errors and unexpected behavior.

Example Conflict Scenario

sys.path = ["/project", "/usr/lib/python3/site-packages"]

If both paths contain a requests/ directory:

  • /project/requests/ ← This shadows the real package
  • /usr/lib/python3/site-packages/requests/

When you import requests, Python will use /project/requests/ instead of the installed package, which is likely not what you want!

Installation

uv add detect-shadowed-modules

Or, you can just use via uvx:

uvx detect-shadowed-modules@latest --help

Usage

Command Line

# Print conflicts to stdout with scanning progress
detect-shadowed-modules

detect-shadowed-modules --json

# Quiet mode (conflicts only, no progress messages)
detect-shadowed-modules -q

As a Python Module

import detect_shadowed_modules

# Find conflicts
conflicts = detect_shadowed_modules.find_conflicts()

# Generate a human-readable report
report = detect_shadowed_modules.format_report(conflicts)
print(report)

# Or get JSON output
json_output = detect_shadowed_modules.format_json(conflicts)
print(json_output)

Output Examples

Human-Readable Format

Conflicting directory names detected:

These directories exist in multiple sys.path locations. The first
location listed will shadow the others during import.

  requests/
  → /home/user/myproject/requests (shadows others)
    /usr/lib/python3/site-packages/requests [requests] (shadowed)

Searched directories (50 total):
  /home/user/myproject [editable]
  /home/user/.venv/lib/python3.13/site-packages
  /usr/lib/python3/site-packages
  ...

JSON Format

{
  "shadowed": [
    {
      "path": "/usr/lib/python3/site-packages/requests",
      "shadowed_by": "/home/user/myproject/requests",
      "owner": "requests"
    }
  ],
  "searched_paths": [
    {
      "path": "/home/user/myproject",
      "editable": true
    },
    {
      "path": "/home/user/.venv/lib/python3.13/site-packages",
      "editable": false
    },
    {
      "path": "/usr/lib/python3/site-packages",
      "editable": false
    }
  ]
}

How It Works

The tool scans all directories in sys.path and:

  1. Automatically detects editable packages installed via uv (using uv pip list -e)
  2. Combines editable package paths with sys.path (editable paths take precedence)
  3. Identifies subdirectories in each path
  4. Detects when the same directory name appears in multiple locations
  5. Reports which packages are being shadowed and by what
  6. Attempts to identify the installed package that owns each directory

Excluded from scanning:

  • Hidden directories (starting with .)
  • __pycache__ directories
  • Package metadata directories (.dist-info, .egg-info)

Debugging Import Issues

If you suspect a module is being imported from the wrong location, you can check where Python is actually loading it from:

python -c 'import <module>; print(<module>.__file__)'

For example:

python -c 'import requests; print(requests.__file__)'
# Output: /home/user/myproject/requests/__init__.py

This will show you the exact file path Python is using for the import, helping you confirm if shadowing is occurring.

Exit Codes

  • 0: No conflicts found
  • 1: Conflicts detected

This makes it easy to use in CI/CD pipelines or pre-commit hooks.

MIT License

Metadata

Release files for detect-shadowed-modules 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for detect-shadowed-modules 0.2.1
File Size Uploaded Provenance Actions
detect_shadowed_modules-0.2.1.tar.gz 7.5 kB Without provenance Details for detect_shadowed_modules-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for detect-shadowed-modules 0.2.1
File Interpreter ABI Platform Actions
detect_shadowed_modules-0.2.1-py3-none-any.whl Python 3 none any Details for detect_shadowed_modules-0.2.1-py3-none-any.whl

Total release size: 16.2 kB

Release files / detect_shadowed_modules-0.2.1.tar.gz

Download URL detect_shadowed_modules-0.2.1.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
08ff77b483fa4f694f8416af6e618b470e92e2b46dc34a0e310667d459ae120a
BLAKE2b-256 checksum
How to use checksums
5141d11c70eb38201acae40641896da781a89370bb380222c1b67b23d6cbc645
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / detect_shadowed_modules-0.2.1-py3-none-any.whl

Download URL detect_shadowed_modules-0.2.1-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
502ae97cabcc2fc9567bb3138c7893b413be6ef0771cb4becbad55afbb0852ae
BLAKE2b-256 checksum
How to use checksums
8615c549f61193299ee7a708483cc1ceb2c7323657a6a8775dbd077421f6395c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for detect-shadowed-modules 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page