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
Make pyimport able to return module attributes #4395
Conversation
Before this PR, `pyimport` can be used like: `pyimport("package")` or `pyimport("package.module")` but `pyimport("package.attribute")` fails. This updates `pyimport` to also work to get package attributes.
@ryanking13 another possibility is to add an optional second pyodide.pyimport(path, fromlist); So that you could do: const { PyodideConsole, repr_shorten, BANNER } = pyodide.pyimport(
"pyodide.console",
["PyodideConsole", "repr_shorten", "BANNER"]
); |
I guess I'll add that here and we can discuss. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @hoodmane!
For me, pyimport("package.attribute")
seems slightly weird as it is not a supported behavior in Python. For instance,
import sys.platform
will raise exception: No module named 'sys.platform'; 'sys' is not a package
.
So I am +1 for adding fromlist
argument (or probably with some other name).
Well we are already deviating from It is a little bit weird having: pyimport("pkg.module") ==> from pkg import module
pyimport("pkg.module", fromlist) ==> from pkg.module import <fromlist> but I don't think it's completely crazy. Also, when we pass a |
I see. That makes sense. Then I think About the const { PyodideConsole, repr_shorten, BANNER } = pyodide.pyimport("pyodide.console")
const { platform, version, path } = pyodide.pyimport("sys") already works without any modification. So maybe we don't need the fromlist parameter so much? |
Oh this is a great observation! The only very minor concern is that you create and leak a temporary
So I will remove the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Before this PR,
pyimport
can be used like:pyimport("package")
orpyimport("package.module")
butpyimport("package.attribute")
fails. This updatespyimport
to also work to get package attributes.