Propagate import for all modules in a package.

P

Phil

I'm really new to Python and I am absolutely stumped trying to figure
this out. I have searched plenty, but I am either searching for the
wrong keywords or this isn't possible.

What I want to do is have one import be global for the entire package.
Here is an example...

<package>
__init__.py
module1.py
module2.py
...
moduleN.py

I was thinking that I could just, for example, 'from datetime import
datetime' in __init__.py and have the ability to use 'datetime'
anywhere in any of the modules in 'package'.

This didn't work for me. Did I just do something wrong? Is what I am
trying to do possible?

Thanks in advance.
 
D

Diez B. Roggisch

Phil said:
I'm really new to Python and I am absolutely stumped trying to figure
this out. I have searched plenty, but I am either searching for the
wrong keywords or this isn't possible.

What I want to do is have one import be global for the entire package.
Here is an example...

<package>
__init__.py
module1.py
module2.py
...
moduleN.py

I was thinking that I could just, for example, 'from datetime import
datetime' in __init__.py and have the ability to use 'datetime'
anywhere in any of the modules in 'package'.

This didn't work for me. Did I just do something wrong? Is what I am
trying to do possible?

Each module has it's own namespace, there is no real global one. So
there is no obvious and recommended way to do what you want.

You can do it through, by stuffing names into the __builtins__-module,
which acts as "namespace of last resort"


from datetime import datetime
__builtins__['datetime'] = datetime

However, this is strongly discouraged - you can easily get
name-conflicts, and undefined behavior in your modules if you rely on this.

Diez
 
A

Albert Hopkins

I'm really new to Python and I am absolutely stumped trying to figure
this out. I have searched plenty, but I am either searching for the
wrong keywords or this isn't possible.

What I want to do is have one import be global for the entire package.
Here is an example...

<package>
__init__.py
module1.py
module2.py
...
moduleN.py

I was thinking that I could just, for example, 'from datetime import
datetime' in __init__.py and have the ability to use 'datetime'
anywhere in any of the modules in 'package'.

This didn't work for me. Did I just do something wrong? Is what I am
trying to do possible?

That's not how packages (were designed to) work. A package is basically
a namespace. It doesn't do anything special outside of providing a
namespace for common modules. There are some special features (e.g.
__init__.py which is basically the "body" of the package and '__all__'
which handles wildcard imports of a package) but other than that it's
just a namespace.
 
P

Phil

Thanks to both of you for the fast and detailed responses. I will just
treat the package for what it is, a namespace.
 
P

Phil

Thanks to both of you for the fast and detailed responses. I will
just
use the package for what it is, a namespace.
 
P

Phil

Thanks to both of you for the fast and detailed responses. I will just
use the package for what it is, a namespace.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top