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

Script inline python requirement not respected when .python-version file is in project #9393

Open
Magglish opened this issue Nov 24, 2024 · 3 comments
Labels
question Asking for clarification or support

Comments

@Magglish
Copy link

Hello,

I'm really exicted that we can specify python version and libraries dependency for scripts in uv and this thing can convinced me to move from poetry to uv.

Description

Unfortunately, I've encounter an issue which I don't if it's a bug or expected behaviour.
The problem is that script inline python requirement is not respected by uv when .python-version file is specified in the same directory.

Steps to reproduce:

  1. OS Version: Ubuntu 22.04 LTS

  2. uv version: 0.5.4 (installed through official installation script as mentioned in documentation)

  3. .python-version contains:

3.12.7
  1. example.py script with dependencies:
# /// script
# requires-python = "==3.11.5"
# dependencies = [
#   "rich==13.9.4",
# ]
# ///

import sys
import time
from rich.progress import track

for i in track(range(20), description="For example:"):
    time.sleep(0.05)
print(sys.version)
  1. Create venv with uv
uv venv
  1. Run example.py
uv run example.py

What we see:

  1. warning: The Python request from .python-version resolved to Python 3.12.7, which is incompatible with the script's Python requirement: ==3.11.5
  2. print(sys.version) returns 3.12.7 but not 3.11.5

What should we see:

The script should be run with python 3.11.5, but it's not.

Alternative

We can run example.py with

uv run --python 3.11.5 example.py

But this means that the inline metadata requirements are not being used at all.

Is this a bug or a feature?

@FishAlchemist
Copy link
Contributor

FishAlchemist commented Nov 24, 2024

It has a warning, so I personally think it's an expected behavior.

warning: The Python request from `.python-version` resolved to Python 3.12.7, which is incompatible with the script's Python requirement: `==3.11.5`

Personally, I believe that if the script has a specified version, we ought to adhere to it unless a different version is explicitly defined via the CLI.
However, since uv run is categorized as a project API, it is also reasonable for it to respect the .python-version file under the project.

@Magglish
Copy link
Author

In my opinion, if the uv wants to respect the inline metadata requirements presented in the scripts then all requirements should be respected.

@charliermarsh charliermarsh added the question Asking for clarification or support label Nov 24, 2024
@charliermarsh
Copy link
Member

Yeah I believe this is intentional. Another option would be to throw a hard error because you have conflicting requirements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Asking for clarification or support
Projects
None yet
Development

No branches or pull requests

3 participants