• A question about import

    From Gabor Urban@urbangabo@gmail.com to comp.lang.python on Fri Feb 16 21:07:24 2024
    From Newsgroup: comp.lang.python

    Hi guys,
    I need something about modules to be clarified.
    Suppose I have written a module eg: ModuleA which imports an other
    module, let us say the datetime.
    If I import ModuleA in a script, will be datetime imported automatically? Thanks in advance,
    --
    Urbán Gábor
    Linux is like a wigwam: no Gates, no Windows and an Apache inside.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From MRAB@python@mrabarnett.plus.com to comp.lang.python on Fri Feb 16 20:32:05 2024
    From Newsgroup: comp.lang.python

    On 2024-02-16 20:07, Gabor Urban via Python-list wrote:
    Hi guys,

    I need something about modules to be clarified.

    Suppose I have written a module eg: ModuleA which imports an other
    module, let us say the datetime.

    If I import ModuleA in a script, will be datetime imported automatically?

    Yes. When a module is imported it can import other modules.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Cameron Simpson@cs@cskk.id.au to comp.lang.python on Sat Feb 17 08:13:50 2024
    From Newsgroup: comp.lang.python

    On 16Feb2024 20:32, MRAB <python@mrabarnett.plus.com> wrote:
    On 2024-02-16 20:07, Gabor Urban via Python-list wrote:
    I need something about modules to be clarified.

    Suppose I have written a module eg: ModuleA which imports an other
    module, let us say the datetime.

    If I import ModuleA in a script, will be datetime imported automatically?

    Yes. When a module is imported it can import other modules.

    But note that `datetime` does not magicly get put in the script's
    namespace.

    Module A:

    import datetime

    Script:

    import A

    In the code in module A the name datetime is known and can be used.

    In the code in the script the name A is known and can be used. Importing
    A does not magicly set the name datetime in the script's namespace -
    imagine the the pollution!

    You _can_ access it as A.datetime because it is in the A module's
    namespace. But really if you just wanted datetime for direct use in the
    script you would import it there too:

    import datetime
    import A

    Note that the datetime module is only actually loaded once. The import
    binds the name into your local namespace like any other variable.

    Cheers,
    Cameron Simpson <cs@cskk.id.au>
    --- Synchronet 3.20a-Linux NewsLink 1.114