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

uv run supports python package #7281

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,8 @@ pub(crate) enum RunCommand {
PythonScript(PathBuf, Vec<OsString>),
/// Execute a `pythonw` script (Windows only).
PythonGuiScript(PathBuf, Vec<OsString>),
/// Execute a Python package containing a `__main__.py` file.
PythonPackage(PathBuf, Vec<OsString>),
/// Execute a `python` script provided via `stdin`.
PythonStdin(Vec<u8>),
/// Execute an external command.
Expand All @@ -790,7 +792,9 @@ impl RunCommand {
fn display_executable(&self) -> Cow<'_, str> {
match self {
Self::Python(_) => Cow::Borrowed("python"),
Self::PythonScript(_, _) | Self::Empty => Cow::Borrowed("python"),
Self::PythonScript(_, _) | Self::PythonPackage(_, _) | Self::Empty => {
Cow::Borrowed("python")
}
Self::PythonGuiScript(_, _) => Cow::Borrowed("pythonw"),
Self::PythonStdin(_) => Cow::Borrowed("python -c"),
Self::External(executable, _) => executable.to_string_lossy(),
Expand All @@ -805,7 +809,7 @@ impl RunCommand {
process.args(args);
process
}
Self::PythonScript(target, args) => {
Self::PythonScript(target, args) | Self::PythonPackage(target, args) => {
let mut process = Command::new(interpreter.sys_executable());
process.arg(target);
process.args(args);
Expand Down Expand Up @@ -868,7 +872,7 @@ impl std::fmt::Display for RunCommand {
}
Ok(())
}
Self::PythonScript(target, args) => {
Self::PythonScript(target, args) | Self::PythonPackage(target, args) => {
write!(f, "python {}", target.display())?;
for arg in args {
write!(f, " {}", arg.to_string_lossy())?;
Expand Down Expand Up @@ -931,6 +935,8 @@ impl TryFrom<&ExternalCommand> for RunCommand {
&& target_path.exists()
{
Ok(Self::PythonGuiScript(target_path, args.to_vec()))
} else if target_path.is_dir() && target_path.join("__main__.py").exists() {
Ok(Self::PythonPackage(target_path, args.to_vec()))
} else {
Ok(Self::External(
target.clone(),
Expand Down
22 changes: 22 additions & 0 deletions crates/uv/tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,28 @@ fn run_stdin() -> Result<()> {
Ok(())
}

#[test]
fn run_package() -> Result<()> {
let context = TestContext::new("3.12");

let main_script = context.temp_dir.child("__main__.py");
main_script.write_str(indoc! { r#"
print("Hello, world!")
"#
})?;

uv_snapshot!(context.filters(), context.run().arg("."), @r###"
success: true
exit_code: 0
----- stdout -----
Hello, world!
----- stderr -----
"###);

Ok(())
}

/// When the `pyproject.toml` file is invalid.
#[test]
fn run_project_toml_error() -> Result<()> {
Expand Down
Loading