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

Add an option to uv run to run a .py script with pythonw.exe instead of python.exe #6805

Closed
piepero opened this issue Aug 29, 2024 · 4 comments
Labels
cli Related to the command line interface enhancement New feature or improvement to existing functionality good first issue Good for newcomers

Comments

@piepero
Copy link

piepero commented Aug 29, 2024

I am using uv run for a simple Python script with inline dependencies.

This script is used in two ways/environments.

First, it is run from the console. In this case, it should be run using python.exe, thus showing stout/stderr on the console.

Second, the same script is run by the Windows 11 Task Scheduler. In this case, I would like to use the windowless pythonw.exe to run the script without opening another window.

Currently, the only way to get uv run to choose either one or the other interpreter, is by naming the script either myscript.py or myscript.pyw.

In other words, I have to maintain two otherwise identical copies of the same script.

There may be workarounds, but the ideas I came up with either did not work (uv run pythonw myscript.py does not parse the inline metadata) or were cumbersome and error prone (i.e. using Windows soft or hard links, or importing the .py script from a second .pyw script and keeping the inline dependencies in sync).

It would be very convenient, if there was an additional flag for uv run to instruct uv to use pythonw.exe on a file with the .py extension (e.g. uv run --pythonw myscript.py).

@zanieb zanieb self-assigned this Aug 29, 2024
@zanieb zanieb added cli Related to the command line interface enhancement New feature or improvement to existing functionality labels Aug 29, 2024
@zanieb
Copy link
Member

zanieb commented Aug 29, 2024

We could parse the metadata with pythonw myscript.py, I think? Alternatively, we can provide a --script-exec option that we use to execute scripts per #6542 (comment)

@zanieb
Copy link
Member

zanieb commented Oct 4, 2024

We added a --script flag in #7739 but we don't have a --gui-script flag — which would be Windows-only but reasonable to include, I think.

@zanieb zanieb added the good first issue Good for newcomers label Oct 4, 2024
@zanieb zanieb removed their assignment Oct 4, 2024
zanieb added a commit that referenced this issue Dec 10, 2024
#9152)

Addresses #6805

## Summary

This PR adds a `--gui-script` flag to `uv run` that allows running
Python scripts with `pythonw.exe` on Windows, regardless of file
extension. This solves the issue where users need to maintain duplicate
`.py` and `.pyw` files to run the same script with and without a console
window.

The implementation follows the pattern established by the existing
`--script` flag, but uses `pythonw.exe` instead of `python.exe` on
Windows. On non-Windows platforms, the flag is present but returns an
error indicating it's Windows-only functionality.

Changes:
- Added `--gui-script` flag (Windows-only)
- Added Windows test to verify GUI script behavior
- Added non-Windows test to verify proper error message
- Updated CLI documentation


## Test Plan

The changes are tested through:

1. New Windows-specific test that verifies:
- Script runs successfully with `pythonw.exe` when using `--gui-script`
- Console output is suppressed in GUI mode but visible in regular mode
   - Same script can be run both ways without modification

2. New non-Windows test that verifies:
- Appropriate error message when `--gui-script` is used on non-Windows
platforms

3. Documentation updates to clearly indicate Windows-only functionality

---------

Co-authored-by: Zanie Blue <contact@zanie.dev>
@Choudhry18
Copy link

@zanieb I just got interested in the project and was looking at good first issues to start, this issue is still open but after reading the pull request #9152 it seems the issue should be closed.

@zanieb
Copy link
Member

zanieb commented Dec 31, 2024

Yep this is implemented, sorry!

@zanieb zanieb closed this as completed Dec 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli Related to the command line interface enhancement New feature or improvement to existing functionality good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants