Skip to content

Commit 618ea47

Browse files
committed
Add support for uv python dir --bin
1 parent 6bf47d0 commit 618ea47

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
@@ -3730,7 +3730,9 @@ pub enum PythonCommand {
37303730
/// `%APPDATA%\uv\data\python` on Windows.
37313731
///
37323732
/// The Python installation directory may be overridden with `$UV_PYTHON_INSTALL_DIR`.
3733-
Dir,
3733+
///
3734+
/// To instead view the directory uv installs Python executables into, use the `--bin` flag.
3735+
Dir(PythonDirArgs),
37343736

37353737
/// Uninstall Python versions.
37363738
Uninstall(PythonUninstallArgs),
@@ -3758,6 +3760,25 @@ pub struct PythonListArgs {
37583760
pub only_installed: bool,
37593761
}
37603762

3763+
#[derive(Args)]
3764+
#[allow(clippy::struct_excessive_bools)]
3765+
pub struct PythonDirArgs {
3766+
/// Show the directory into which `uv python` will install Python executables.
3767+
///
3768+
/// By default, `uv python dir` shows the directory into which the Python distributions
3769+
/// themselves are installed, rather than the directory containing the linked executables.
3770+
///
3771+
/// The Python executable directory is determined according to the XDG standard and is derived
3772+
/// from the following environment variables, in order of preference:
3773+
///
3774+
/// - `$UV_PYTHON_BIN_DIR`
3775+
/// - `$XDG_BIN_HOME`
3776+
/// - `$XDG_DATA_HOME/../bin`
3777+
/// - `$HOME/.local/bin`
3778+
#[arg(long, verbatim_doc_comment)]
3779+
pub bin: bool,
3780+
}
3781+
37613782
#[derive(Args)]
37623783
#[allow(clippy::struct_excessive_bools)]
37633784
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
@@ -1112,9 +1112,13 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
11121112
.await
11131113
}
11141114
Commands::Python(PythonNamespace {
1115-
command: PythonCommand::Dir,
1115+
command: PythonCommand::Dir(args),
11161116
}) => {
1117-
commands::python_dir()?;
1117+
// Resolve the settings from the command-line arguments and workspace configuration.
1118+
let args = settings::PythonDirSettings::resolve(args, filesystem);
1119+
show_settings!(args);
1120+
1121+
commands::python_dir(args.bin)?;
11181122
Ok(ExitStatus::Success)
11191123
}
11201124
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,
@@ -601,6 +601,23 @@ impl PythonListSettings {
601601
}
602602
}
603603

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

docs/reference/cli.md

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

45614561
The Python installation directory may be overridden with `$UV_PYTHON_INSTALL_DIR`.
45624562

4563+
To instead view the directory uv installs Python executables into, use the `--bin` flag.
4564+
45634565
<h3 class="cli-reference">Usage</h3>
45644566

45654567
```
@@ -4568,7 +4570,23 @@ uv python dir [OPTIONS]
45684570

45694571
<h3 class="cli-reference">Options</h3>
45704572

4571-
<dl class="cli-reference"><dt><code>--cache-dir</code> <i>cache-dir</i></dt><dd><p>Path to the cache directory.</p>
4573+
<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>
4574+
4575+
<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>
4576+
4577+
<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>
4578+
4579+
<ul>
4580+
<li><code>$UV_PYTHON_BIN_DIR</code></li>
4581+
4582+
<li><code>$XDG_BIN_HOME</code></li>
4583+
4584+
<li><code>$XDG_DATA_HOME/../bin</code></li>
4585+
4586+
<li><code>$HOME/.local/bin</code></li>
4587+
</ul>
4588+
4589+
</dd><dt><code>--cache-dir</code> <i>cache-dir</i></dt><dd><p>Path to the cache directory.</p>
45724590

45734591
<p>Defaults to <code>$HOME/Library/Caches/uv</code> on macOS, <code>$XDG_CACHE_HOME/uv</code> or <code>$HOME/.cache/uv</code> on Linux, and <code>%LOCALAPPDATA%\uv\cache</code> on Windows.</p>
45744592

0 commit comments

Comments
 (0)