rubenf
rubenf7mo ago

relative imports in python

@Ask Inkeep relative imports in python
2 Replies
Ask Inkeep
Ask Inkeep7mo ago
Thanks for tagging me @rubenf. I'll look through relevant sources and get back to you shortly. ----------------------------- While I generate an answer, here are some sources that might be helpful: * Sharing Common Logic * Dependency Management & Imports * Python Quickstart * bug: Scheduled script with relative import fails when import added to upstream -----------------------------
Ask Inkeep
Ask Inkeep7mo ago
Answer In Windmill, when working with Python scripts, you can share common logic between your scripts using relative imports. This feature allows you to easily reuse code across different scripts in your project. Here's how you can do it: - To import directly from another Python script, you can follow the path layout. For instance, to import a function foo from a script called common_logic located in the same user space, you would use the following syntax:
# if common_logic is a script in the same folder or user-space
from .common_logic import foo
# otherwise, if you need to access the folder 'folder'
from ..folder.common_logic import foo
# if common_logic is a script in the same folder or user-space
from .common_logic import foo
# otherwise, if you need to access the folder 'folder'
from ..folder.common_logic import foo
- It works with scripts contained in folders and for scripts contained in user spaces, such as f.<foldername>.script_path or u.<username>.script_path. - Keep in mind that you can only import scripts that you have view rights on at the time of execution. This ensures that your code respects the necessary access controls (1) . Remember, this flexibility in sharing common logic enhances your ability to maintain and organize your code efficiently, making your development process with Windmill more streamlined and effective.