Mailing List Archive

Module import question
Hi guys,

I have a quite simple question but I could not find the correct answer.

I have twoo modules A and B. A imports B. If I import A in a script, Will
be B imported automatically? I guess not, but f? not know exactly.

Thanks for your answer ín advance,

Gábor
--
https://mail.python.org/mailman/listinfo/python-list
Re: Module import question [ In reply to ]
Gabor Urban <urbangabo@gmail.com> writes:

> Hi guys,
>
> I have a quite simple question but I could not find the correct answer.
>
> I have twoo modules A and B. A imports B. If I import A in a script, Will
> be B imported automatically? I guess not, but f? not know exactly.
>
> Thanks for your answer ín advance,

#+BEGIN_SRC: sh + python
(bionic)soyeomul@localhost:~/222$ cat b.py
name = "b"
(bionic)soyeomul@localhost:~/222$ cat a.py
import b

name = "a"
(bionic)soyeomul@localhost:~/222$ cat c.py
import a

print(a.name) # a.py's name
print(a.b.name) # b.py's name
(bionic)soyeomul@localhost:~/222$ python3 c.py
a
b
(bionic)soyeomul@localhost:~/222$
#+END_SRC

Sincerely, Byung-Hee

--
^????? _????_ ?????_^))//
--
https://mail.python.org/mailman/listinfo/python-list
Re: Module import question [ In reply to ]
On 8/9/20 12:51 AM, Gabor Urban wrote:
> Hi guys,
>
> I have a quite simple question but I could not find the correct answer.
>
> I have twoo modules A and B. A imports B. If I import A in a script, Will
> be B imported automatically? I guess not, but f? not know exactly.
>
> Thanks for your answer ín advance,

Think of import as meaning "make available in namespace".

If A simply imports B, then B is available in A's namespace as a name
for module B's namespace, and you can access things inside B by
qualifying the names: if Foo is in B, then B.Foo works, Foo does not.
Different forms of the import statement change the way symbols are made
available, e.g you can do

from B import * # all the symbols from B are in A's global namespace,
Foo works now
import B as Baz # B is available through the name Baz, so use Baz.Foo
etc.

If you have a separate program, call it C, and it imports A, then A is
available in C as a name for module A's namespace. That said nothing
about B, so the symbol B is not available in C. But if C calls
something in A that uses B, then that will work fine, because B exists
in A's namespace. And you can access symbols from B by properly
qualifying: A.B.Foo.

Which is it you meant by "imported automatically"?

See Byung-Hee's example to see this in action without all these messy
words :)
--
https://mail.python.org/mailman/listinfo/python-list
Re: Module import question [ In reply to ]
Hi guys,

Thanks for the answers. IT is clear Noé.

Gábor
--
https://mail.python.org/mailman/listinfo/python-list
Re: Module import question [ In reply to ]
On Aug 9, 2020 11:41 AM, "Mats Wichmann" <mats@python.org> wrote:
>
> On 8/9/20 12:51 AM, Gabor Urban wrote:
> > Hi guys,
> >
> > I have a quite simple question but I could not find the correct answer.
> >
> > I have twoo modules A and B. A imports B. If I import A in a script,
Will
> > be B imported automatically? I guess not, but f? not know exactly.
> >
> > Thanks for your answer ín advance,
>
> Think of import as meaning "make available in namespace".

Well it's actually a little more involved than that. When python import a
module it executes the module code. This is why you often see at the bottom
of a module:

if __name__ == '__main__':
# code to execute when running the module as opposed to importing it.

When importing a module __name__ is the module's name rather than
'__main__'.

What happens when module A Imports module B depends on whether or not the
import B statement is actually executed.
--
https://mail.python.org/mailman/listinfo/python-list