astral-sh / uv

An extremely fast Python package and project manager, written in Rust.
https://docs.astral.sh/uv
Apache License 2.0
20.44k stars 605 forks source link

Enable CLI package installed with `uv add` to be accessed from the virtual env #7587

Open PatrickAlphaC opened 2 hours ago

PatrickAlphaC commented 2 hours ago

Let's say I add the following package that is a CLI:

First, let's setup and activate a virtual environment.

uv init
uv venv
source .venv/bin/activate

Then, add our package that has an executable.

uv add moccasin

Assuming moccasin has an executable named mox, if I do the above, I am able to run the tool with:

uv run mox --help

However, I cannot do:

mox --help

By itself. If I instead run:

uv pip install moccasin

I am then able to run it's executable in the virtual environment without uv run:

mox --help

It would be nice if uv add had a flag to expose the executable without calling uv run. Maybe like:

uv add moccasin --expose-script

Or something.

charliermarsh commented 2 hours ago

Did you activate the virtual environment in .venv prior to running mox --help?

PatrickAlphaC commented 2 hours ago

@charliermarsh yes. I can send a video if that's helpful?

charliermarsh commented 2 hours ago

Sure, anything to help reproduce would be appreciated.

PatrickAlphaC commented 1 hour ago

https://github.com/user-attachments/assets/21080fc0-e502-4735-bd37-c033957530bd

PatrickAlphaC commented 1 hour ago

I also updated the original issue so that the steps are more specific (included the virtual environment setup to make reproducing it more straightforward)

charliermarsh commented 9 minutes ago

Thanks for this, it helps a lot.

I think the issue here is that uv add moccasin does not install into the activated virtual environment (hence the warning you're seeing). It installs into the .venv in the current directory, by design. So you need to activate the environment in the current directory in order to add the executable to the PATH. It's configurable here (https://docs.astral.sh/uv/concepts/projects/#project-environments) though that's intended for deployment environments like Docker containers.