Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #96247

Question about import

From "Frank Millman" <frank@chagford.com>
Subject Question about import
Date 2015-09-10 09:12 +0200
Newsgroups comp.lang.python
Message-ID <mailman.311.1441869188.8327.python-list@python.org> (permalink)

Show all headers | View raw


Hi all

My project comprises a number of modules, split into packages. Modules 
frequently need to access the contents of other modules, in the same or in a 
different package. I am getting better at it, but I still occasionally bump 
my head against circular imports, and have to fiddle around until it settles 
down again. Not ideal, I know.

I have just noticed something odd, and I wondered if it might provide a 
solution, or if it is a dangerous sidetrack. Here is a simple example -

/test
| start.py
  /a
  | aa.py
  /b
  | bb.py

start.py
    import a.aa
    import b.bb
    a.aa.aaa()
    b.bb.bbb()

aa.py
    import b
    def aaa():
        print('in aaa')
        b.bb.bbbb()
    def aaaa():
            print('in aaaa')

bb.py
    import a
    def bbb():
        print('in bbb')
        a.aa.aaaa()
    def bbbb():
        print('in bbbb')

c:\test>start.py
in aaa
in bbbb
in bbb
in aaaa

The surprising thing is that, within aa.py, I just have to say 'import b', 
and I can access 'b.bb.bbbb', and the same applies to 'bb.py'.

That makes me wonder if, in my project, I can import all modules inside 
'start.py', and then just use 'import package_name' inside each module?

Another question - I thought that, because aa.py and bb.py are in different 
sub-directories, I would have to set them up as packages by adding 
'__init__.py' to each one, but it works fine without that. What am I 
missing?

I am using python 3.4.

Any comments appreciated.

Frank Millman

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Question about import "Frank Millman" <frank@chagford.com> - 2015-09-10 09:12 +0200

csiph-web