Skip to content

Commit

Permalink
uv run support a python package
Browse files Browse the repository at this point in the history
  • Loading branch information
j178 committed Sep 11, 2024
1 parent 77d278f commit bb3986a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
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.
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

0 comments on commit bb3986a

Please sign in to comment.