A first look at Python module files


What is a module?



Each .py file is a module, and other files can import a module Reading the contents of this module is equivalent to include in C... A large program often takes the form of multiple modules. One of the module files is designed as the main file (or top-level file).


Module import:



Here we make the simplest example, more More on this later. Here, put the script1 you just wrote in the location where we installed Python:

Then start-run-python:




10. import, from and reload
  • Modules can only be imported once
Importing is such an expensive operation that it can only be imported once per program run. You want to run it again unless you call the built-in reload function:


The difference between import and reload is that import does not require parentheses, it is a statement, and reload is a function whose return value is a python module object.
  • Modules play the role of tool libraries: modules are often encapsulations of variable names and can be regarded as namespaces; variable names in a package are attributes, or in other words, attributes are bound to Variable name on a specific object. A module can usually have two Python statements to read in: import and from, and the use of the reload function
  • The import statement references the module as a whole, which is equivalent to introducing a class. object. You will understand by looking at the effect below. myfile is referenced as a module, and then script1 can use the properties of myfile.
  • Another citation method:



As shown in the figure, from and import are very similar, except that the latter adds an additional assignment to the variable name, which is to copy the attributes of the module, so it can be imported with title instead of the myfile object.


  • We will talk about the knowledge of modules in detail in the following chapters. Here is another import example. Some friends asked, why is the import not like #include in C, which can subsequently call the methods and properties of this module, but is it executed directly? Here is an explanation. It is not that methods of this module cannot be called, but that the above example is relatively simple. It does not involve class encapsulation and only has a few execution statements. The example given below is a simple module example. In this example, Worker.py defines the Worker class, and the external module is imported through import Worker to perform related operations.




    #import&fromSummary version:



## dir(myfile) is to get all the attributes in myfile, where the underline in front is Python’s predefined built-in variable name


Other concepts of modules:


The module is the largest program structure of the Python program
  • Each module file is an independent and complete variable package, that is, a namespace
  • execfile('myfile.py') is another way to execute a program, but essentially it does not import the module
Commonly used Python modules


    difflib python diff comparison module
  • datetime python module for processing date and time
  • chardet string/ File encoding detection (very useful)
  • adodb:我们领导推荐的数据库连接组件
    bsddb3:BerkeleyDB的连接组件
    Cheetah-1.0:我比较喜欢这个版本的cheetah
    cherrypy:一个WEB framework
    ctypes:用来调用动态链接库
    DBUtils:数据库连接池
    django:一个WEB framework
    docutils:用来写文档的
    dpkt:数据包的解包和组包
    MySQLdb:连接MySQL数据库的
    py2exe:用来生成windows可执行文件
    Pylons:我们领导推荐的web framework
    pysqlite2:SQLite的连接组件
    pythonwin:Python的Windows扩展
    setuptools:无奈,PEAK的一套python包管理机制
    sqlalchemy:数据库连接池
    SQLObject:数据库连接池
    twisted:巨无霸的网络编程框架
    wxPython-2.6:因为需要保持与linux相同的版本才没用最新的,GUI编程框架
    pypcap:抓包的
    python-dnet:控制网络安全的其他设备
    pyevent:Python的事件支持
    pydot:画图的,graphiz
    sendpkt:Python发包
    simplejson:JSON的支持
    
    
    DPKT:raw-scoket网络编程
    Cx-oracle:连接oracle的好东东
    Mechanize:爬虫连接网站常用
    PIL:图像处理工具包
    
    
    reportlab for PDF 文件。
    PyQt4 for GUI界面
    
    feedparser: rss解析
    chardet:编码检测
    scons: 项目构建工具,写好了模板用起来还是很方便的
    scapy: 网络包构建分析框架,可编程的wireshark,有兴趣的google “Silver Needle in the Skype”
    pefile: windows pe文件解析器
    winpdb: 自己的程序或者用别的库不太明白的时候就靠它了
    pywmi: 省了好多折腾功夫
    pylint: 培养良好的编码习惯
    
    
    下面是准备用的,不做评论:
    
    pygccxml
    pyparsing
    pymacs
    idapython
    paimei
    pysvn
    pyLucene
    wikidpad
    
    
    1.Python操作PDF文件
    2.Python連接PostgreSQL引擎
    3.Python的.NET2.0的曲線表
    
    http://pybrary.net/
    —————————————————–
    1.Python Image Libary
    2.Tkinter Libary
    
    http://www.pythonware.com/index.htm
    —————————————————–
    1.Karrigell
    
    http://karrigell.sourceforge.net/
    —————————————————–
    1.The Standard Python Libary
    
    http://effbot.org/librarybook/
    —————————————————–
    1.Python操作PDF的Libary
    
    http://reportlab.org/
    —————————————————–
    1.Python cURL Libary
    
    http://pycurl.sourceforge.net/
    —————————————————–
    1.urlgrabber
    
    http://linux.duke.edu/projects/urlgrabber/
    —————————————————–
    1.MySQL-Python
    
    http://mysql-python.sourceforge.net/
    —————————————————–
    1.pyScripter
    2.Python for Delphi
    
    http://mmm-experts.com/Products.aspx
    —————————————————–
    1.Python Cookbook
    
    http://aspn.activestate.com/ASPN/Python/Cookbook/
    —————————————————–
    1.pygame
    
    http://www.pygame.org/news.html
    ——————————————————
    1.pymedia
    
    http://www.pymedia.org
    
    ——————————————————
    1.pydbf
    
    http://dbfpy.sourceforge.net/
    ——————————————————
    1.Pexpect
    
    http://pexpect.sourceforge.net/
    ——————————————————
    1.dabo
    desktop application frameworks
    
    http://dabodev.com/
    ——————————————————
    1.pySerial
    
    http://pyserial.sourceforge.net/
    http://wiki.woodpecker.org.cn/moin/PySerial
    ——————————————————
    1.python’s doc
    
    http://wiki.woodpecker.org.cn/moin/PythonZhDoc
    ——————————————————
    1.The Visual Module of VPython(3D)
    
    http://vpython.org/index.html
    ——————————————————
    1.PyLucene
    
    http://pylucene.osafoundation.org/
    ——————————————————
    1.DBUils
    
    http://www.webwareforpython.org/DBUtils
    ——————————————————
    1.http://happyiww.popo.blog.163.com/blog/
    
    a good people’s blog
    ——————————————————
    1.AppleScript,OSAComponents,HtmlCalendar
    
    http://freespace.virgin.net/hamish.sanderson/index.html
    ——————————————————
    1.Pypcap
    
    http://code.google.com/p/pypcap/
    http://monkey.org/~dugsong/dpkt/
    http://www.winpcap.org/install/default.htm
    
    
    python-memcached client module for memcached
    
    
    Kodos,python的正则表达式调试工具,
    
    
    
    PLY (Python Lex-Yacc)
    http://www.dabeaz.com/ply/
    正在用它写汇编器.
    
    wxPython - GUI Framework
    omniORBpy - CORBA Library
    ZODB3 - an OODB implement
    ReportLab - PDF generating solution
    numpy+scipy+matplotlib
    
    
    hachoir 各种多媒体元数据解析
    python-orge 演示用
    
    数据库的:
    sqlalchemy
    sqlobject
    MySQL-python (easy_install的时候就是这个名字)
    
    
    routes
    beaker
    
    两个模板引擎:
    genshi
    mako
    
    paster
    
    
    另外还有知道的
    PyCrypto 加密的
    paramiko 这个可以帮你写sftp,比twisted里面的那个好用一些
    
    twisted 写服务器玩的
    
    PyOpenGL
    
    PyGame
    
    Psyco 传说可以加速你的python程序
    
    PIL (Python Image Library) 处理图片
    
    PyObjC 这个是苹果上用的,写Cocoa程序
    
    
    Python desktop client GUI application:
    wxPython
    PyOpenGL
    PyXML
    ZSI
    davlib.py
    
    ChartDirector
    一个支持多种语言的图表库,无论是效果还是效率,都比jfreechart高。