packages in Python
A package is a hierarchical file directory structure that defines a Python application environment consisting of a module and a sub-package, and a sub-package under a sub-package.
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"
In the same way, we have two other files that hold different functions:
- phone/isdn.py contains function Isdn ()
- phone/g3.py contains function G3 ()
Now, create the file __init__.py in the phone directory:
phone/__init__.py
When you import the phone, in order to be able to use all the functions, you need to use explicit import statements in __init__.py, as follows:
From Pots import potsfrom Isdn import isdnfrom G3 import G3
When you add the code to __init__.py, the classes are all available when you import the phone package.
#!/usr/bin/python#-*-coding:utf-8-*-# import the phone package import phone phone.pots () phone.isdn () phone.g3 ()
The result of the above example output:
I ' m Pots phonei ' m 3G phonei ' m ISDN Phone
As above, for example, we only put a function in each file, but in fact you can put a lot of functions. You can also define Python classes in these files, and then build a package for those classes.
Python's Package management tool
Distribute is an enhancement to the standard library Disutils module, and we know that disutils is primarily used to package and distribute packages more easily, especially for packages that have dependencies on other packages.
Distribute was created because the Setuptools package is no longer maintained.
Installing distribute
Can be installed through the distribute_setup.py script distribute can also be installed through Easy_install, Pip, source files, but the use of distribute_setup.py to install is the simplest and popular way
$ curl-0 http://python-distribute.org/distribute_setup.py$ sudo python distribute_setup.py
Pip is a tool for installing the Python package, providing an installation package that lists the packages that have been installed, the upgrade package, and the ability to uninstall the package.
Pip is a replacement for Easy_install, which provides the same functionality for finding packages as Easy_install, so packages that can be installed using Easy_install can also be installed using PIP.
Install PIP
PIP installation can be via source code package, Easy_install or script.
Here are the various installation methods:
Source Code mode:
$ wget http://pypi.python.org/packages/source/p/pip/pip-0.7.2.tar.gz (replace with 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, here's a look at the use of PIP
Install the Package
$ pip Install Markdown
List the installed packages
$ pip Freeze
Install a specific version of the package
by using = =, >=, <=,;<>
$ pip Install ' markdown<2.0 ' $ pip install ' markdown>2.0,<2.0.3 '
Upgrade Package
Upgrade package to current latest version, can use-u or--upgrade
$ pip install-u Markdown
Uninstalling packages
$ pip Uninstall Markdown
Query Package
Pip Search "Markdown"
PS--Package after installation of the Py file path:
/usr/local/lib/python2.7/dist-packages