The existence of the mymod.tools
attribute is crucial to maintaining proper function of the import system. One of the normal invariants of Python imports is that if a module x.y
is registered in sys.modules
, then the x
module has a y
attribute referring to the x.y
module. Otherwise, things like
import x.y
x.y.y_function()
break, and depending on the Python version, even
from x
import y
You are not importing the tools
module, it's just available when you import the package like you're doing:
import mymod
You will have access to everything defined in the __init__
file and all the modules of this package:
import mymod
# Reference a module
mymod.tools
# Reference a member of a module
mymod.tools.foo
# And any other modules from this package
mymod.tools.subtools.func
When you import foo
inside __init__
you are are just making foo
available there just like if you have defined it there, but of course you defined it in tools
which is a way to organize your package, so now since you imported it inside __init__
you can:
import mymod
mymod.foo()
But you can import foo
without making it available inside __init__
, you can do the following which is exactly the same as the example above:
from mymod.tools
import foo
foo()
Try putting this in your __init__.py
file:
from.tools
import foo
del tools
I have a Python module with the following anycodings_python structure:,But you can import foo without making it anycodings_python-module available inside __init__, you can do anycodings_python-module the following which is exactly the same anycodings_python-module as the example above:,You will have access to everything anycodings_python-module defined in the __init__ file and all the anycodings_python-module modules of this package:,You are not importing the tools module, anycodings_python-module it's just available when you import the anycodings_python-module package like you're doing:
I have a Python module with the following anycodings_python structure:
mymod / __init__.py tools.py
mymod/
__init__.py
tools.py
# __init__.py
from.tools
import foo
# __init__.py
from .tools import foo
# tools.py def foo(): return 42
Funnily enough, if tools.py is called foo.py anycodings_python you get what you want:
mymod.foo()
The existence of the mymod.tools anycodings_python-module attribute is crucial to maintaining anycodings_python-module proper function of the import system. anycodings_python-module One of the normal invariants of Python anycodings_python-module imports is that if a module x.y is anycodings_python-module registered in sys.modules, then the x anycodings_python-module module has a y attribute referring to anycodings_python-module the x.y module. Otherwise, things like
import x.y
x.y.y_function()
break, and depending on the Python anycodings_python-module version, even
from x
import y
You are not importing the tools module, anycodings_python-module it's just available when you import the anycodings_python-module package like you're doing:
import mymod
You will have access to everything anycodings_python-module defined in the __init__ file and all the anycodings_python-module modules of this package:
import mymod
# Reference a module
mymod.tools
# Reference a member of a module
mymod.tools.foo
# And any other modules from this package
mymod.tools.subtools.func
When you import foo inside __init__ you anycodings_python-module are are just making foo available there anycodings_python-module just like if you have defined it there, anycodings_python-module but of course you defined it in tools anycodings_python-module which is a way to organize your package, anycodings_python-module so now since you imported it inside anycodings_python-module __init__ you can:
import mymod
mymod.foo()
But you can import foo without making it anycodings_python-module available inside __init__, you can do anycodings_python-module the following which is exactly the same anycodings_python-module as the example above:
from mymod.tools
import foo
foo()
Try putting this in your __init__.py anycodings_python-module file:
from.tools
import foo
del tools
Modules can import other modules. It is customary but not required to place all import statements at the beginning of a module (or script, for that matter). The imported module names, if placed at the top level of a module (outside any functions or classes), are added to the module’s global namespace.,There is a variant of the import statement that imports names from a module directly into the importing module’s namespace. For example:,Note that relative imports are based on the name of the current module. Since the name of the main module is always "__main__", modules intended for use as the main module of a Python application must always use absolute imports.,This is often used either to provide a convenient user interface to a module, or for testing purposes (running the module as a script executes a test suite).
# Fibonacci numbers module
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while a < n:
print(a, end = ' ')
a, b = b, a + b
print()
def fib2(n): #
return Fibonacci series up to n
result = []
a, b = 0, 1
while a < n:
result.append(a)
a, b = b, a + b
return result
>>>
import fibo
>>> fibo.fib(1000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>>
fibo.fib2(100)[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] >>>
fibo.__name__ 'fibo'
>>> fib = fibo.fib >>> fib(500) 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
>>> from fibo
import fib, fib2
>>>
fib(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
>>> from fibo
import *
>>>
fib(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
If you closely inspect each of these objects, you’ll notice an important difference. The urllib package has a __path__ member that urllib.request does not have:,The important point here is that urllib.request is nested inside urllib. In this case, urllib is a package and request is a normal module.,Many parts of Python’s standard library are implemented as packages. To see an example, open your REPL and import urllib and urllib.request:,If you start Python with no arguments, an empty string is put at the front of sys.path. This instructs Python to import modules from the current directory.
>>>
import urllib >>>
import urllib.request
>>> type(urllib)<class 'module'>>>> type(urllib.request)<class 'module'>
>>> urllib.__path__['./urllib']>>> urllib.request.__path__Traceback (most recent call last): File "<stdin>", line 1, in <module>AttributeError: 'module' object has no attribute '__path__'
>>> sys.path[0]
''
>>> sys.path[-5: ]['/Library/Frameworks/Python.framework/Versions/3.5/lib/python33.zip', '/Library/Framewor\ks/Python.framework/Versions/3.5/lib/python3.5', '/Library/Frameworks/Python.framework/Ve\rsions/3.5/lib/python3.5/plat-darwin', '/Library/Frameworks/Python.framework/Versions/3.5\/lib/python3.5/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.5/lib/pytho\n3.5/site-packages']