Python3 Modules and Packages

Source: Internet
Author: User

I. Modules and packages (package)

1. Module: A file that contains all of the functions and variables you define, followed by a. Py, a. py file is a module

2. Package: must contain the folder of the __init__.py module, usually also contains some other modules and sub-packages

3. Library (LIB): A library is a collection of code that accomplishes a certain function, whether it can be a package or a module

4. Framework: Supporting structures designed to solve an open problem with certain constraints

Some libraries are built into the 5.python, and there are some libraries that others have made themselves, called third-party libraries.

6. In general, third-party libraries are placed in .../python3/lib/site_packages

Two. Download and installation of third-party libraries

1. Installing a third-party library is essentially downloading and using code written by someone else

2. Common third-party library format: source (compressed package, need to extract first, get contains setup.py folder); Egg (essentially a compressed package); WHL (essentially a compressed file)

3. Installation Method:

(1) Source installation (local installation): Manual download, and then install to the local;

(2) Package management installation (Remote Installation): Automate the installation of Management Packs and modules for users with commands

4. Source Code Installation

(1) to the corresponding library hosting site download required files, https://pypi.org/or https://www.lfd.uci.edu/~gohlke/pythonlibs/, etc.

(2) Download the file to correspond to their own version of Python, but also choose 32-bit or 64-bit Python-installed system

(3) Open command line (win+r cmd): Switch to the directory where the downloaded files are located cd/d

(4) For files with setup.py (source file), enter command: Python setup.py install

Note: If you do not have a setuptools package, you need to manually download the installation, and then use Setuptools to install other packages that use Setuptools packaging

(5) for. Egg files using Easy_stall installation, enter command: Easy_install xxx.egg (full file name)

(6) For WHL files can be used easy_stall such as installed on, you can also use PIP installation, input command: Pip install XXX.WHL (full file name)

5. To install Easy_install and PIP remotely, you need to know the name of the library to install

6.easy_install:http://peak.telecommunity.com/devcenter/easyinstall

(1) is Setuptools's own installation script for installing the. Egg file

(2) switch installation in multiple Python versions: installed in 3.6python: easy_install-3.6 xxx

(3) Install the specified version package: Easy_install "library name Qualifier (<,>,<=,>=,==) version"; Example: Easy_install "request >1.0,<2.0" installation greater than 1.0 and less than 2.0 of the version

(4) Upgrade three-party package: Easy_install--upgrade (-u) library name, for example: Easy_install--upgrade requests

(5) Uninstall the three-party package: (1) Manually uninstall delete the corresponding package record or delete the package record in Easy-install.pth (2) easy_install-m package Name: Automatically delete package records in Easy-install.pth

(6)-M true meaning: Support multiple versions, can be switched at runtime, using: >>>import pkg_resources >>>pkg resources.require ("corresponding version of the package name") > >import the corresponding version of the package

(7) Easy_install.pth file: Records the modules that are currently installed through Easy_install (multiple versions of the module, only the last one installed); path retrieval when importing modules

(8) Switch the three-party installation source: In the Easy_install modified file setuptools\command\easy_install.py file search pypi.python.org find the corresponding address and modify

7.pip:http://pip.pypa.io/en/stable/

(1) is the installation script in the PIP library for installing the. WHL file

(2) Switch the installation package installation Source:

One-time modification: Pip install--index-url https://..../package name (Specify search) or pip Install-extra-index-url https://..../package name (extended search)

Permanent modification

(3) installed in a different Python version environment (1) Python m pip install xxx or python3 m pip install xxx (2) py-2 m pip install xxx or py-3 m pip install xx X

(4) View package: Already installed package: PIP list; not dependent package: Pip list--not-required; Expired package: Pip list--outdated; specific information for a package: Pip show xxx

(5) Search package: Pip search xxx or pip search-i retrieve address xxx

(6) Install the specified version package: Pip Install "library name Qualifier (<,>,<=,>=,==) version"; Example: Pip install "Request >1.0,<2.0" installs a version greater than 1.0 and less than 2.0

(7) Upgrade three-party package: Pip install--upgrade (-u) library name, for example: Pip install--upgrade requests Note: Pip upgrade is to first uninstall the current version, in the download and install the latest version, if the library exists, Directly executing the install command does not update the library

(8) Uninstall the three-party package: Pip Uninstall xxx

(9) Generate the text of the freeze requirement: Store the currently installed three-party package record in the specified file: Pip freeze > C:\Users\11373\Desktop\bao.txt Generate a text document of the storage package information

Pip install-r bao.txt Download and install the package in the text document

8.pycharm Installation: Settings---Project---project interpreter---Interface to the right of the package under different circumstances, right action: + Add-delete | update----corresponding PACKAGE install Package----Manage Repositories (link address for Management library)

9. Summarize the installation of a library:

(1) Search and installation in Pycharm

(2) pip install xxx

(3) https://www.lfd.uci.edu/~gohlke/pythonlibs/download the corresponding library to local, pip install XXX.WHL

Three. Import modules and Packages

1. Import using (from a) import B (as C): Imports from a B to the current position, using alias C.

(1) From package import single or multiple modules

(2) from Module import module resources (such as: functions, attributes, etc.)

(3) To ensure that the principle, that can only import their own next level, can not import the module from the package of resources or import packets from the package module

(4) Import * indicates all resources that the import can match to, stored in __all__=[] (each element in the list is a string)

(5) Import and From...import ... There is no more memory, the module object is created, the difference is which part of the content to the current location to use

(6) When importing packages, if you write only the package name, the default is not to import all modules, you should import the required modules in the Init module again or in From...import ... The form of import

2. When importing, you need to put the command at the top of the script

3. The import of the same library will only be executed once, no matter how many import you have. This will prevent the library from being executed over and over.

4. When the module is imported, the module is automatically executed and the corresponding init.py file is automatically executed when the package is imported.

5. When an import statement is executed, it is automatically searched to determine if the package or module exists

(1) The lookup path order is the module that is contained in the Sys.path path (the environment variable of the import module), which is already loaded in memory, the built-in module

(2) When the interpreter encounters an import statement, the module is imported if it is in the current search path

(3) When installing third-party modules, if they are not installed in a standard manner, the installation paths of these modules must be added to Sys.path in order to be able to reference (import) These modules

(4) If the package or module is placed in the Sys.path path, it can be used by import

6. First-time and second-time import

(1) First import: Executes all the code in its current namespace, creates a module object, binds all top-level variables in the module to the module object as attributes, and in the import position, introduces the variable name after import to the current namespace, and the second time Directly perform the last step above

Search by module to retrieve the path sequence: 1. Built-in Module 2.sys.path 3. Append the path yourself

(2) The way to append the path: 1.sys.path.append () Only this time add the path; 2. Modify the environment variables, only valid in the shell; 3. Add the. pth file

1 Import site2  3 print (Site.getsitepackages ()) #查找特殊路径 that can be used to store the. pth file 4 #在 Add a path to the. PTH profile and the system automatically recognizes
. PTH

(3) Modify path in Pycharm: Settings--project interpreters (* Show All)--click the last logo on the right side of the Project interpreters screen show Path---Add the paths in the Interpreter paths interface by clicking the plus sign

(4) Sys.path priority: The path that is configured in the current directory---the python environment variable----The installation path of Pthon----------lib Library (lib\\site-packages) for the. pth file under the installation path---- The. pth file that is configured in the LIB library---pycharm the path we added

(5) Second import: From the module that has been loaded, look for the loaded module: sys.modules

7. Common Scenarios for importing modules: Partial import, overwrite import (after path overwrite path in sys, built-in override custom), cyclic import (a import b,b import a), optional import (two packages with similar features, preference for one import try according to demand). Except ... ), in-Package import (out-of-package: absolute Import from a import B, inside Package: relative import from). /. InPort b)

8. When we try to use an interpreter to execute a PY file, he determines the directory in which the current file resides, and then adds the directory to the Sys.path. After the addition, and then back, basically this sys.path inside the content has been determined

8. Importing modules and Importing packages

1 #目录: 2 # import modules and Packages---3 # | 4 # init module for parent package, parent module, import module and package-----5 # | 6 # Peer Pack, sibling module, init module for parent package, test module--------7 # | 8 # Sub-package, downlevel module, the init module of sibling package-----9 # |10 #最下级模块, sub-package init module One by one #以test模块为执行模块导入相应的模块13 #导入同级模块14 import sibling module # directly into a single module and execute the module 1 5 Print (sibling module. Name) #打印模块中的name属性16 print (' * ' *30) #导入下级模块19 # import downlevel Module #不能直接导入同级包下面的模块No module named ' downlevel module ', Print (downlevel module. Name) #打印模块中的name属性21 #修改方法: Import Level pack. The Subordinate module # imports the sub-modules of the sibling package, executes the package's __init__ module and the imported module, print (Sibling package. Downlevel module. Name) #打印该模块的name属性24 print (' * ' *30) #导入下下级模块27 # import sub-package. The most subordinate module #no module named ' Subordinate Package ' "* * print (Subordinate package." Sub-module. Name) # import Peer Package. Sub-module #no modules na Med ' Sibling package. Sub-module ' # Print (sibling package. Name) to #修改: Import Peer Package. Sub- package. The subordinate module, print (sibling package. Subordinate package. Sub-module.name) print (' * ' *30) #导入上级模块37 # import Superior Module # cannot directly import the module above the sibling package no module named ' Superior module ' ' # print ' (downlevel module. name) #打印模块中的name属性39 #修改方法40 im Port imports modules and packages. Advanced Module # import the parent module under the superior package, execute the __init__ module of the package and the imported module print(import modules and packages.) The advanced module. Name)------------------------------   The init module of the ******************************46 sibling module, which performs the sub-module 49, is executed at the same level.   The init module of the sub-package executes the most subordinate module, the most subordinate module 53 ******************************54 Import module and package execute Superior Module 56 Superior Module              
Import Module
1 #导入包 2 Import Sibling Package #导入同级包直接导入 3 4 # Print (Sibling package. Name) #module ' sibling package ' has no attribute ' downlevel module ' 5 # Modify: To use a module under the package, you can The appropriate modules are imported into the IT module 6 7 # Import Parent Package #no module named ' parent package ' 8 # import downlevel Package No module named ' downlevel Package ' 9 #为什么找不到对应的包: Because it is found in the built-in modules and Sys.path , in the corresponding path does not have this package 10 # Solution: ImportSYS12 Print(Sys.path) sys.path.append (' e:\\python_work\\ import modules and packages ') sys.path.append (R ' E:\python_work\ import module and package \ Parent package \ Sibling Package ' ) import  Parent Package Import  sub-package, import  import modules and packages---------------------------------------------------------- the init module of the sibling package [' E:\\python_work\ \ import modules and packages \ \ Parent package ', ' e:\\python_work ', ' e:\\python3.6.4\\python36.zip ', ' e:\\python3.6.4\\dlls ', ' e:\\python3.6.4\\lib ' , ' e:\\python3.6.4 ', ' c:\\users\\11373\\appdata\\roaming\\python\\python36\\site-packages ', ' E:\\python3.6.4\\lib \\site-packages ', ' E:\\python3.6.4\\lib\\site-packages\\requests-2.18.4-py3.6.egg ', ' e:\\python3.6.4\\lib\\ Site-packages\\pymongo-3.6.1-py3.6-win32.egg ', ' E:\\python3.6.4\\lib\\site-packages\\easygui-0.98.1-py3.6.egg ', ' E:\\python3.6.4\\lib\\site-packages\\jedi-0.12.0-py3.6.egg ', ' e:\\python3.6.4\\lib\\site-packages\\ Parso-0.2.0-py3.6.egg ', ' e:\\pycharm\\pycharm 2017.3.3\\helpers\\pycharm_matplotlib_backend ' ]23  Init module for Parent package (init module of  downlevel package) 25 import modules and packages         
Importing Packages

Four. SYS module (module related to system function)

1.sys.path a directory listing for Python to find third-party extensions

1 Import sys 2 #sys. Path gets a collection of strings for the specified module search path, you can put the written module under a given path, you can correctly find the 3 print(Sys.path) 4 in the program import----------- --------------------------------------------------------------------5 [' e:\\python_work ', ' e:\\python_work ', 6  ' E:\\python3.6.4\\python36.zip ', 7  ' E:\\python3.6.4\\dlls ', 8  ' E:\\python3.6.4\\lib ', 9 ' e:\\python3.6.4 ', ten ' e:\\python3.6.4\\lib\\site-packages ', one ' E:\\pycharm\\pycharm 2017.3.3\\helpers\\ Pycharm_matplotlib_backend ']       
Sys.path

2.sys.argv

1 Import sys 2 #在外部向程序内部传递参数sys. argv 3 print (sys.argv[0]) # Get script name 4 print(SYS.ARGV) 5  6 for I in sys.argv: 7     Print (i)       # get parameter list 8  9 print (len (SYS.ARGV)-1) # Number of statistical parameters------------------------------------e:/ python_work/python.3.11. py12 [' e:/python_work/python.3.11.py ']13 e:/python_work/python.3.11. PY14 0 
SYS.ARGV

Five. __name__ Properties

When the 1.a module is introduced by another program for the first time, the A module main program will run

2. Use the __name__ property to make the block execute only when the module itself is running, not when it is referenced

3. Each module has a __name__ property, when its value is ' __main__ ', indicates that the module itself is running, otherwise it is introduced

4. A py file is executed directly in the form of a script, whose name is __main__; if it is loaded in the form of a module, his name is determined by the path of the load: Package name. module name top-level name

1 def Dayin (): 2     print (' Dayingg 'dayin () 4 if __name__ = = ' __main__ ': 5    print (' program itself is running ') 6 Else
        
         : 7 print (' I came from another module '
         DAYINGG10 program itself is running 
           
dayin.py
1 Import dayin2  dayingg4 I came from another module 
in another file

Six. Dir () function

1. the built-in function dir() can find all the names defined within the module. Returns as a list of strings

2. If no parameters are given, then the Dir () function lists all the names currently defined

1 A = [1, 2, 3, 4, 5] 2 import dayin 3 print (dir (dayin)) # Get a name defined in the specified Module 4 print (dir ())     # Get a list of properties defined in the current module 5 c =5 # Create a new variable ' a ' 6 print(dir ()) 7 del C # delete variable name a 8 print I came from another module [' __builtins__ ', ' __cached__ ', ' __doc__ ', ' __fi le__ ', ' __loader__ ', ' __name__ ', ' __package__ ', ' __spec__ ', ' a ', ' Dayin ']13 [' __annotations__ ', ' __builtins__ ', ' __ Cached__ ', ' __doc__ ', ' __file__ ', ' __loader__ ', ' __name__ ', ' __package__ ', ' __spec__ ', ' a ', ' Dayin ']14 [' __ Annotations__ ', ' __builtins__ ', ' __cached__ ', ' __doc__ ', ' __file__ ', ' __loader__ ', ' __name__ ', ' __package__ ', ' __spec_ ' _ ', ' A ', ' C ', ' Dayin ']15 [' __annotations__ ', ' __builtins__ ', ' __cached__ ', ' __doc__ ', ' __file__ ', ' __loader__ ', ' __ name__ ', ' __package__ ', ' __spec__ ', ' a ', ' Dayin ']      
dir ()

Seven. Supplement

1. Three-party package version rules N1.n2.n3

(1) N1: modified the previous function or added a new feature (modified the previous API) n1+1

(2) N2: Added a small function n2+1

(3) N3: When the version of the bug is fixed n3+1

2. Publish a package: https://python_packaging.readthedocs.io/en/latest/minimal.html

(1) Registered account PyPI

(2) Environment preparation

(3) Pre-release preparation

Create a project and compile the build release package;

Specific setup.py Script documentation description: https://docs.python.org/2/distutils/setupscript.html and https://packaging.python.org/tutorials/ distributing-packages/

(packaged) command line execution setup.py: Switch the directory where the package resides------python setup.py sdist (automatically generate dist files in the package with compressed files)

Readme.rst:http:\\zh-sphinx-doc.readthedocs.io/en/latest/contents.html

 

license.txt:https://choosealicense.com/

Manifest.in:https://docs.python.org/3/distutils/sourcedist.html#specifying-the-files-to-distribute

Binary release Package: Unzip the direct copy to the specified directory

installation files under Windows directly double-click to perform the installation

Upload to PyPI: command-line operation: 1. Switch target directory, 2.twine upload file to upload

Python3 Modules and Packages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.