& Introduction path adding module

Module is divided into three types:

  • Custom Modules
  • Third party modules
  • Built-in module

Python reason why more and more widely, to a certain extent also depends on its offers a number of modules for use as a programmer, if you want to use the module, you need to import. There are several ways to look at importing module:

1 import module
2 from module.xx.xx import xx
3 from module.xx.xx import xx as rename 
4 from module.xx.xx import *

Import module in fact, tell the Python interpreter to interpret the py file

  • Import a file py, py interpreter interprets the file
  • Introducing a packet interpreter interpret py2.7] [__init__.py file in the package

So the question is, when you import the module is carried out according to the path as a reference of it? Namely: sys.path

1 import sys
2 print sys.path

If no path sys.path list of paths you want, you can add by sys.path.append ( 'path').

1 import sys
2 import os
3 project_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
4 sys.path.append(project_path)

 

Guess you like

Origin www.cnblogs.com/zhemeshenqi/p/12354869.html