K
K
KondakovVladimir2020-05-02 01:22:29
Python
KondakovVladimir, 2020-05-02 01:22:29

Why are the modules inside the package not visible?

Hello! Faced strange behavior of pylint in microsoft visual studio code. Directory structure:

test\
  a.py
  b.py
  c.py

in module a.py here is the code
import b
import c

def test():
    b.say()
    c.say()

if __name__ == "__main__":
    test()

The code works and does not throw errors
if you add the index file __init__.py to the directory pylint starts to give an error "Unable to import 'b'",
while the code continues to work, but if you call this module from outside, it gives an error
File "/test_bot.py", line 29, in <module>
    import test.a as test
  File "/test/a.py", line 1, in <module>
    import b
ModuleNotFoundError: No module named 'b'


It turns out that the modules inside the package cannot see each other. This is true? Or am I doing something wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
KondakovVladimir, 2020-05-02
@KondakovVladimir

Found a solution, but maybe it's a crutch

if __name__ == "__main__":
    import b
    import c
else:
    from test import b
    from test import c

If the module is launched by itself, the call via import is relevant for it, but if as part of the package, then import via from is available
, at least it works, despite the error from the linter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question