Skip to content

Check env for py_require()d packages#1850

Merged
t-kalinowski merged 11 commits intomainfrom
check-env
Nov 20, 2025
Merged

Check env for py_require()d packages#1850
t-kalinowski merged 11 commits intomainfrom
check-env

Conversation

@dfalbel
Copy link
Member

@dfalbel dfalbel commented Oct 22, 2025

Warns when py_require()d packages are not installed in the resolved virtual environment.

@t-kalinowski
Copy link
Member

Thanks!

Can you please add a test and NEWS entry?

@t-kalinowski
Copy link
Member

We should probably add an escape hatch to let users disable the warning by setting an envvar.

@dfalbel
Copy link
Member Author

dfalbel commented Oct 23, 2025

my initial idea was disabling with suppressWarnings(reticulate::py_config()), but an env var can be useful too.

@dfalbel dfalbel force-pushed the check-env branch 4 times, most recently from 3e0f0d7 to c1ea8b9 Compare October 23, 2025 19:49
@t-kalinowski
Copy link
Member

Thanks, Daniel!

I fixed the CI failures and tweaked the approach slightly.

Like all things involving Python environment management, this change has a high potential for unintended behavior. I’ll merge it right after the next CRAN release to let it run on the dev version of reticulate and surface any issues.

I also noted two TODOs:

  • Also check python_version requirements.
  • Also warn if py_require() is called after reticulate has initialized Python.


if (would_install) {
# subset the py_require()$packages vector to keep only those that
# match output from `uv pip install`.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the output from uv already just the subset of packages that are required and are missing in the env?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uv outputs frozen versions docutils==1.2.3. I think we should display the actual requirement in py_require(), which is likely unfrozen docutils, or with a looser constraint like docutils[extra]>=1.2.3

R/package.R Outdated
"--color never --no-progress",
"--no-build",
# "--verbose",
"--offline", "--no-config",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think --offline is a good idea here. This will only warn if the package is already on the cache. Otherwise it's a resolution error, and status != 0.

If we keep --offline, we will need to change below.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I gave it a quick try locally I didn't run into issues, but thats probably because i already had all the packages in my local cache. I'm fine switching it back. We should have a unit test for this to make sure we don't accidentally trigger a download of a package!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you test that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe an approach like this pseudo-code?

output <- r_session({
  py_require("tensorflow")
  use_virtualenv("some/tmp/venv")
  py_config()
})
expect_false(grepl("downloading tensorflow", output))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, although we will need a escape hatch to return the actual uv output. With the changes mentioned in #1850 (comment) we no longer forward this output to the user.

@dfalbel dfalbel requested a review from t-kalinowski November 19, 2025 17:48
@t-kalinowski t-kalinowski merged commit 7bdbe7f into main Nov 20, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants