Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A question about import

185 views
Skip to first unread message

Gabor Urban

unread,
Feb 16, 2024, 3:07:58 PMFeb 16
to
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.

MRAB

unread,
Feb 16, 2024, 3:35:29 PMFeb 16
to
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.

Cameron Simpson

unread,
Feb 16, 2024, 4:14:19 PMFeb 16
to
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 <c...@cskk.id.au>
0 new messages