Describes the concepts and Management of the Python programming package, and details of the python programming package
Packages in Python
A package is a hierarchical file directory structure that defines a Python application environment consisting of modules, sub-packages, and sub-packages.
Consider a pots. py file in the Phone directory. This file has the following source code:
#!/usr/bin/python# -*- coding: UTF-8 -*- def Pots(): print "I'm Pots Phone"
Similarly, we have two other files that save different functions:
- Phone/Isdn. py contains the function Isdn ()
- Phone/G3.py contains function G3 ()
Now, create file _ init _. py in the Phone directory:
Phone/__init__.py
To use all functions when you import Phone, you need to use an explicit Import Statement in _ init _. py, as shown below:
from Pots import Potsfrom Isdn import Isdnfrom G3 import G3
After you add the code to _ init _. py, all these classes are available when you import the Phone package.
#! /Usr/bin/python #-*-coding: UTF-8-*-# import the Phone package import Phone. Pots () Phone. Isdn () Phone. G3 ()
Output result of the above instance:
I'm Pots PhoneI'm 3G PhoneI'm ISDN Phone
For example, we place only one function in each file, but you can place many functions. You can also define Python classes in these files and create a package for these classes.
Python package management tool
Distrils is an enhancement to the standard library's disutils module. We know that disutils is mainly used for easier packaging and distribution of packages, especially for packages that are dependent on other packages.
Distribute is created because the Setuptools package is no longer maintained.
Install Distribute
You can install distribute_setup.py through the distribute_setup.py script, or install distribute_install, pip, or the source file. However, using distribute_setup.py is the simplest and most popular method
$ curl -0 http://python-distribute.org/distribute_setup.py$ sudo python distribute_setup.py
Pip is a tool for installing python packages. It provides the installation package to list installed packages, upgrade packages, and uninstall packages.
Pip is a replacement of easy_install and provides the same search Package function as easy_install. Therefore, you can use the package installed by easy_install or pip for installation.
Install Pip
You can use the source code package, easy_install, or script to install Pip.
The following describes various installation methods:
Source code method:
$ Wget http://pypi.python.org/packages/source/p/pip/pip-0.7.2.tar.gz (Replace with the latest package) $ tar xzf pip-0.7.2.tar.gz $ cd pip-0.7.2 $ python setup. py installeasy_install: $ easy_install pip
Get_pip.py script:
$ curl -0 https://raw.github.com/pypa/pip/master/contrib/get-pip.py$ sudo python get-pip.py
OK. Let's take a look at Pip usage.
Install package
$ pip install Markdown
List Installed packages
$ pip freeze
Install a package of a specific version
You can specify a version number by using ==,>=, <=, >,<.
$ pip install 'Markdown<2.0'$ pip install 'Markdown>2.0,<2.0.3'
Upgrade package
Upgrade the package to the latest version. You can use-U or -- upgrade
$ pip install -U Markdown
Uninstall package
$ pip uninstall Markdown
Query package
pip search "Markdown"
PS -- path of The py file after package installation:
/usr/local/lib/python2.7/dist-packages