Skip to content

Importing modules does not work #1305

Closed

Description

@certik
$ 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).

Activity

  1. certik commented on Nov 16, 2022

    @certik
    ContributorAuthor

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

  2. Thirumalai-Shaktivel commented on Nov 17, 2022

    @Thirumalai-Shaktivel
    Collaborator

    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.

  3. Thirumalai-Shaktivel commented on Nov 18, 2022

    @Thirumalai-Shaktivel
    Collaborator

    Fixed here: #1306

  4. certik commented on Nov 18, 2022

    @certik
    ContributorAuthor

    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).
  5. Thirumalai-Shaktivel commented on Nov 19, 2022

    @Thirumalai-Shaktivel
    Collaborator

    Fixed here: #1315

  6. certik commented on Nov 20, 2022

    @certik
    ContributorAuthor

    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?

  7. Thirumalai-Shaktivel commented on Nov 21, 2022

    @Thirumalai-Shaktivel
    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.

  8. certik commented on Nov 21, 2022

    @certik
    ContributorAuthor

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

  9. czgdp1807 commented on Nov 21, 2022

    @czgdp1807
    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.

  10. Thirumalai-Shaktivel commented on Nov 21, 2022

    @Thirumalai-Shaktivel
    Collaborator

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

  11. Thirumalai-Shaktivel commented on Nov 21, 2022

    @Thirumalai-Shaktivel
    Collaborator

    I referenced this as a new issue.

  12. certik commented on Nov 21, 2022

    @certik
    ContributorAuthor

    Thanks!

    #1317

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions