Python module definition, module import__ name__ Analysis of usage examples

Time:2021-7-22

This paper introduces the definition and import of Python module__ name__ Usage. The details are as follows:

Related contents:

  • What is a module
  • Module import
    • Module import
    • Import from module
      • Peer directory import
      • Different level directory import
        • In directory import out of directory
        • Import from out of directory to in directory
  • __name__

What is a module

In Python, a module is a repository of methods and classes. If we want to use a method or class in a module, we need to import the corresponding template.

[Python has built-in methods and classes, so we don’t need to import modules for some methods]

Module usage: module. Function,      Module. Class

#Function
import math
print(math.sqrt(3))
#Class
import threading
a=threading.Thread()
print(a)

Module import:

 

  • Module import:

    • Import module name
    #Import module name
    import math
    print(math.sqrt(9))
    • From… Import statement: import a specified part from the module, such as class and method, where from… Import * represents importing all contents
#From module name, import class name, method name
from collections import Iterable 
print(isinstance("abc",Iterable))
print(isinstance([1,2,3],Iterable))
  • When importing from… Import, you can also give the imported part a custom name: for example: from… Import funca as FA
  • Import from module:

Search path of Python import

1. Search the module in the current directory

2. Search the path list specified in the environment variable pythonpath in turn [can be obtained from sys. Path]

import sys
print(sys.path)

----------
Results of operation:
['J:\HardWork\Code\python', 
'J:\HardWork\Code\python',
 'I:\python3\python36.zip',
 'I:\python3\DLLs', 
'I:\python3\lib',
 'I:\python3', 
'I:\python3\lib\site-packages']

3. Search the Lib Library of Python installation path

  • Peer directory import: direct import

 image_thumb

image_thumb[2]

image_thumb[4]

  • Different level directory import: add the corresponding directory to sys.path, and then import

    • B in the directory is imported into C out of the directory

Directory structure:image

C content:image

How to import C into B

import sys,os
print(__ file__)## This is the absolute path to the current file
parent_ dir=os.path.dirname(__ file__)## Gets the directory of the current file
pp_dir=os.path.dirname(parent_dir)
sys.path.append(pp_dir)
# print(sys.path)
Import C # to import C, diff_ Add dir to sys.path
    • C outside the directory is imported into D inside the directory

Directory structure:image

D. contents:image

How to import D in C


from d2 import d

Or:


import d2.d

result:image


__name__:

Because modules can be imported or run directly, in order to distinguish what is the content of direct run-time running, there are many problems__ name__:

 

When running directly__ name__ Yes__ main__:

print("import C done")
print(__name__)

if __name__=='__main__':
 print("direct run")

---------------
Results of operation:
import C done
__main__
direct run

When used for import__ name__ Is the name of the currently imported module__ name__ Not equal to__ main__, So it won’t be implementedimage_thumb[7]

import sys,os

# print(__ file__)## This is the absolute path to the current file
parent_ dir=os.path.dirname(__ file__)## Gets the directory of the current file
pp_dir=os.path.dirname(parent_dir)
sys.path.append(pp_dir)
# print(sys.path)
Import C # to import C, diff_ Add dir to sys.path




------------------
Results of operation:
import C done
c

More about Python related content, interested readers can see the site topic: “Python file and directory operation skills summary”, “Python text file operation skills summary”, “Python data structure and algorithm tutorial”, “Python function use skills summary”, “Python string operation skills summary” and “Python introduction and advanced classic tutorial”

I hope this article is helpful for Python programming.