Pre-Building Test Workflow
Test package installation before deployment.
Overviewโ
This workflow validates that the built Python package can be installed and imported correctly before publishing to PyPI, preventing broken package deployments.
When to Useโ
- โ You want to validate package installation
- โ You need to test package imports
- โ You're preparing for PyPI deployment
- โ You want to catch packaging errors early
Inputsโ
| Input | Type | Default | Description |
|---|---|---|---|
package_path | string | 'dist/' | Path to built packages |
python_version | string | '3.11' | Python version for testing |
test_import | boolean | true | Test package import |
package_name | string | '' | Package name to import |
Outputsโ
| Output | Description |
|---|---|
installation_success | Whether installation succeeded |
import_success | Whether import test succeeded |
Usage Examplesโ
Basic Usageโ
jobs:
build:
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_python_package.yaml@master
test-install:
needs: build
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_pre-building_test.yaml@master
with:
package_name: my_package
Multi-Version Testingโ
jobs:
build:
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_python_package.yaml@master
test-install-py311:
needs: build
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_pre-building_test.yaml@master
with:
python_version: '3.11'
package_name: my_package
test-install-py312:
needs: build
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_pre-building_test.yaml@master
with:
python_version: '3.12'
package_name: my_package
Complete Build and Deploy Pipelineโ
jobs:
build:
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_python_package.yaml@master
test-install:
needs: build
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_pre-building_test.yaml@master
with:
package_name: my_package
publish:
needs: test-install
if: needs.test-install.outputs.installation_success == 'true'
uses: Chisanan232/GitHub-Action_Reusable_Workflows-Python/.github/workflows/rw_push_pypi.yaml@master
secrets:
pypi_token: ${{ secrets.PYPI_API_TOKEN }}
How It Worksโ
Step 1: Download Packageโ
Downloads built package from artifacts:
- uses: actions/download-artifact@v4
with:
name: python-package-distributions
path: dist/
Step 2: Install Packageโ
Installs the wheel package:
pip install dist/*.whl
Step 3: Test Importโ
Tests that package can be imported:
import sys
try:
import my_package
print(f"Successfully imported {my_package.__name__}")
print(f"Version: {my_package.__version__}")
sys.exit(0)
except ImportError as e:
print(f"Failed to import: {e}")
sys.exit(1)
Step 4: Validate Metadataโ
Checks package metadata:
pip show my-package
Best Practicesโ
1. Test All Supported Python Versionsโ
strategy:
matrix:
python-version: ['3.9', '3.10', '3.11', '3.12']
2. Test Both Wheel and Source Distributionโ
- name: Test wheel installation
run: pip install dist/*.whl
- name: Test sdist installation
run: pip install dist/*.tar.gz
3. Validate Package Entry Pointsโ
# Test CLI entry points
import subprocess
result = subprocess.run(['my-command', '--version'], capture_output=True)
assert result.returncode == 0
4. Check Dependenciesโ
pip check # Verify no dependency conflicts
Troubleshootingโ
Installation Failsโ
Symptoms:
- Package installation errors
- Dependency conflicts
Solutions:
-
Check package dependencies:
[project]
dependencies = [
"requests>=2.28.0",
] -
Verify package structure
-
Test locally:
pip install dist/*.whl
Import Failsโ
Symptoms:
- ImportError after installation
- Module not found
Solutions:
-
Verify package name matches:
import my_package # Must match package name -
Check package structure:
src/
โโโ my_package/
โโโ __init__.py -
Ensure
__init__.pyexists
Related Workflowsโ
- rw_python_package - Build packages
- rw_push_pypi - Publish to PyPI
- rw_checking_deployment_state - Check deployment