-
Notifications
You must be signed in to change notification settings - Fork 4
feat: ensure modules next to tesseract_api.py can always be imported #400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a37292b
ensure modules next to tesseract_api.py can always be imported
dionhaefner acb6b81
Merge branch 'main' into dion/local-module-support
dionhaefner 2c8cca0
add test
dionhaefner b2fd4af
add unit test, update docs
dionhaefner 91e7212
add missing files
dionhaefner 5ac0e09
Apply suggestion from @dionhaefner
dionhaefner bb94f4c
Apply suggestion from @dionhaefner
dionhaefner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| # Installing local Python modules into a Tesseract | ||
|
|
||
| ## Context | ||
| Sometimes it might be necessary to bundle local Python modules into a Tesseract. | ||
|
|
||
| There are 2 ways to do this: | ||
|
|
||
| 1. Make them a proper Python package with `pyproject.toml` and add the local path to the `tesseract_requirements.txt` file. | ||
| Both absolute and relative paths work, but in case they are relative paths, they should be | ||
| relative to the Tesseract's root folder (i.e., the one which contains the `tesseract_api.py` file). | ||
| 2. Just put them as `.py` files next to `tesseract_api.py` and add them to `build_config.package_data` (see also [packagedata.md]) in `tesseract_config.yaml` to make sure they're being included in container builds. | ||
|
|
||
| ## Example Tesseract | ||
|
|
||
| Here is an example Tesseract that highlights both ways to include dependencies. | ||
|
|
||
| ```{literalinclude} ../../../../examples/localpackage/tesseract_api.py | ||
| :language: python | ||
| ``` | ||
|
|
||
| The custom package `helloworld` is shipped with a file `helloworld.py` and a `pyproject.toml` that ensures it can be built as a Python package. | ||
|
|
||
| ```{literalinclude} ../../../../examples/localpackage/helloworld/helloworld.py | ||
| :language: python | ||
| ``` | ||
|
|
||
| ```{literalinclude} ../../../../examples/localpackage/helloworld/pyproject.toml | ||
| :language: toml | ||
| ``` | ||
|
|
||
| Then, it can be added as a local dependency to `tesseract_requirements.txt` by passing a relative path: | ||
|
|
||
| ```{literalinclude} ../../../../examples/localpackage/tesseract_requirements.txt | ||
| :language: text | ||
| ``` | ||
|
|
||
| The local module `goodbyeworld.py` is just a single Python file: | ||
|
|
||
| ```{literalinclude} ../../../../examples/localpackage/goodbyeworld.py | ||
| :language: python | ||
| ``` | ||
|
|
||
| To ensure it gets added to built Tesseracts, we have to register it as `package_data`: | ||
|
|
||
| ```{literalinclude} ../../../../examples/localpackage/tesseract_config.yaml | ||
| :language: yaml | ||
| ``` | ||
|
|
||
| Now we are ready to build the Tesseract. This Tesseract will accept a name like "Tessie" as an input and return a message: | ||
|
|
||
| ```bash | ||
| $ tesseract build examples/localpackage | ||
| $ tesseract run localpackage apply '{"inputs": {"name": "Tessie"}}' | ||
| {"message":"Hello Tessie!\nGoodbye Tessie!"} | ||
| ``` | ||
|
|
||
| This confirms that both custom dependencies are available to the Tesseract container. | ||
|
|
||
| To run the Tesseract without containerization, we have to make sure that `helloworld` is installed into our dev environment: | ||
|
|
||
| ```bash | ||
| $ pip install examples/localpackage/helloworld | ||
| $ TESSERACT_API_PATH=examples/localpackage/tesseract_api.py tesseract-runtime apply '{"inputs": {"name": "Tessie"}}' | ||
| {"message":"Hello Tessie!\nGoodbye Tessie!"} | ||
| ``` | ||
|
|
||
| ## Advanced pattern: injecting private dependencies as local wheels | ||
|
|
||
| In case a Tesseract depends on private packages that are not accessible from the public Python package index (PyPI), injecting them as local files can be a useful way to side-step authentication issues. | ||
|
|
||
| This is especially powerful in conjunction with `pip download` (e.g. from a private PyPI registry) to obtain a pre-built wheel: | ||
|
|
||
| ```bash | ||
| $ pip download cowsay==6.1 | ||
| Collecting cowsay==6.1 | ||
| Obtaining dependency information for cowsay==6.1 from https://files.pythonhosted.org/packages/f1/13/63c0a02c44024ee16f664e0b36eefeb22d54e93531630bd99e237986f534/cowsay-6.1-py3-none-any.whl.metadata | ||
| Downloading cowsay-6.1-py3-none-any.whl.metadata (5.6 kB) | ||
| Downloading cowsay-6.1-py3-none-any.whl (25 kB) | ||
| Saved ./cowsay-6.1-py3-none-any.whl | ||
| Successfully downloaded cowsay | ||
| ``` | ||
|
|
||
| We can then specify it as a local dependency in `tesseract_requirements.txt` by adding the following line: | ||
| ``` | ||
| ./cowsay-6.1-py3-none-any.whl | ||
| ``` | ||
|
|
||
| Finally, let's build the Tesseract, and verify it works | ||
| ```bash | ||
| $ tesseract build mytess | ||
| [i] Building image ... | ||
| [i] Built image sha256:7d024, ['mytess:latest'] | ||
|
|
||
| $ tesseract run mytess apply '{"inputs": {"message": "Hello, World!"}}' | ||
| {"out":" _____________\n| Hello, World! |\n =============\n \\\n \\\n ^__^\n (oo)\\_______\n (__)\\ )\\/\\\n ||----w |\n || ||"} | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # Copyright 2025 Pasteur Labs. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
|
|
||
| def ungreet(name): | ||
| return f"Goodbye {name}!" |
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| name: "localpackage" | ||
| version: "0.0.1" | ||
| description: "" | ||
|
|
||
| build_config: | ||
| package_data: | ||
| - ["goodbyeworld.py", "goodbyeworld.py"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| ./package | ||
| ./helloworld |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.