Replies: 2 comments
-
|
@zopyx did you export your variables? I have a similar issue that I think is related to this. The python code is: # file: test_typer.py
import typer
from typing_extensions import Annotated
arg_database: str = typer.Option(
...,
help="URL for Postgres sync database (e.g. postgresqlext://user:pw@localhost/mydb)",
envvar="SYNC_DATABASE",
)
def main(database: Annotated[str, arg_database]):
print(f"DATABASE: {database}")
if __name__ == "__main__":
typer.run(main)I prepared the environment in with the following commands: 🐍 v3.12.1 (.venv)
❯ echo "SYNC_DATABASE='my_nice_db'\n" > myenv
🐍 v3.12.1 (.venv)
❯ cat myenv
SYNC_DATABASE='my_nice_db'
🐍 v3.12.1 (.venv)
❯ source myenv
syngw on bus.nats [!+?⇡] is 📦 v0.0.1 via 🐍 v3.12.1 (.venv)
❯ echo $SYNC_DATABASE
my_nice_dbNow I tested the code with: 🐍 v3.12.1 (.venv)
❯ python test_typer.py --database="my_nice_db"
DATABASE: my_nice_db
🐍 v3.12.1 (.venv)
❯ echo $SYNC_DATABASE
my_nice_db
🐍 v3.12.1 (.venv)
❯ python test_typer.py
Usage: test_typer.py [OPTIONS]
Try 'test_typer.py --help' for help.
╭─ Error ────────────────────────────────────────────────╮
│ Missing option '--database'. │
╰────────────────────────────────────────────────────────╯
🐍 v3.12.1 (.venv)
❯ SYNC_DATABASE=$SYNC_DATABASE python test_typer.py
DATABASE: my_nice_dbSo from the above test the command seems to take the environment variable only when passed explicitly from the command line, probably this is due that the variables are available only at process level and are not pass through the processes. Exporting the variable fixed my issue! 🐍 v3.12.1 (.venv)
❯ export SYNC_DATABASE="my_nice_db"
🐍 v3.12.1 (.venv)
❯ python test_typer.py
DATABASE: my_nice_dbI believe that this should fix also your issue. |
Beta Was this translation helpful? Give feedback.
-
|
LOL...shall I remember what I did two years ago? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
I havr a
main()method with a parameterdatabaseas documented above. It will pick up its configuration from the given environment variableSYNC_DATABASE. This works perfectly from the commandline.Now, I want to use the same application code with
main()as entry point from within an AWS Lambda function.The related environment variable is correctly set through the AWS Lambda environment (checked by using logger messages).
So
SYNC_DATABASEis correctly available for the Python process.The Lambda handler invokes
typer.run(main).However, the
databaseparameter is always set toOptionInforather than the value of the configured environment variable.Expected behavior:
databaseshould have the value of the env varSYNC_DATABASE.Operating System
Linux
Operating System Details
AWS Lambda
Typer Version
0.4.2
Python Version
3.9
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions