Releases: alelom/python-package-folder
Added support for automated versioning number
Several fixes and improvements
Fix: Add file exclusion patterns to prevent non-package files in distributions
When building subfolders as separate packages, the tool was including
unnecessary files from the project root (e.g., .cursor/, .github/, .vscode/,
data/, docs/, references/, reports/, scripts/, Dockerfile, etc.) in the
published distribution.
This fix adds comprehensive file exclusion patterns to the temporary
pyproject.toml created for subfolder builds using hatchling's paths-exclude
configuration. The exclusions cover:
- IDE/config directories: .cursor/, .github/, .vscode/, .idea/
- Non-package directories: data/, docs/, references/, reports/, scripts/, tests/
- Build artifacts: dist/, build/, *.egg-info/, pycache/, .pytest_cache/
- Other files: Dockerfile, .gitignore, .gitattributes, .pylintrc, etc.
The exclusion patterns are added in both code paths:
- When creating a temporary pyproject.toml from the parent
- When using an existing subfolder pyproject.toml
Changes:
- Added [tool.hatch.build] section with paths-exclude to subfolder_build.py
- Updated _modify_pyproject_string() to include exclusions
- Updated _adjust_subfolder_pyproject_packages_path() to include exclusions
- Added unit tests to verify exclusion patterns are present
Tests:
- Added test_file_exclusion_patterns_added() to verify exclusions when
creating from parent pyproject.toml - Added test_file_exclusion_patterns_with_subfolder_pyproject() to verify
exclusions when subfolder has its own pyproject.toml - All 114 tests pass
This ensures that only the intended package code is included in the
distribution, preventing bloated packages with unnecessary files.
Fix: Use only-include to prevent non-package files in subfolder distributions
When building subfolders as separate packages, the published tar.gz files
were including unnecessary files from the project root (e.g., .cursor/,
.github/, .vscode/, data/, docs/, references/, reports/, scripts/,
Dockerfile, etc.) instead of only the subfolder code.
The initial fix attempted to use paths-exclude in [tool.hatch.build], but
this approach didn't work because hatchling was still including files from
the project root during source distribution builds.
Solution:
- Replaced paths-exclude with only-include in [tool.hatch.build.targets.sdist]
- Explicitly include only the subfolder directory and necessary files:
- The subfolder package directory (e.g., "src/integration/empty_drawing_detection")
- pyproject.toml
- README files (README.md, README.rst, README.txt, README)
Changes:
- Updated _modify_pyproject_string() to use only-include for sdist targets
- Updated _adjust_subfolder_pyproject_packages_path() to use only-include
- Updated unit tests to verify only-include configuration instead of paths-exclude
- Both code paths (parent pyproject.toml and subfolder pyproject.toml) now use only-include
Testing:
- Updated test_file_exclusion_patterns_added() to verify only-include
- Updated test_file_exclusion_patterns_with_subfolder_pyproject() to verify only-include
- All 114 tests pass
This ensures that only the intended package code is included in the
distribution, preventing bloated packages with unnecessary project root files.
Fix: Convert absolute imports of copied dependencies to relative imports
When building subfolders as separate packages, external dependencies are
copied into the subfolder. However, the original files in the subfolder
still use absolute imports (e.g., from _shared.image_utils import ...),
which fail when the package is installed because those modules are now
part of the package and should be imported relatively.
This fix automatically converts absolute imports of copied dependencies
to relative imports during the build process for subfolder builds:
from _shared.image_utils import ...→from ._shared.image_utils import ...import _globals→from . import _globals- Only modifies imports of copied dependencies, not stdlib or third-party
- Only modifies files in the original subfolder, not the copied dependency files
- Restores original imports after build completes
Changes:
- Added
_convert_copied_dependency_imports_to_relative()method to BuildManager - Uses AST parsing for accurate import detection and modification
- Tracks modified files in
_modified_import_filesdictionary - Restores original imports in
cleanup()method - Integrated into
prepare_build()for subfolder builds only
Testing:
- Added
test_convert_copied_dependency_imports_to_relative()unit test - Verifies imports are converted to relative format
- Verifies stdlib imports are not modified
- Verifies original imports are restored after cleanup
- All 115 tests pass
This ensures that published subfolder packages can correctly import their
copied dependencies when installed, fixing ModuleNotFoundError issues.
Fix import handling for subfolder builds with copied dependencies
This commit addresses two import issues that occur when building
subfolder packages with external dependencies:
-
Fix relative imports in copied dependency files:
- When external dependencies are copied into the subfolder, their
relative imports (e.g.,from ._shared.shared_dataclasses) break
because the file structure changes - Added
_fix_relative_imports_in_copied_files()to convert these
relative imports to absolute imports based on the target location
- When external dependencies are copied into the subfolder, their
-
Convert local file imports to relative:
- Imports of local files within the subfolder (e.g.,
from detect_empty_drawings_utils import ...) need to be converted to
relative imports (e.g.,from .detect_empty_drawings_utils import ...) - Updated
_convert_imports_to_relative()(renamed from
_convert_copied_dependency_imports_to_relative) to handle both
copied dependencies and local files
- Imports of local files within the subfolder (e.g.,
The implementation:
- Fixes relative imports in copied files first (converts to absolute)
- Then converts absolute imports of both copied dependencies and local
files to relative imports in the subfolder's original files - Tracks all modified files and restores them during cleanup
This resolves ImportError issues like "attempted relative import with
no known parent package" that occurred when published packages tried
to import copied dependencies.
Tests updated to verify both behaviors.
Full Changelog: v3.0.2...v3.1.3
Make directories recognizable as Python packages, allowing type checkers to resolve imports and type annotations
Print the temporary pyproject.toml content for debugging
Preserve the original directory structure when copying external depencies
Enhanced file resolution in analyzer.py
Updated resolve_local_import() to check nested subdirectories (up to 2 levels deep) when searching for local files
Finds files in paths like src/data/spreadsheet_creation/spreadsheet_formatting_dataclasses.py
Checks all subdirectories of parent directories, not just common ones like _shared
Stricter dependency extraction in manager.py:
Only adds ambiguous imports as dependencies if they can be verified as installed packages
If an import can't be verified as a package, it's skipped (likely a local file that wasn't resolved)
Prevents adding local files as third-party dependencies
Added tests:
Created tests/test_spreadsheet_creation_imports.py to verify files in nested subdirectories are correctly identified as external dependencies and copied, not added as third-party packages
Full Changelog: v2.0.6...v2.0.7
Add _shared to identifiers
Full Changelog: v2.0.4...v2.0.6
Added more logging info
Full Changelog: v2.0.4...v2.0.5
Add third-party dependency detection and inclusion for subfolder builds
- Extract third-party dependencies from all Python files (including copied external dependencies)
- Add dependencies to temporary pyproject.toml's [project.dependencies] section
- Handle cases where dependencies aren't installed in build environment by treating ambiguous imports as third-party
- Add test to verify third-party dependencies are correctly added
- Fix test cleanup assertion to account for pre-existing files
Full Changelog: v1.4.1...v2.0.4