You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have wrote a program called pdfsimuti that combines the feature of several PDF utility tools into a single CLI application. This includes watermarking, merging, compressing, splitting etc. All these modules import some heavy packages which reduces responds time on my machine. I wanted to make my program fast by importing only the modules that is called.
If I wanted to use a module like merge, I would go something like python main.py merge --help. But I ran into a problem with the other imports.
To add the module on typer, I have to do something like this,
from pdfsimuti import merge
from pdfsimuti import compress
from pdfsimuti import checkhealth
app = typer.Typer()
app.command(help="....")(merge.merge)
app.command(help="....")(compress.compress)
app.command(help="....")(checkhealth.checkhealth)
so that it shows the module's short help when python main.py --help is called.
this is where the issue starts. Each of this modules has a lot of global variables tied to imports. Despite doing encapsulations, it still isn't enough. If I do something like python main.py merge file.pdf then it also calls compress and watermark as well.
When they are called, their entire global variables are triggered. I do not need them to be called unless they are needed.
To use lazy imports on them, I have tried this~
@app.command(help="....")
def merge(params1, params2):
from pdfsimuti import merge
merge(params1, params2)
but I have ran into another issue. The modules uses enum choices which is present only on the modules file itself. Not only that, the merge parameters also have their own typer properties as well which isn't shown when python main.py merge--help gets called.
Before,
> pdfsimuti checkhealth --help
Usage: pdfsimuti checkhealth [OPTIONS]
Shows you the status of the packages required for the program.
╭─ Options ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --verbose -v,-V INTEGER RANGE [x<=3] Verbose level [default: 1] │
│ --help -h Show this message and exit. │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
(
After my attempt on lazy imports,
> pdfsimuti checkhealth --help
Usage: pdfsimuti checkhealth [OPTIONS] VERBOSE
Shows you the status of the packages required for the program.
╭─ Arguments ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ * verbose TEXT [default: None] [required] │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Options ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --help -h Show this message and exit. │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
I have wrote a program called
pdfsimuti
that combines the feature of several PDF utility tools into a single CLI application. This includes watermarking, merging, compressing, splitting etc. All these modules import some heavy packages which reduces responds time on my machine. I wanted to make my program fast by importing only the modules that is called.If I wanted to use a module like merge, I would go something like
python main.py merge --help
. But I ran into a problem with the other imports.To add the module on typer, I have to do something like this,
so that it shows the module's short help when
python main.py --help
is called.this is where the issue starts. Each of this modules has a lot of global variables tied to imports. Despite doing encapsulations, it still isn't enough. If I do something like
python main.py merge file.pdf
then it also calls compress and watermark as well.When they are called, their entire global variables are triggered. I do not need them to be called unless they are needed.
To use lazy imports on them, I have tried this~
but I have ran into another issue. The modules uses enum choices which is present only on the modules file itself. Not only that, the merge parameters also have their own typer properties as well which isn't shown when
python main.py merge--help
gets called.Before,
After my attempt on lazy imports,
Even if I attempt something like
on the
main.py
, it won't solve the case if enum choices are used as they are not available onmain.py
Conclusion
I want to have my modules imported in such way that~
--help
properties are displayed.--help
properties are displayed in such way even if they have enum choices, they will be displayedOperating System
Windows
Operating System Details
Windows 11 Version 24H2
Typer Version
0.12.5
Python Version
python 3.13.2
Additional Context
Beta Was this translation helpful? Give feedback.
All reactions