8000 Automatic import plugin by FabioRosado · Pull Request #1099 · pyscript/pyscript · GitHub
[go: up one dir, main page]

Skip to content

Automatic import plugin #1099

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

Closed
wants to merge 2 commits into from

Conversation

FabioRosado
Copy link
Contributor

I'm opening this PR as a draft to show the code of the automatic imports that I presented yesterday.

I think there's a lot of room for improvement so feel free to add any comments 😄

Here's a few things I would like to do:

  • Add a parser for the most common packages and update package name with the import statement
  • Only do automatic import if the user didn't specify any packages in config
  • Maybe toggle it off until all the rough edges are fixed (or a bit less rough)

@JeffersGlass
Copy link
Contributor
JeffersGlass commented Jan 6, 2023

It might be worth looking at pyodide.loadPackagesFromImports - they've got a fair bit of infrastructure built up around similar functionality, including mapping known packages whose import name is different from their package name..

It only works with packages in the Pyodide distribution, not PyPi, but still might be interesting.

@FabioRosado
Copy link
Contributor Author

It might be worth looking at pyodide.loadPackagesFromImports - they've got a fair bit of infrastructure built up around similar functionality, including mapping known packages whose import name is different from their package name..

It only works with packages in the Pyodide distribution, not PyPi, but still might be interesting.

Awesome thank you for the reference! I was thinking to do something like an index of packages+imports seems like that's what pyodide is using in a way 😄 This will definitely help!

@bugzpodder
Copy link
Contributor
bugzpodder commented May 18, 2023

I was playing around with this, had a few problems I ran into, just thought I'd point it out:

  • very occasionally, py-script might inserted dynamically so they are not present at configure. I've had success doing it at beforePyScriptExec but it needs to be awaited
  • there are some imports that can't be installed, eg js, pyodide, pyScript and any python stdlib imports (eg math, json). py-script's installPackage will throw if they aren't excluded. I think micropip has a keep_going flag that may help but it isn't part of pyscript js api
  • i think this will be super helpful for py-repl rather than py-script (which most people has it static so they know which packages they need). currently in order to dynamically import some package, you have to declare async main, use asyncio to run it and do await micropip install. As a user I do not want to do this in order to install some package.
  • loadPackagesFromImports only works for pyodide packages and not those from PyPI
  • mismatched package names like scikit-learn vs sklearn

// named group called "dependency". The rule will match any text
// that contains 'import' or 'from' at the beginning of the line
// or '\nimport', '\nfrom' which is the case when we invoke _trimWhiteSpace
const importRegexRule = /^(?:\\n)?(?:import|from)\s+(?<dependency>[a-zA-Z0-9_]+)?/gm;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't you add \s* to the beginning of the regex and remove trimWhiteSpace?

Suggested change
const importRegexRule = /^(?:\\n)?(?:import|from)\s+(?<dependency>[a-zA-Z0-9_]+)?/gm;
const importRegexRule = /^(?:\\n)?\s*(?:import|from)\s+(?<dependency>[a-zA-Z0-9_]+)?/gm;

@FabioRosado
Copy link
Contributor Author

Closing this PR since this was a POC for pyscript fun meeting

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.

4 participants
0