Skip to content
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

dinamically set env variable #2993

Closed
ZeeD opened this issue Jul 3, 2024 · 4 comments · Fixed by #3087
Closed

dinamically set env variable #2993

ZeeD opened this issue Jul 3, 2024 · 4 comments · Fixed by #3087
Labels
⭐ enhancement Improvements for existing features

Comments

@ZeeD
Copy link

ZeeD commented Jul 3, 2024

Is your feature/enhancement proposal related to a problem? Please describe.

I am starting an airflow project.
I successfully used pdm to install airflow, my dependencies, etc...

my goal is to set AIRFLOW_HOME to a folder of my project (basically I want a layout like the following one)

.../myproject
├── airflow
│   └── dags
...
├── pdm.lock
└── pyproject.toml

and also, I am trying to leverage pdm scripts support.

basically I want to set

[tool.pdm.scripts]
_.env = { AIRFLOW_HOME = ".../myproject/airflow" }
start = { cmd = "airflow standalone" }

but airflow expect an absolute path for AIRFLOW_HOME and I don't want to hardcode that in the pyproject.toml file

Describe the solution you'd like

There are many tricks that could help me, but at the end I want to be able to set an environmental variable dinamically, and not with a literal.

  • maybe pdm could expose ${PROJECT_ROOT} as an env variable AND let me set AIRFLOW_HOME = "${PROJECT_ROOT}/airflow"
  • or pdm let me invoke a command, so I could write something like AIRFLOW_HOME = "$({pdm} info --where)/airflow"

but at the moment the values are treated as literals

@ZeeD ZeeD added the ⭐ enhancement Improvements for existing features label Jul 3, 2024
@o-moe
Copy link
Contributor

o-moe commented Jul 26, 2024

Have you seen the pdm-dotenv plugin from the awesome-pdm list? Maybe you can utilize it to define your AIRFLOW_HOME and use it with pdm.

@ZeeD
Copy link
Author

ZeeD commented Jul 27, 2024

hi @o-moe , thanks for your suggestion.
I was checking the plugin, but from a cursory look I might have some doubts about its benefits.

Moreso that my understanding is that pdm's [tool.pdm.scripts] section already support the load of a shared custom env file, via _.env_file = ".env"

In any case your suggestion bring me to do some additional tests, and it seems that I can recycle env variables in the .env file. Witch is reasonable, as from my understanding pdm is using python-dotenv under the hood.

But it seems to me that PROJECT_ROOT is not available during the .env analysis, so I'm stuck again, because I cannot retrieve the project full path.
And from what I know python-dotenv doesn't support any kind of process execution in the .env file boy, but maybe I'm wrong.

Do you think there are some paths I've missed?

@ZeeD
Copy link
Author

ZeeD commented Jul 31, 2024

Hi, it's me again.

I've tried to find a workaround, and I've summarized my work on https://github.com/ZeeD/pdm-dotenv-test and indeed it "works": it's possible to just clone the repo, run pdm install once and then pdm run test will show an absolute path using the project folder.

The idea was to leverage pdm-dotenv plugin to pre-load a custom .env.projectroot file. This file is auto-generated after the install of the project locally.

Yet, I have some questions:

  • first of all, is the approach sound? I'm not sure I'm doing something "wrong" with the plugins and the event handlers.
  • on a technical level, is it correct to leverage the post_install hook to install the pdm-dotenv plugin? it seems to me that pdm install doesn't install the plugin and I don't know why
  • for the dotenv_projectroot.py - that is basically just an helper for the initial installation - is there a better folder I should use? I don't want to mix it with the other sources (in src)

In any case, I still think that it would make sense for pdm itself to set the PROJECT_ROOT env variable

@ZeeD
Copy link
Author

ZeeD commented Aug 8, 2024

oh, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⭐ enhancement Improvements for existing features
Projects
None yet
2 participants