pyodide / micropip

A lightweight Python package installer for Pyodide
https://micropip.pyodide.org
Mozilla Public License 2.0
68 stars 16 forks source link

ENH Add support for reinstalling packages #64

Open ryanking13 opened 1 year ago

ryanking13 commented 1 year ago

Resolve #51

This PR changes micropip.install behavior to remove previously installed packages and reinstall the new version. In addition, it adds the force_reinstall parameter to micropip.install, allowing it to force a reinstall even if it already meets the requirements.

Edit:

This PR adds reinstall parameter to micropip.install which allows reinstalling packages that are already installed.

ryanking13 commented 1 year ago

@rth I just noticed that you added sprint label in #51. Maybe there is a sprint running in PyconUS? Sorry if I hijacked someones work.

rth commented 1 year ago

Yes, there is a sprint in PyConUS so I tagged a few issues people have more to choose from. No worries if you took it :)

hoodmane commented 1 year ago

Thanks @ryanking13!

remove previously installed packages and reinstall the new version

I don't think this makes sense as a default. In my opinion it should not reinstall anything unless it is specifically requested.

ryanking13 commented 1 year ago

I don't think this makes sense as a default. In my opinion it should not reinstall anything unless it is specifically requested.

Maybe my description was not clear. It uninstalls only if the requested version is incompatible with the installed version. Previously in such cases, micropip failed with:

ValueError: Requested 'packaging==22.0', but packaging==23.0 is already installed
rth commented 1 year ago

If True, reinstall all packages even if they are already up-to-date.

Yeah, I'm also not sure about it. As a user, in the example above I would expect the install command to only change the packages that need changing and that I requested. But I guess it's more complex in terms of dependency resolution. This feels like re-installing a venv, which is good but strange to do in Python when not managing venv.

If we take the case where we install some packages. Some are already installed with different versions. If the version is compatible we do nothing, if it isn't we uninstall them, and install the requested version (and leave all other packages unchanged). That's more or less what pip does (or used to do) by default. Can't we do that?

ryanking13 commented 1 year ago

I still think failing is better by default than reinstall. I think people really mostly don't want to reinstall packages that they've already imported but they might not realize they don't want to. Maybe having an allow reinstall flag would be good.

Hmm, yes. Unlike other package managers micropip is a runtime package manager so the situation is more complex that some packages might be already imported... and we don't show any logs (unless verbose option is enabled in #60), so people will not even notice that some packages are reinstalled.

If we take the case where we install some packages. Some are already installed with different versions. If the version is compatible we do nothing, if it isn't we uninstall them, and install the requested version (and leave all other packages unchanged). That's more or less what pip does (or used to do) by default. Can't we do that?

Yes, that is the current expected bahavior but I guess Hood does not prefer uninstalling packages without an explicit flag?

ryanking13 commented 1 year ago

Then how about:

  1. (default) micropip.install(reinstall=False) will show error messages if there is an installed version that is incompatible.

  2. micropip.install(reinstall=True) will reinstall packages that are incompatible / keep packages that are compatible (same as pip)

    • maybe show logging messages by default so that people can aware which packages are reinstalled?
hoodmane commented 1 year ago

Hood does not prefer uninstalling packages without an explicit flag?

Well if it's for matching pip then I could sort of see it. But the issue is just what happens if the package is already imported...

ryanking13 commented 1 year ago

Well if it's for matching pip then I could sort of see it. But the issue is just what happens if the package is already imported...

Well jupyter shares the same problem and users would need to call importlib.reload if they have already imported a package. One difference is that pip is verbose by default while micropip is silent so users might not notice that they have reinstalled a package.