Databricks Serverless compute requires environments to manage dependencies instead of directly specifying libraries. This guide shows you how to create and use environments with dbx_test.
- Serverless Requirement: Serverless tasks don't support the
librariesfield - Reusability: Environments can be shared across multiple jobs and notebooks
- Performance: Dependencies are pre-installed and cached for faster execution
- Consistency: Same environment across all serverless tasks
You can create environments via the Databricks UI or API.
- Go to your Databricks workspace
- Click on Compute in the sidebar
- Click on the Environments tab
- Click Create Environment
- Configure:
- Name:
dbx_test_env - Python Version:
3.10or higher - Libraries: Add
git+https://github.com/jsparhamii/dbx_test.git
- Name:
- Click Create
# Create environment using Databricks SDK
databricks environments create \
--name dbx_test_env \
--client aws-west \
--spec '{
"dependencies": [
"git+https://github.com/jsparhamii/dbx_test.git",
"pandas>=2.0.0"
]
}'Update your config/test_config.yml:
workspace:
profile: "aws-west"
cluster:
# Use the environment name you created
environment_key: "dbx_test_env"
# Serverless is used by default when no cluster settings are specified
execution:
timeout: 600
reporting:
output_dir: ".dbx-test-results"
formats:
- "console"
- "junit"dbx_test run --remote \
--tests-dir /Workspace/Users/youruser@company.com/tests \
--profile aws-westThat's it! Your tests will run on serverless compute with the environment's dependencies.
The easiest way for quick setup:
- Navigate to Compute → Environments
- Click Create Environment
- Fill in:
- Name:
dbx_test_env - Description: Environment for dbx_test framework
- Python Version:
3.10+ - Libraries:
git+https://github.com/jsparhamii/dbx_test.git- Any other dependencies your tests need
- Name:
For automation and CI/CD:
from databricks.sdk import WorkspaceClient
w = WorkspaceClient(profile="aws-west")
# Create environment
env = w.environments.create(
name="dbx_test_env",
spec={
"dependencies": [
"git+https://github.com/jsparhamii/dbx_test.git",
"pandas>=2.0.0",
"numpy>=1.24.0",
],
"python_version": "3.10",
}
)
print(f"Environment created: {env.name}")Include in your databricks.yml:
environments:
dbx_test_env:
spec:
dependencies:
- "git+https://github.com/jsparhamii/dbx_test.git"
- "pandas>=2.0.0"
python_version: "3.10"
resources:
jobs:
my_tests:
tasks:
- task_key: run_tests
environment_key: dbx_test_env
# ...rest of task configEnvironments support various dependency types:
# In Databricks Environment Configuration
dependencies:
# PyPI packages
- "dbx_test==0.1.0"
- "pandas>=2.0.0"
- "numpy==1.24.3"
# Git repositories
- "git+https://github.com/jsparhamii/dbx_test.git"
- "git+https://github.com/user/repo.git@branch"
- "git+https://github.com/user/repo.git@v1.0.0"
# Wheel files from DBFS
- "/dbfs/FileStore/wheels/custom_lib-1.0.0-py3-none-any.whl"Specify the Python version:
python_version: "3.10" # or "3.11"You can also set environment variables:
environment_variables:
MY_CONFIG: "value"
API_ENDPOINT: "https://api.example.com"from databricks.sdk import WorkspaceClient
w = WorkspaceClient(profile="aws-west")
env = w.environments.create(
name="ml_test_env",
spec={
"dependencies": [
# Testing framework
"git+https://github.com/jsparhamii/dbx_test.git",
# Data science stack
"pandas==2.0.3",
"numpy==1.24.3",
"scikit-learn==1.3.0",
# Databricks
"databricks-sdk>=0.20.0",
# Custom library from DBFS
"/dbfs/FileStore/wheels/company_lib-1.0.0-py3-none-any.whl",
],
"python_version": "3.10",
"environment_variables": {
"TEST_MODE": "true",
}
}
)
print(f"✓ Environment created: {env.name}")# config/test_config.yml
workspace:
profile: "aws-west"
cluster:
environment_key: "ml_test_env"
execution:
timeout: 900
parallel: true
max_parallel_jobs: 5
reporting:
output_dir: ".dbx-test-results"
formats:
- "console"
- "junit"
- "html"dbx_test run --remote \
--tests-dir /Workspace/Users/james.parham@databricks.com/.bundle/my_project/dev/files/tests \
--profile aws-west \
--verbosecluster:
environment_key: "dbx_test_env"
# No other cluster settings = serverlessPros:
- ✅ Fast startup (no cluster creation)
- ✅ Cost-effective (pay per use)
- ✅ Auto-scaling
- ✅ No cluster management
Cons:
- ❌ Requires pre-created environment
- ❌ Limited customization
cluster:
libraries:
- pypi:
package: "git+https://github.com/jsparhamii/dbx_test.git"
# Option A: Use existing cluster
cluster_id: "0123-456789-abc123"
# Option B: Create new cluster
size: "S"
spark_version: "14.3.x-scala2.12"Pros:
- ✅ More control over cluster configuration
- ✅ Can use custom node types
- ✅ Works with complex library setups
Cons:
- ❌ Slower startup (cluster creation)
- ❌ Higher cost (minimum cluster time)
- ❌ Requires cluster management
If using serverless (recommended for most use cases):
cluster:
environment_key: "your_environment_name"
# Don't specify libraries hereIf using a dedicated cluster:
cluster:
cluster_id: "0123-456789-abc123" # or size: "S"
libraries:
- pypi:
package: "git+https://github.com/jsparhamii/dbx_test.git"Create versioned environments for reproducibility:
dbx_test_env_v1dbx_test_env_v2dbx_test_env_prod
Environments can be reused:
# Team A
cluster:
environment_key: "shared_ml_env"
# Team B
cluster:
environment_key: "shared_ml_env"Before creating an environment, test dependencies locally:
pip install git+https://github.com/jsparhamii/dbx_test.git pandas
python -m pytest tests/Error: Environment 'dbx_test_env' does not exist
Solution:
- Check environment name:
databricks environments list --profile aws-west - Verify you're using the correct workspace/profile
- Create the environment if it doesn't exist
Error: Dependency resolution conflicts
Solution:
# Be explicit with versions
dependencies:
- "pandas==2.0.3" # Not "pandas>=2.0.0"
- "numpy==1.24.3" # Not "numpy"Error: ModuleNotFoundError: No module named 'dbx_test'
Solution:
- Verify environment includes
dbx_test:databricks environments get dbx_test_env --profile aws-west
- Check environment status (should be "READY")
- Update environment if needed
Cause: Environment not ready or needs rebuilding
Solution:
- Environments are cached after first use
- Subsequent runs will be faster
- Consider using a persistent cluster for development
Before (won't work with serverless):
cluster:
libraries:
- pypi:
package: "git+https://github.com/jsparhamii/dbx_test.git"After (works with serverless):
cluster:
environment_key: "dbx_test_env"Migration Steps:
- Create environment with your libraries
- Update config to use
environment_key - Remove
librariessection (only needed for clusters) - Test with
--verboseflag
| Feature | Serverless + Environment | Cluster + Libraries |
|---|---|---|
| Setup | Create environment once | Configure each time |
| Startup | Fast (~30s) | Slower (2-5 min) |
| Cost | Pay per use | Minimum cluster time |
| Management | Minimal | More involved |
| Best For | Production, CI/CD | Development, debugging |
Recommendation: Use serverless with environments for production workloads, and clusters with libraries for development.
- Create your environment in Databricks workspace
- Update config with
environment_key - Run tests with
--remoteflag - Monitor execution in Databricks UI
Happy testing! 🚀