Skip to content

Include PYTHONPATH directories in package scanning#6

Merged
zackverham merged 6 commits intoposit-dev:mainfrom
zackverham:fix/include-pythonpath-in-package-scanning
Apr 27, 2026
Merged

Include PYTHONPATH directories in package scanning#6
zackverham merged 6 commits intoposit-dev:mainfrom
zackverham:fix/include-pythonpath-in-package-scanning

Conversation

@zackverham
Copy link
Copy Markdown
Contributor

@zackverham zackverham commented Apr 24, 2026

Summary

When PYTHONPATH is set (e.g., via supervisor.sh environment modules in Posit Connect), packages installed in those directories are visible to pip but not to picopip's get_site_package_paths(). This causes false "missing packages" errors when picopip is used to validate that a venv contains all required packages.

This extends get_site_package_paths() to also scan directories from the PYTHONPATH environment variable, gated behind the existing include_system_packages flag for consistency with how system site-packages are handled.

Context

This fixes a regression in Posit Connect where Python apps fail after upgrade when packages exist in global PYTHONPATH directories.

Changes

  • src/picopip.py: After scanning system site-packages, also iterate over PYTHONPATH entries and include any that exist as directories. Uses Path.resolve() to normalize paths for deduplication.
  • tests/test_packages_discovery.py: 9 new tests covering inclusion, exclusion, empty/unset/nonexistent/multiple PYTHONPATH values, and end-to-end package discovery.

Test results

All 56 tests pass (17 existing discovery + 9 new PYTHONPATH + 30 other).

🤖 Generated with Claude Code

zackverham and others added 5 commits April 24, 2026 14:09
When PYTHONPATH is set (e.g., via supervisor.sh environment modules),
packages installed in those directories are visible to pip but not to
picopip's get_site_package_paths(). This causes false "missing packages"
errors when picopip is used to validate that a venv contains all
required packages.

This extends get_site_package_paths() to also scan directories from
the PYTHONPATH environment variable, gated behind the existing
include_system_packages flag for consistency with system site-packages.

Ref: posit-dev/connect#38951

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Rename unused `site` to `_site` in test destructuring (RUF059)
- Move `import os` to module level (PLC0415)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Extract PYTHONPATH scanning into a dedicated helper function to stay
within ruff's complexity limits (C901 max 12, PLR0912 max 12 branches).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The for/if dedup loops for system packages and PYTHONPATH each count as
2 branches. Extract into a shared _extend_unique helper to bring
get_site_package_paths under ruff's C901/PLR0912 limits.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Comment thread src/picopip.py Outdated
Co-authored-by: E. David Aja <edavidaja@hey.com>
Comment thread src/picopip.py
Comment on lines +145 to +146
if pp.exists() and pp.is_dir():
scan_paths.append(pp)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest docs says...

In addition to normal directories, individual PYTHONPATH entries may refer to zipfiles containing pure Python modules (in either source or compiled form). Extension modules cannot be imported from zipfiles.

Do we need to support zipfiles here?

https://docs.python.org/3/using/cmdline.html#environment-variables

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

potentially - though I'd prefer to follow-up w/ a new issue and get this merged, we just tested in their dev environment and the patch worked.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#7

@zackverham zackverham merged commit 8b3a278 into posit-dev:main Apr 27, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants