• Re: A question about import

    From Cameron Simpson@21:1/5 to MRAB on Sat Feb 17 08:13:50 2024
    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>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From MRAB@21:1/5 to Gabor Urban via Python-list on Fri Feb 16 20:32:05 2024
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)