Skip to content

Instantly share code, notes, and snippets.

@refraction-ray
Last active January 3, 2019 05:13
Show Gist options
  • Save refraction-ray/22b9ab5721d562eba3ab5c8831176986 to your computer and use it in GitHub Desktop.
Save refraction-ray/22b9ab5721d562eba3ab5c8831176986 to your computer and use it in GitHub Desktop.
Python import mechanism
"demo package to show how python import system is implemented"

This is a demo package to show the implementation details of python import system. Collect the above py files into a folder app. The folder orginization is

root
├── ext.py
│   
├── app
│   ├── __init__.py
│   ├── a.py
│   ├── b.py
│   ├── ...

Outside the app folder, run python3 -m app.c. The stdout is shown below.

------------
you are now entering __main__
c module is now as __main__
in c, import a
------------
you are now entering app.a
before create class A
end create class A
before create obja
initialization of obja
end create obja
end of app.a
------------
in c, import b
------------
you are now entering app.b
in b, import a
initialize list b as [1]
assign attr b to obja
create function modif_list
create function modif_list2
create function pre_defined
begin change the listb
import c in function modify_list2 within b
------------
you are now entering app.c
c module is now as app.c
in c, import a
in c, import b
init tuplec as (2,)
in c, obja.b: 2
in c, listb:  [1]
end of app.c
------------
end change the listb
end of app.b
------------
init tuplec as (2,)
in c, obja.b: 2
in c, listb:  2
end of __main__
------------

You can also try python3 -m app.e or python3 ext.py, the stdout for the latter is

entering app.e
entering app.f
finish import e in f
finish register of fun f1
finish register of fun f2
finish import f in e
finish register of fun e1
finish register of fun e2
finish import e from ext
finish import f from ext
call e2 in ext
e2
f1
call f2 in ext
f2
e1
print("------------")
print("you are now entering %s"%__name__)
print("before create class A")
class A:
def __init__(self,a):
print("initialization of obja")
self.a = a
def other(self):
print("other function of class A is called")
return self.a
print("end create class A")
print("before create obja")
obja = A(1)
print("end create obja")
print("end of %s"%__name__)
print("------------")
print("------------")
print("you are now entering %s"%__name__)
print("in b, import a")
from .a import obja
print("initialize list b as [1]")
listb = [1]
print("assign attr b to obja")
obja.b = 2
print("create function modif_list")
def modify_list(listb):
print("exec the modify list function")
return (1,)
print("create function modif_list2")
def modify_list2(listb):
print("import c in function modify_list2 within b")
from .c import tuplec
pre_defined()
return tuplec
print("create function pre_defined")
def pre_defined():
return 1
print("begin change the listb")
listb = modify_list2(listb)
print("end change the listb")
print("end of %s"%__name__)
print("------------")
print("------------")
print("you are now entering %s"%__name__)
print("c module is now as %s"%__name__)
print("in c, import a")
from .a import obja
print("in c, import b")
from .b import listb
print("init tuplec as (2,)")
tuplec = (2,)
print("in c, obja.b: %s"%obja.b)
print("in c, listb: %s"%listb)
print("end of %s"%__name__)
print("------------")
print("entering %s"%__name__)
import app.f
print("finish import f in e")
def e1():
print("e1")
return "e1"
print("finish register of fun e1")
def e2():
print("e2")
return app.f.f1()
print("finish register of fun e2")
from app.e import e2
print("finish import e from ext")
from app.f import f2
print("finish import f from ext")
print("call e2 in ext")
e2()
print("call f2 in ext")
f2()
print("entering %s"%__name__)
import app.e
print("finish import e in f")
def f1():
print("f1")
return
print("finish register of fun f1")
def f2():
print("f2")
return app.e.e1()
print("finish register of fun f2")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment