Skip to content

Commit 6b7d517

Browse files
committed
Add support for uv python dir --bin (#8569)
Following #8458 we want to support showing the target directory in the CLI
1 parent 31c3ddb commit 6b7d517

File tree

5 files changed

+80
-14
lines changed

5 files changed

+80
-14
lines changed

crates/uv-cli/src/lib.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -3838,7 +3838,9 @@ pub enum PythonCommand {
38383838
/// `%APPDATA%\uv\data\python` on Windows.
38393839
///
38403840
/// The Python installation directory may be overridden with `$UV_PYTHON_INSTALL_DIR`.
3841-
Dir,
3841+
///
3842+
/// To instead view the directory uv installs Python executables into, use the `--bin` flag.
3843+
Dir(PythonDirArgs),
38423844

38433845
/// Uninstall Python versions.
38443846
Uninstall(PythonUninstallArgs),
@@ -3866,6 +3868,25 @@ pub struct PythonListArgs {
38663868
pub only_installed: bool,
38673869
}
38683870

3871+
#[derive(Args)]
3872+
#[allow(clippy::struct_excessive_bools)]
3873+
pub struct PythonDirArgs {
3874+
/// Show the directory into which `uv python` will install Python executables.
3875+
///
3876+
/// By default, `uv python dir` shows the directory into which the Python distributions
3877+
/// themselves are installed, rather than the directory containing the linked executables.
3878+
///
3879+
/// The Python executable directory is determined according to the XDG standard and is derived
3880+
/// from the following environment variables, in order of preference:
3881+
///
3882+
/// - `$UV_PYTHON_BIN_DIR`
3883+
/// - `$XDG_BIN_HOME`
3884+
/// - `$XDG_DATA_HOME/../bin`
3885+
/// - `$HOME/.local/bin`
3886+
#[arg(long, verbatim_doc_comment)]
3887+
pub bin: bool,
3888+
}
3889+
38693890
#[derive(Args)]
38703891
#[allow(clippy::struct_excessive_bools)]
38713892
pub struct PythonInstallArgs {

crates/uv/src/commands/python/dir.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@ use anyhow::Context;
33
use owo_colors::OwoColorize;
44

55
use uv_fs::Simplified;
6-
use uv_python::managed::ManagedPythonInstallations;
6+
use uv_python::managed::{python_executable_dir, ManagedPythonInstallations};
7+
8+
/// Show the Python installation directory.
9+
pub(crate) fn dir(bin: bool) -> anyhow::Result<()> {
10+
if bin {
11+
let bin = python_executable_dir()?;
12+
println!("{}", bin.simplified_display().cyan());
13+
} else {
14+
let installed_toolchains = ManagedPythonInstallations::from_settings()
15+
.context("Failed to initialize toolchain settings")?;
16+
println!(
17+
"{}",
18+
installed_toolchains.root().simplified_display().cyan()
19+
);
20+
}
721

8-
/// Show the toolchain directory.
9-
pub(crate) fn dir() -> anyhow::Result<()> {
10-
let installed_toolchains = ManagedPythonInstallations::from_settings()
11-
.context("Failed to initialize toolchain settings")?;
12-
println!(
13-
"{}",
14-
installed_toolchains.root().simplified_display().cyan()
15-
);
1622
Ok(())
1723
}

crates/uv/src/lib.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1111,9 +1111,13 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
11111111
.await
11121112
}
11131113
Commands::Python(PythonNamespace {
1114-
command: PythonCommand::Dir,
1114+
command: PythonCommand::Dir(args),
11151115
}) => {
1116-
commands::python_dir()?;
1116+
// Resolve the settings from the command-line arguments and workspace configuration.
1117+
let args = settings::PythonDirSettings::resolve(args, filesystem);
1118+
show_settings!(args);
1119+
1120+
commands::python_dir(args.bin)?;
11171121
Ok(ExitStatus::Success)
11181122
}
11191123
Commands::Publish(args) => {

crates/uv/src/settings.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use url::Url;
88
use uv_cache::{CacheArgs, Refresh};
99
use uv_cli::{
1010
options::{flag, resolver_installer_options, resolver_options},
11-
AuthorFrom, BuildArgs, ExportArgs, PublishArgs, ToolUpgradeArgs,
11+
AuthorFrom, BuildArgs, ExportArgs, PublishArgs, PythonDirArgs, ToolUpgradeArgs,
1212
};
1313
use uv_cli::{
1414
AddArgs, ColorChoice, ExternalCommand, GlobalArgs, InitArgs, ListFormat, LockArgs, Maybe,
@@ -597,6 +597,23 @@ impl PythonListSettings {
597597
}
598598
}
599599

600+
/// The resolved settings to use for a `python dir` invocation.
601+
#[allow(clippy::struct_excessive_bools)]
602+
#[derive(Debug, Clone)]
603+
pub(crate) struct PythonDirSettings {
604+
pub(crate) bin: bool,
605+
}
606+
607+
impl PythonDirSettings {
608+
/// Resolve the [`PythonDirSettings`] from the CLI and filesystem configuration.
609+
#[allow(clippy::needless_pass_by_value)]
610+
pub(crate) fn resolve(args: PythonDirArgs, _filesystem: Option<FilesystemOptions>) -> Self {
611+
let PythonDirArgs { bin } = args;
612+
613+
Self { bin }
614+
}
615+
}
616+
600617
/// The resolved settings to use for a `python install` invocation.
601618
#[allow(clippy::struct_excessive_bools)]
602619
#[derive(Debug, Clone)]

docs/reference/cli.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -4693,6 +4693,8 @@ By default, Python installations are stored in the uv data directory at `$XDG_DA
46934693

46944694
The Python installation directory may be overridden with `$UV_PYTHON_INSTALL_DIR`.
46954695

4696+
To instead view the directory uv installs Python executables into, use the `--bin` flag.
4697+
46964698
<h3 class="cli-reference">Usage</h3>
46974699

46984700
```
@@ -4701,7 +4703,23 @@ uv python dir [OPTIONS]
47014703

47024704
<h3 class="cli-reference">Options</h3>
47034705

4704-
<dl class="cli-reference"><dt><code>--cache-dir</code> <i>cache-dir</i></dt><dd><p>Path to the cache directory.</p>
4706+
<dl class="cli-reference"><dt><code>--bin</code></dt><dd><p>Show the directory into which <code>uv python</code> will install Python executables.</p>
4707+
4708+
<p>By default, <code>uv python dir</code> shows the directory into which the Python distributions themselves are installed, rather than the directory containing the linked executables.</p>
4709+
4710+
<p>The Python executable directory is determined according to the XDG standard and is derived from the following environment variables, in order of preference:</p>
4711+
4712+
<ul>
4713+
<li><code>$UV_PYTHON_BIN_DIR</code></li>
4714+
4715+
<li><code>$XDG_BIN_HOME</code></li>
4716+
4717+
<li><code>$XDG_DATA_HOME/../bin</code></li>
4718+
4719+
<li><code>$HOME/.local/bin</code></li>
4720+
</ul>
4721+
4722+
</dd><dt><code>--cache-dir</code> <i>cache-dir</i></dt><dd><p>Path to the cache directory.</p>
47054723

47064724
<p>Defaults to <code>$XDG_CACHE_HOME/uv</code> or <code>$HOME/.cache/uv</code> on macOS and Linux, and <code>%LOCALAPPDATA%\uv\cache</code> on Windows.</p>
47074725

0 commit comments

Comments
 (0)