albertlauncher / albert

A fast and flexible keyboard launcher
https://albertlauncher.github.io
Other
7.29k stars 307 forks source link

Simplify import in python plugins #1471

Open AdamPS opened 1 month ago

AdamPS commented 1 month ago

Following on from #1423.

Currently a python plugin that imports any dist packages is required to have a metadata line like below. Then albert will show the user a dialog asking to import the dependencies, and they will be copied. If the metadata is missing, then the dialog still appears, but the import then fails with a strange error message.

md_lib_dependencies = "ZZZ"

For many users this is unnecessarily complicated, and perhaps they don't need to use a Virtualenv at all. The copied import is presumably "stuck" at this version while the base version gets updated automatically (for example using the OS package manager). The import process can be confusing for the user, especially when it fails.

Suggestions

ManuelSchneid3r commented 4 weeks ago

and they will be copied

installed using pip.

fails with a strange error message.

which one?

don't need to use a Virtualenv at all

this sounds as if a venv would be something bad.

is presumably "stuck" at this version

well we could have a button to pip install upgrade, but you could also nuke the site dir. iirc when installing another dependency the packages are updated. if not being stuck is not that much of a problem if everything works well right?

Provide an option whether to use virtualenv (disabled by default?)

thats nothing i want the user to be able to choose. simply because it will double the issues i will get. i prefer to use whatever faclitates distribution of OOTB working plugins.

  • Improve the error reporting in the the import process.

i have to know the problem with the current one first

Before showing the import dialog, check that the missing package is actually listed in metadata_.runtime_dependencies.

runtime dependendies and lib dependencies are different. the former are executables, the latter python packages.

If not, instead show an error that informs the user that the metadata is missing.

Metadata is missing? I dont get it.

AdamPS commented 3 weeks ago

Many thanks for the reply, and sorry about my inaccurate terminology and wrong understandings. I am not a python programmer. I am writing in python only because I very much like albert and I wish to write some of my own plugins😃.

Perhaps I can explain by sharing my situation? I have a plug-in that includes the following code

import requests

In 0.26.6 my plug-in broke. Eventually I fixed my plug-in by adding this following line

md_lib_dependencies = "requests"

If you create a plugin that includes an import but not a lib dependency then you should be able to see exactly what I saw.

1) I saw a dialog asking me if I want to install dependencies into the virtual environment. At the time I didn't understand the question. Clicking "No" seems like it will always be a bad choice, because the plugin will fail. Perhaps it would be better to remove this dialog and always do "Yes". As you already pointed out, if you give the user a choice which they don't understand and one option is anyway wrong then you will just get more issues.

2) When I clicked Yes, albert called pip, but with an empty string of packages (because I don't have any md_lib_dependencies). This is the "strange error message" I referred to. I no longer have the error message to tell you exactly what it says, however hopefully I have now enabled you to understand what I'm talking about. I propose that albert should avoid calling pip when the packages list is empty.

3) My situation was a plugin bug with missing metadata. What I am referring to here is that I was missing md_lib_dependencies. It would be great if albert can give a clear warning here to help the plugin developer. This could be done by the code that catches the exception for a missing package. It can check if the package is in the list of md_lib_dependencies. If yes, then call pip as the code does now. If not, the print an error message explaining about the need for md_lib_dependencies.

4) Regarding outdated packages - well it seems likely it will happen eventually. A plugin developer might specifically need a certain minimum version of a package dependency, yet the user has an older version already in the virtual environment. Perhaps the solution would be to allow specifying a version in the md_lib_dependencies? But I don't really know enough about python so feel free to ignore this one.

Thanks again

martinengelke commented 3 weeks ago

@AdamPS

I am writing in python only because I very much like albert and I wish to write some of my own plugins😃

That's at least a honest answer which I appreciate. I was already playing around with python before and came here out of the same reason, and felt in love. Especially if you see developers working constantly on improvements. I like those mentality.