Python module, python module download
1. module definition
As the program code is written more and more, the code in a file will grow and become increasingly difficult to maintain. In order to write maintainable code, we divide many function groups into different files, so that each file contains less code, many programming languages use this method to organize code. In Python, A. py file is called a module. The name of the abc. py module is changed to mycompany. abc.
2. _ init _. py
Each package directory contains a _ init _. py file, which must exist. Otherwise, Python treats this directory as a normal directory rather than a package. _ Init _. py can be an empty file or Python code, because _ init _. py itself is a module and Its Module name is mycompany.
3. Variable Fields
The default naming rules for function domains in python are as follows: normal function and variable names are public and can be directly referenced, variables like _ xxx _ are special variables and can be directly referenced, but they have special purposes. functions or variables such as _ xxx and _ xxx are private and should not be directly referenced. python has no restrictions and cannot reference private.
4. Module Installation
In Python, third-party modules are installed through the package management tool pip.
Generally, third-party libraries are registered on the official Python pypi.python.org website.
The command to install Pillow is:
pip install Pillow
5. Search Path of the reference module
When we try to load a module, Python will search for the corresponding. py file in the specified path. If it cannot be found, an error will be reported.
By default, the Python interpreter searches for the current directory, all installed built-in modules, and third-party modules. The search path is stored in the path variable of the sys module.
If you want to add your own search directory, there are two methods:
First, directly modify sys. path and add the directory to be searched:
>>> import sys>>> sys.path.append('/Users/michael/my_py_scripts')
The second method is to set the environment variable PYTHONPATH. The content of this environment variable is automatically added to the module search path.
6. _ init _. py file usage
(1) import other packages or modules.
[Python] # It seems that you do not need to write import readers import writers import commands import users import meta import auth import admin
In this way, when we import this package, the __init _. py file runs automatically. After importing so many modules, we do not need to write all the import statements in one file, which can reduce the amount of code.
In a package, there are foo. py, _ init _. py
#__init__.pyimport osimport datetime
#foo.pyfrom __init__ import *print datetime.datetime.now()
(2) _ all __
Another important variable in _ init _. py is called _ all __. We sometimes make a trick to "import all", that is:
From PackageName import *
In this case, import the sub-modules and sub-packages registered in the _ init _. py file _ all _ list to the current scope.
For example, _ all _ = ["Module1", "Module2", "subPackage1", "subPackage2"]
(3) Add the mian function and execute it when called as a module.
def main(): print('__init__.main()')
(4) During package introduction, __init __. py runs. Therefore, if some variables or methods need to be resident in the memory, you can write them into _ init __. in The py file
import PackageName
7 pip Common commands
Usage: pip <command> [options] Commands: install the installation package. uninstall the package. freeze outputs the list of installed packages in a certain format to list the installed packages. show Displays the package details. search package, similar to search in yum. wheel Build wheels from your requirements. zip is not recommended. zip individual packages. unzip is not recommended. unzip individual packages. bundle is not recommended. create pybundles. help current help. general Options:-h, -- help displays help. -v, -- verbose more output, you can use up to 3-V, -- version real version information and then exit. -q, -- quiet has the least output. -- log-file <path> overwrites verbose error logs. default file:/root /. pip/pip. log -- log <path> does not overwrite the logs output by verbose. -- proxy <proxy> Specify a proxy in the form [user: passwd @] proxy. server: port. -- timeout <sec> connection timeout (15 seconds by default ). -- exists-action <action> Default action when a path already exists: (s) witch, (I) gnore, (w) ipe, (B) ackup. -- cert <path> certificate.
# Pip show -- files somepackage pip check which files are contained in the installed package # pip list -- outdated pip check which packages need to be updated
8 pip timeout Solution
Timeout Errors often occur when pip installation package is used. You can download the corresponding installation package from an image.
For example, Tsinghua University image address: http://e.pypi.python.org
Run the following command to install
pip install flask -i http://e.pypi.python.org/simplepip install flask -i http://pypi.v2ex.com/simple
You can also modify the timeout and image addresses in the configuration file.
[global]timeout = 6000index-url = http://e.pypi.python.org/simple[install]use-mirrors = truemirrors = http://e.pypi.python.org
In unix and macos, the configuration file is $ HOME/. pip/pip. conf.
On windows, the configuration file is % HOME % \ pip. ini.