-
Notifications
You must be signed in to change notification settings - Fork 164
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
Compiling Python packages with LPython #992
Comments
Yes, we should get LPython working with actual Python packages, that we write ourselves to ensure we stick to the subset that LPython supports. And the goal here is to fix up the inevitable bugs that we either know about or that we will hit. After most things work, it will allow people to upload packages to PyPi and to have dependencies, etc. We can use regular Python tooling to install dependencies and download/upload from PyPi. We have to improve LPython to be able to compile such a package with dependencies. We probably also need to add a mode to compile the library and all dependencies to some kind of a "mod" file that stores the ASR in it, for quick compilation, so that when you modify a script, only that script has to be parsed and compiled to ASR, all other modules can be just deserialized. LFortran works this way, and it works great. Finally, we should then start to write libraries of reusable Python code:
Effectively we need "lite" versions of such libraries, written in pure LPython. We might later venture into web servers too (Flask, etc.), but I would start with numerical / scientific libraries. We can start creating packages in the Python standard library as external 3rd party packages, and then we'll simply download them / pull them into the LPython distribution, since we need to ship with them by default. The same with NumPy. Ideall LPython itself (AST->ASR) has support for all the features that are needed, including a small subset of NumPy, such that all of NumPy and all of the Python Standard Library can be implemented as a regular LPython package, no magic / help from the compiler. Equivalent to user code. |
I think we can re-use the serialization-deserialisation mechanism of LFortran and use it here to compile Python modules till ASR level. In fact, modules written by us right now compile (parsed + AST->ASR) only when we import them. However LFortran pre-compiles them till ASR and loads them if we import in our program. LPython should behave in a similar manner I think. Also, the question is how to integrate LPython with a build system (writing build files in simple words) so that LPython compiles all the intrinsic modules defined in it when its being built from source. |
Yes. I think CPython compiles every module into |
I see. Let me try implementing this idea then. Let's see how far we can get. |
In order to achieve first and second point in #992 (comment), I think we need to write |
For end users the main mode that we want to support is automatic compilation, just like CPython works. Is the CMake based mode just for testing that we can compile things by hand. |
I think
By automatic compilation you mean that a module will be compiled to |
We will probably support both modes:
I think most users would prefer the second approach. Or to rephrase it -- the cmake build system can be automatically generated by LPython, so why bother and not just compile things directly? We know all the information. |
I see. Makes sense. Second approach would be much easier. We will have to implement timestamp strategy to check whether the file has been changed or not. Do you know how to fetch the timestamp the last time a file was modified on disk a.k.a modification time? https://stackoverflow.com/a/40504396 seems to be a promising way to do get the modification time of a file but if you know of anything better then we can go for that as well. |
So I am planning to implement the strategy in my above comment. However the problem is that when you auto-compile and re-use the auto-compiled modules then symbol table IDs depend on the order in which you compile those modules. This would have consequences on |
This is dealt with in the Fortran desearilization by constructing a new symbol table and resolving it correctly. So everything should just work, as long as we reuse all the code, which I strongly recommend. :) |
To move forward:
Issues that we need to tackle:
|
Here is our goal:
|
So what pieces do we need to get to the goal?
What does that mean?
The second option is more work for us, and we can tackle it later. We need something similar for LFortran as well, so we can work towards that as we go. So far I like the idea |
So the first task can be: Also what we need are nested modules. Say:
And we need to get the following working in from a.b import x
import a.b
from a import b plus the relative import syntax in Python. We need to get One issue is how to tell LPython where to look for the "a" package if you do |
Once the above works, we can think about how to build dependencies. I suggest we use |
@Shaikh-Ubaid are you working on this issue? |
Yes, @Shaikh-Ubaid is working on this issue, but if you are interested @Thirumalai-Shaktivel you can go ahead and also work on it, there is plenty of tasks to do. |
I just tested it, here is the latest status on this: |
It's time to start writing LPython packages, upload to pypi, make them pip installable and see what breaks, see #992 (comment). Report issues that break and we'll fix it. |
Here are three ideas that we can do right away:
Make it pip installable (from github). |
I'll start with the first one. |
@Shaikh-Ubaid I figured out the instructions how to install some package using conda create -n test1 python=3.11
conda activate test1
pip install lpynn lpdraw
lpython -I$CONDA_PREFIX/lib/python3.11/site-packages/ test_pkg_lnn_01.py where conda install numpy
python test_pkg_lnn_01.py It looks like we can do:
Let's get a few more packages working. Also let's iterate, perhaps something like this: |
There is also
Sure. |
It looks like the |
I would version |
I am upgrading this package: certik/mathfn#5, I can see a few things:
|
We recently released LPython |
That's what I would do. Do you see a problem with it? |
Got it. Sure, we can do that. It seems |
LPython depends on |
Got it, let's focus on delivering |
Following is the plan to achieve the goal mentioned in issue title,
__init__.py
file to 3-4 folders/subpackages one inside the other each level having an__init__.py
. We can write packages for different sorting algorithms, graph algorithms and some practical stuff like that. We should try not to use workarounds but write code in a way that feels natural to us.Alongside we should also try to interface with CPython code (#703).
@certik What do you say?
The text was updated successfully, but these errors were encountered: