orchestr8.execution_runtime
create_project
function¶
Create a uv project from a python script.
create_project(
"/path/to/script.py",
id="project_id", # Not required, defaults to script name.
requirements=["package=0.0.0"], # Set `True` to auto generate
)
Refer orchestr8.execution_runtime.package_utils.generate_requirements
to know more
about auto-generation of requirements from python source file and dependency_overrides
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
__script
|
str | Path
|
The python script to create project from |
required |
id
|
str | None
|
The id to use for the project. Defaults to the script name |
None
|
requirements
|
List[str] | Literal[True] | None
|
List of requirement strings (e.g., ["package==1.0.0"]) or True to auto-generate |
None
|
dependency_overrides
|
Dict[str, Dependency] | None
|
Depedency overrides. Applicable only if requirements=True |
None
|
force
|
bool
|
Whether to force create the project even if it's already available |
False
|
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the script file is not found |
Source code in orchestr8/execution_runtime/__init__.py
create_execution_runtime
function¶
Create execution runtime instance for running Python scripts and created projects.
runtime = create_execution_runtime(isolate=True, python_tag="3.10-alpine3.20")
# Run a script with auto-generated requirements
output = runtime.run_script('script1.py', '-p1', 'value1', requirements=True)
# Run a script with pre-defined requirements
output = runtime.run_script('script2.py', requirements=["requests==2.25.1"])
# Run a created project
output = runtime.run_project('project_id', '--param1', 'value1')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
isolate
|
bool
|
Whether to isolate the runtime. Defaults to False |
False
|
python_tag
|
str | None
|
Python image tag. Defaults to 3.10-alpine3.20. Applicable, if |
None
|
docker_config
|
Any
|
Docker client configuration. Applicable, if |
{}
|
Returns:
Type | Description |
---|---|
ExecutionRuntime | IsolatedExecutionRuntime
|
Runtime instance |
Source code in orchestr8/execution_runtime/__init__.py
ExecutionRuntime (host-based)¶
Interface for running Python scripts and created projects in the host machine.
# Create a host execution runtime instance
runtime = ExecutionRuntime()
# Run a script with auto-generated requirements
output = runtime.run_script('script1.py', '-p1', 'value1', requirements=True)
# Run a script with pre-defined requirements
output = runtime.run_script('script2.py', requirements=["requests==2.25.1"])
# Run a created project
output = runtime.run_project('project_id', '--param1', 'value1')
Source code in orchestr8/execution_runtime/__init__.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
|
shell: Shell
property
¶
The shell instance.
run_project
¶
Run the project with the given id.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
__id
|
str
|
The id of the project to run |
required |
args
|
SupportsStr
|
The arguments to pass to the script associated with the project |
()
|
env
|
Dict[str, str] | None
|
Environment variables required by the project |
None
|
Returns:
Type | Description |
---|---|
str | None
|
The output of the script |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the project with the given id does not exist |
Source code in orchestr8/execution_runtime/__init__.py
run_script
¶
Run a python script inside on-demand environment.
Reference: https://docs.astral.sh/uv/guides/scripts/#running-a-script-with-dependencies
To know more about auto-generation of requirements from python source
file, refer orchestr8.execution_runtime.package_utils.generate_requirements
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
__script
|
str | Path | BytesIO
|
The python script to run |
required |
args
|
SupportsStr
|
The arguments to pass to the script |
()
|
env
|
Dict[str, str] | None
|
Environment variables required by the script |
None
|
requirements
|
List[str] | Literal[True] | None
|
List of requirement strings (e.g., ["package==1.0.0"]) or True to auto-generate |
None
|
dependency_overrides
|
Dict[str, Dependency] | None
|
Depedency overrides. Applicable only if requirements=True |
None
|
Returns:
Type | Description |
---|---|
str | None
|
The output of the script |
Source code in orchestr8/execution_runtime/__init__.py
IsolatedExecutionRuntime¶
Interface for running Python scripts and created projects in isolated environments.
# Create an isolated execution runtime instance
runtime = IsolatedExecutionRuntime(isolate=True, python_tag="3.10-alpine3.20")
# Run a script with auto-generated requirements
output = runtime.run_script('script1.py', '-p1', 'value1', requirements=True)
# Run a script with pre-defined requirements
output = runtime.run_script('script2.py', requirements=["requests==2.25.1"])
# Run a created project
output = runtime.run_project('project_id', '--param1', 'value1')
Source code in orchestr8/execution_runtime/__init__.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
|
shell: IsolatedShell
property
¶
The shell instance.
run_project
¶
Run the project with the given id.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
__id
|
str
|
The id of the project to run |
required |
args
|
SupportsStr
|
The arguments to pass to the script associated with the project |
()
|
env
|
Dict[str, str] | None
|
Environment variables required by the project |
None
|
Returns:
Type | Description |
---|---|
str | None
|
The output of the script |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the project with the given id does not exist |
Source code in orchestr8/execution_runtime/__init__.py
run_script
¶
Run a python script inside on-demand environment.
Reference: https://docs.astral.sh/uv/guides/scripts/#running-a-script-with-dependencies
To know more about auto-generation of requirements from python source
file, refer orchestr8.execution_runtime.package_utils.generate_requirements
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
__script
|
str | Path | BytesIO
|
The python script to run. |
required |
args
|
SupportsStr
|
The arguments to pass to the script |
()
|
env
|
Dict[str, str] | None
|
Environment variables required by the script |
None
|
requirements
|
List[str] | Literal[True] | None
|
List of requirement strings (e.g., ["package==1.0.0"]) or True to auto-generate |
None
|
dependency_overrides
|
Dict[str, Dependency] | None
|
Depedency overrides. Applicable only if requirements=True |
None
|
Returns:
Type | Description |
---|---|
str | None
|
The output of the script |
Source code in orchestr8/execution_runtime/__init__.py
package_utils¶
Dependency
¶
Dependency specification with package name and version requirements.
Source code in orchestr8/execution_runtime/package_utils.py
ReleaseInfo
¶
Specifier
¶
Version specifier.
Represents a package version specifier with an operator and version string.
Source code in orchestr8/execution_runtime/package_utils.py
extract_module_names
¶
Extract imported module names from a Python script or AST Module.
Analyzes Python source code to find all imported module names, including those within conditional statements (if/else) and try/except blocks. For 'import' statements, only the root module name is extracted (e.g., 'pandas' from 'pandas.DataFrame'). For 'from' imports, the module being imported from is extracted.
```python script = Path('example.py') # Contains: import pandas as pd; from numpy import array extract_module_names(script)
output: ['pandas', 'numpy']¶
Args: script: Either a Path to a Python file or an ast.Module object
Returns: List of unique module names found in the script
Raises: FileNotFoundError: If the script path does not exist
Source code in orchestr8/execution_runtime/package_utils.py
generate_requirements
¶
Generate a list of Python package requirements from module names.
For extracting module names from a Python script, use:
from orchestr8.runtime.utils import extract_module_names
module_names = extract_module_names("/path/to/script.py")
The function follows a systematic approach to resolving package dependencies:
First, standard library modules are automatically excluded from the processing. The remaining modules are then searched among globally installed packages. When a module is found in installed packages, its package name and version are used.
If a module is not installed locally, the function searches the PyPI package index. When a package is found on PyPI, its latest version is utilized. If no package can be found, the function raises an error.
The function provides flexibility through the dependency_overrides
parameter.
This is particularly useful in scenarios where:
- The package name differs from the module name
- Custom version specifications are required
A prime example is the yaml
module, which corresponds to the PyYAML
package.
Through dependency overrides, you can specify custom package names or version constraints.
The function performs comprehensive validation: - Checks if specified versions exist in package releases - Validates version specifiers against available package versions - Ensures precise and correct dependency specification
generate_requirements(['pandas', 'numpy'])
# output: ['pandas==2.0.0', 'numpy==1.24.0']
overrides = {
'yaml': {
'package_name': 'PyYAML',
'specifiers': [{'version': '6.0', 'op': '=='}]
}
}
generate_requirements(['yaml'], overrides)
# output: ['PyYAML==6.0']
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module_names
|
List[str]
|
List of Python module names to process |
required |
dependency_overrides
|
Dict[str, Dependency] | None
|
Dependency overrides for specific modules |
None
|
Returns:
Type | Description |
---|---|
List[str]
|
List of requirement strings (e.g., ["package==1.0.0"]) |
Raises:
Type | Description |
---|---|
ValueError
|
If specified package name or versions in overrides are invalid or not found |
LookupError
|
If a package cannot be located on PyPI |
ConnectionError
|
If network-related issues occur when fetching package information |
Source code in orchestr8/execution_runtime/package_utils.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
|
get_module_mapped_packages
¶
Map Python module names to their corresponding package information.
Creates a mapping of module names to dictionaries containing package names and their versions based on installed distributions.
Returns:
Type | Description |
---|---|
Dict[str, Dict[str, str]]
|
Dictionary mapping module names to {package_name: version} dictionaries |
Source code in orchestr8/execution_runtime/package_utils.py
get_package_release_info
¶
Fetch package release information from PyPI or custom package index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
__name
|
str
|
Name of the package to look up |
required |
resolver_url
|
str | None
|
Custom package index URL. Defaults to PyPI |
None
|
urllib_request_kwargs
|
Any
|
Additional keyword arguments for urllib.request.Request |
{}
|
Returns:
Type | Description |
---|---|
ReleaseInfo
|
Dictionary containing 'ids' (list of versions) and 'latest' (latest version) |
Raises:
Type | Description |
---|---|
LookupError
|
If the package is not found |
ConnectionError
|
If there are network issues or invalid responses |
Source code in orchestr8/execution_runtime/package_utils.py
get_stdlib_modules
¶
Get a set of all Python standard library module names.
Returns:
Type | Description |
---|---|
FrozenSet[str]
|
Set of standard library module names read from stdlib.txt |