python import syntax

import can be used independently:

import module
import package

It can be used to import a module, a package, or . concated module/packages.

import package.module
import package1.package2
import package1.package2.module

So if you see “import xxx.yyy.zzz”, you can tell xxx,yyy are packages while zzz is either a package or a module. After importing a package/module, if you want to use a var/function/class/module in that package/module, you still need to write the package/module in front of the var/function/class/module:

import matplotlib
#import pyplot //cannot use it directly:cannot find module pyplot
import matplotlib.pyplot
#plot(...) //plot is not defined
#pyplot.plot(....) //pyplot is not defined
matplotlib.pyplot.plot(...) //good

If you think it’s not convenient to write a long path, you can use “import xxx.yyy.zzz as aaa”. Then you can use aaa to replace xxx.yyy.zzz

import matplotlib.pyplot as plt
plt.plot(...)

To omit even the “plt” and use functions/variables/classes directly, you should use the “from…import…” syntax.

“import…” can not be used to import a variable/function/class in a module:

#import module.var

To import a variable/function/class in a module, you  need to use the “from… import…” statement instead:

from module import var
from package.module import var
from package1.package2.module import var
from package1 import package2
from package1.package2 import module

In “from…import” statements, “from” can be followed by module, package, or dot concated packages/module. But “import” can only be followed by var/function/class, module, or package. “import” can not be followed by dot concated packages/module/var.

#from package1 import package2.module
#from package import module.var
#from package1 import package2.module.var

From the above discussion, you can see you cannot tell the name after the from keyword is a package or module, and you can not tell the name after the import keyword is a package name, a module name  or a variable name, either. You just know the stuff after “from” is either a package or a module. To use a var/function/class directly without a prefix, you should write the whole dot concated name path up to the var/function/class after the from keyword, and put the name of the var/function/class after the import keyword. So if you see a name is used directly and the name is a function/variable/class based on its usage, and the name occurred after the “import” in the “from…import…” statement, you can tell the stuff after the “from” is a module or a path to a module. It is interesting that the following code does not work.

from  matplotlib import pyplot
#from pyplot import plot //No module named 'pyplot'

 

It seems that from package, we can only import module. Actually, we can import var/function/class of module from package provided that the package imports the var/function/class  in its __init__.py.

#a.py
from package import var
print var

 

#__init__.py of package
from module import var
#import module

 

Consider the following situation:

#a.py
from package1 import package2
#print var
#print module.var
print package2.var
print package2.module.var

 

#__init__.py of package2
from module import var

Although __init__.py of package2 imports var in module and can use var directly, a.py can not use var directly. It must use package2. to access var, or use package2.module. to access var. If __init__.py changes to:

#__init__.py of package2
import module

a.py can only access var via package2.module.var.

import (whether the current script is executed by python or imported by another script) finds packages in current directory(the same directory as the script) before searching in sys.path. If the current script is imported by another script, this is equivalent to prefixing a dot before the imported package/module(the dot prefix is only valid in from…import, not in independent import):

from package import module
from .package import module
#import .package

The dot prefix can not be used by a script that is run by python(the main module), otherwise, you will get the following error:

ValueError: Attempted relative import in non-package

The dot-prefix form of “from…import” is called relative import. Every dot goes up one level of directory until the directory of the script run by python.

Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:

Leave a Reply