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

Allow to define local variable in rye scripts #652

Open
souliane opened this issue Feb 14, 2024 · 1 comment
Open

Allow to define local variable in rye scripts #652

souliane opened this issue Feb 14, 2024 · 1 comment

Comments

@souliane
Copy link

souliane commented Feb 14, 2024

Hi, I would like to be able to define local variables for my rye scripts (to avoid copy-pasting over, e.g. if I need to repeat the same filepaths). Also, such variable defined in a chain scope should ideally be usable from sub-commands.

I tried to workaround using the env vars in the scripts' commands, but this doesn't work:

[tool.rye.scripts]
test_env = { cmd = "echo $TEST", env = { TEST = "OK" } }
test_list = { chain = ["test_list:prepare"], env = { TEST = "OK" } }
"test_list:prepare" = { cmd = "echo $TEST" }

Both output "$TEST" and not "OK". Indeed, the output of rye run -l shows that rye handles this by localizing the variable for the command to be called:

test_env (TEST=OK echo '$TEST')
test_list (chain: [test_list:prepare])
test_list:prepare (echo '$TEST')

This is OK, but the env vars can't be used directly [a].

More concerning, I see that the cmd words after the first one are encapsulated in single quotes, thus even a defined variable would not be evaluated.

I am suggesting to either:

  1. Change the way env var are handled, so that they can be used immediately, in addition to being passed to the sub-process [b].
  2. Add a local variable feature e.g. to make test_env = { cmd = "echo $TEST", var = { TEST = "OK" } } print "OK" and not "$TEST",

As far as I am concerned, both options (local variables, or re-using the env ones) would be OK (I see pros and cons in both).

[a] For the chain usecase, the outputs doesn't tell if the env vars will be passed to sub-processes... I assume it does, but this is not relevant for my current need.
[b] This is how Poe the Poet does it.

@snth
Copy link

snth commented Feb 22, 2024

I just learned about about Rye today from the uv announcement and I love it! It's by far the most productive Python environment and package management tool that I've used.

I currently want to set up a script for vim so that I can more easily edit the source files with rye run vim <filename.py> and it will automatically prepend src/my-project/ to the path. I couldn't figure out how to do that from the docs and then found this issue. From the comment it looks like it's currently not possible?

+1 for adding the ability to pass positional and named/environment paramters to the script commands.

In the meantime it looks like Poe the Poet should be able to do it so I'll try that. Thanks for the pointer @souliane ! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants