Skip to content
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

Importing modules does not work #1305

Closed
certik opened this issue Nov 16, 2022 · 12 comments
Closed

Importing modules does not work #1305

certik opened this issue Nov 16, 2022 · 12 comments

Comments

@certik
Copy link
Contributor

certik commented Nov 16, 2022

$ tree      
.
├── a
│   ├── __init__.py
│   └── b.py
└── r.py

1 directory, 3 files
$ cat a/b.py 
def f():
    print("b.f() called")
$ cat r.py 
from a.b import f
f()

This gives:

$ lpython r.py 
semantic error: Could not find the module '__init__.py'. If an import path is available, please use the `-I` option to specify it
 --> r.py:1:1
  |
1 | from a.b import f
  | ^^^^^^^^^^^^^^^^^ 


Note: if any of the above error or warning messages are not clear or are lacking
context please report it to us (we consider that a bug that needs to be fixed).
$ lpython -I . r.py
semantic error: Could not find the module '__init__.py'. If an import path is available, please use the `-I` option to specify it
 --> r.py:1:1
  |
1 | from a.b import f
  | ^^^^^^^^^^^^^^^^^ 


Note: if any of the above error or warning messages are not clear or are lacking
context please report it to us (we consider that a bug that needs to be fixed).
@certik
Copy link
Contributor Author

certik commented Nov 16, 2022

@Thirumalai-Shaktivel any idea why this does not work?

This was referenced Nov 16, 2022
@Thirumalai-Shaktivel
Copy link
Collaborator

Thirumalai-Shaktivel commented Nov 17, 2022

Currently, we don't support

from a.b import f

Instead, this works:

from a import f
$ lpython r.py
b.f() called

I will try to work on this soon.

@Thirumalai-Shaktivel
Copy link
Collaborator

Fixed here: #1306

@certik
Copy link
Contributor Author

certik commented Nov 18, 2022

Ok, the above now works with #1306, but more deeply nested doesn't:

$ tree a
a
├── __init__.py
├── b.py
└── c
    ├── __init__.py
    └── d.py
(lf) repos/lpython(nested_modules_02) $ cat a/c/d.py 
def g():
    print("a.c.g() called")
(lf) repos/lpython(nested_modules_02) $ cat r.py 
from a.b import f
f()
from a.c.d import g
g()
(lf) repos/lpython(nested_modules_02) $ python r.py 
b.f() called
a.c.g() called
(lf) repos/lpython(nested_modules_02) $ lpython r.py
semantic error: Could not find the module 'a/c.d.py'. If an import path is available, please use the `-I` option to specify it
 --> r.py:3:1
  |
3 | from a.c.d import g
  | ^^^^^^^^^^^^^^^^^^^ 


Note: if any of the above error or warning messages are not clear or are lacking
context please report it to us (we consider that a bug that needs to be fixed).

@Thirumalai-Shaktivel
Copy link
Collaborator

Fixed here: #1315

@certik
Copy link
Contributor Author

certik commented Nov 20, 2022

I think it works now!

I can't think of any case that doesn't work, so I am going to close this issue as fixed. If we discover more things that don't work, I'll open a new issue.

@Thirumalai-Shaktivel, @czgdp1807 are we ready now to start making Python packages that compile with LPython?

@Thirumalai-Shaktivel
Copy link
Collaborator

I came across another issue that doesn't work yet. Like for this example:

import test_import

def test():
    print(test_import.print_a())
    print(test_import.print_b())
    print(test_import.print_c())

test()

Error:

lpython integration_tests/test_import_01.py
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  Binary file "/home/thirumalai/Open_Source/lpython/src/bin/lpython", in _start()
  File "./csu/../csu/libc-start.c", line 392, in __libc_start_main_impl()
  File "./csu/../sysdeps/nptl/libc_start_call_main.h", line 58, in __libc_start_call_main()
  File "/home/thirumalai/Open_Source/lpython/src/bin/lpython.cpp", line 1545, in ??
    err = compile_python_to_object_file(arg_file, tmp_o, runtime_library_dir, lpython_pass_manager, compiler_options, time_report);
  File "/home/thirumalai/Open_Source/lpython/src/bin/lpython.cpp", line 592, in ??
    !(arg_c && compiler_options.disable_main), infile);
AssertFailed: asr_verify(*tu, true, diagnostics)

I will try to work on this later this week. After that, I think we are ready to start making python packages.

@certik
Copy link
Contributor Author

certik commented Nov 21, 2022

Can you open up a dedicated issue for this and close this issue?

@czgdp1807
Copy link
Collaborator

@Thirumalai-Shaktivel, @czgdp1807 are we ready now to start making Python packages that compile with LPython?

At least I will start writing code in a modular way and compile it using LPython. Probably by the end of this week or early next week.

@Thirumalai-Shaktivel
Copy link
Collaborator

Yup, that would be very helpful. That way, we can test and report any bugs.

@Thirumalai-Shaktivel
Copy link
Collaborator

I referenced this as a new issue.

@certik
Copy link
Contributor Author

certik commented Nov 21, 2022

Thanks!

#1317

@certik certik closed this as completed Nov 21, 2022
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

No branches or pull requests

3 participants