python 3.5 vs python 2.7: modules importing submodules

  • Last Update :
  • Techknowledgy :

Use an explicit relative import:

from.import subapi

Suggestion : 2

To support this, Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode).,you can make the file usable as a script as well as an importable module, because the code that parses the command line only runs if the module is executed as the “main” file:,There is a variant of the import statement that imports names from a module directly into the importing module’s namespace. For example:,A module can contain executable statements as well as function definitions. These statements are intended to initialize the module. They are executed only the first time the module name is encountered in an import statement. [1] (They are also run if the file is executed as a script.)

# 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

Suggestion : 3

Note that when using from package import item, the item can be either a submodule (or subpackage) of the package, or some other name defined in the package, like a function, class or variable. The import statement first tests whether the item is defined in the package; if not, it assumes it is a module and attempts to load it. If it fails to find it, an ImportError exception is raised.,When packages are structured into subpackages (as with the sound package in the example), you can use absolute imports to refer to submodules of siblings packages. For example, if the module sound.filters.vocoder needs to use the echo module in the sound.effects package, it can use from sound.effects import echo.,Remember, there is nothing wrong with using from Package import specific_submodule! In fact, this is the recommended notation unless the importing module needs to use submodules with the same name from different packages.,Contrarily, when using syntax like import item.subitem.subsubitem, each item except for the last must be a package; the last item can be a module or a package but can’t be a class or function or variable defined in the previous item.

# Fibonacci numbers module

def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
   print b,
   a, b = b, a + b

def fib2(n): #
return Fibonacci series up to n
result = []
a, b = 0, 1
while b < n:
   result.append(b)
a, b = b, a + b
return result
>>>
import fibo
>>> fibo.fib(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
   >>>
   fibo.fib2(100)[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] >>>
   fibo.__name__ 'fibo'
>>> fib = fibo.fib >>>
   fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377
>>> from fibo
import fib, fib2
   >>>
   fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377
>>> from fibo
import *
>>>
fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377

Suggestion : 4

The output from all the example programs from PyMOTW has been generated with Python 2.7.8, unless otherwise noted. Some of the features described here may not be available in earlier versions of Python.,If you are looking for examples that work under Python 3, please refer to the PyMOTW-3 section of the site.,A cache value of None means to use the default filesystem loader. Each missing directory is associated with an imp.NullImporter instance, since modules cannot be imported from directories that do not exist. In the example output below, several zipimport.zipimporter instances are used to manage EGG files found on the path.,The output of this script will vary, especially if run with a custom-built version of the interpreter. This output was created using a copy of the interpreter installed from the standard python.org installer for OS X.

import sys
import textwrap

names = sorted(sys.modules.keys())
name_text = ', '.join(names)

print textwrap.fill(name_text)
$ python sys_modules.py

UserDict, __builtin__, __main__, _abcoll, _codecs, _sre, _warnings,
_weakref, _weakrefset, abc, codecs, copy_reg, encodings,
encodings.__builtin__, encodings.aliases, encodings.codecs,
   encodings.encodings, encodings.utf_8, errno, exceptions, genericpath,
   linecache, os, os.path, posix, posixpath, re, signal, site,
   sphinxcontrib, sre_compile, sre_constants, sre_parse, stat, string,
   strop, sys, textwrap, types, warnings, zipimport
import sys

for name in sys.builtin_module_names:
   print name
$ python sys_builtins.py

__builtin__
__main__
_ast
_codecs
_sre
_symtable
_warnings
_weakref
errno
exceptions
gc
imp
marshal
posix
pwd
signal
sys
thread
xxsubtype
zipimport
import sys

for d in sys.path:
   print d
$ python sys_path_show.py /
   Users / dhellmann / Documents / PyMOTW / src / PyMOTW / sys /
   Library / Frameworks / Python.framework / Versions / 2.6 / lib / python2 .6 /
   Library / Frameworks / Python.framework / Versions / 2.6 / lib / python2 .6 / plat - darwin /
   Library / Frameworks / Python.framework / Versions / 2.6 / lib / python2 .6 / lib - tk /
   Library / Frameworks / Python.framework / Versions / 2.6 / lib / python2 .6 / plat - mac /
   Library / Frameworks / Python.framework / Versions / 2.6 / lib / python2 .6 / plat - mac / lib - scriptpackages /
   Library / Frameworks / Python.framework / Versions / 2.6 / lib / python2 .6 / site - packages

Suggestion : 5

The return value is a new reference to the imported module or top-level package, or NULL with an exception set on failure (before Python 2.4, the module may still be created in this case). Like for __import__(), the return value when a submodule of a package was requested is normally the top-level package, unless a non-empty fromlist was given.,The return value is a new reference to the imported module or top-level package, or NULL with an exception set on failure. Like for __import__(), the return value when a submodule of a package was requested is normally the top-level package, unless a non-empty fromlist was given.,Reload a module. This is best described by referring to the built-in Python function reload(), as the standard reload() function calls this function directly. Return a new reference to the reloaded module, or NULL with an exception set on failure (the module still exists in this case).,Return the module object corresponding to a module name. The name argument may be of the form package.module. First check the modules dictionary if there’s one there, and if not, create a new one and insert it in the modules dictionary. Return NULL with an exception set on failure.

struct _frozen {
   char * name;
   unsigned char * code;
   int size;
};
struct _inittab {
   char * name;
   void( * initfunc)(void);
};