referring to package scope from module, using relative import?

I

Irmen de Jong

Hi,

I have a package with several modules in it. The package also has some objects created
in the package scope (done in the package __init__.py).

Is it possible to access those package scope objects from the modules, with relative
imports or something? So that I don't have to import the package itself in its own
submodules?


Example:

A/
__init__.py -> creates A.something
thing.py -> needs to do "import A" to access A.something
would like to not have to import A


I think it's something simple I'm missing...

Irmen
 
I

Ian Kelly

Hi,

I have a package with several modules in it. The package also has some objects created
in the package scope (done in the package __init__.py).

Is it possible to access those package scope objects from the modules, with relative
imports or something? So that I don't have to import the package itself in its own
submodules?


Example:

A/
  __init__.py    ->  creates A.something
  thing.py       ->  needs to do "import A"  to access A.something
                     would like to not have to import A

You can do the relative import like this:

from . import something

Or if something were defined in A/otherthing.py, then:

from .otherthing import something

Note that PEP 8 discourages relative imports and encourages absolute
imports, though. This would be the preferred way to do it:

from A import something

Cheers,
Ian
 
I

Irmen de Jong

Note that PEP 8 discourages relative imports and encourages absolute
imports, though. This would be the preferred way to do it:

from A import something

Right. I got rid of the silly relative import stuff. As an added bonus, this makes my
original question irrelevant :)

Irmen
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top