In the previous blog post, we summarized the import package in Python and installed a complete line of the package. There is an interesting point of knowledge, there are many ways to install packages, modules and package management in the packaging, release, installation is also worth studying the content.
There are a number of ways to install packages in Python:
- SOURCE package: Python setup.py install
- Online Installation: Pip install package name (Linux)/Easy_install Package name (window)
Python packages are very common in development, and the general use of routines is to do a Python module package, package modules, and then publish, install and use. Packaging and installing packages is the most common work. Learn to encounter Distutils and setuptools two kinds of packaging tools, after learning to do notes records.
distutils
Distutils is part of the Python standard library, which is designed to provide developers with a convenient way to package, while providing users with a convenient way to install. After we have developed our own modules, we use Distutils's setup.py to package them.
First, complete the function python
hello.py
Def hello_fun (): print "I say hello to You"
Ii. establishment of the setup.py file
setup.py
From Distutils.core import setupsetup ( name= "Hello_module", version= "1.0", author= "LJK", author _email= "[Email protected]", py_modules=[' Hello '],)
Third, the implementation of PACKAGING commands
Python setup Sdist
Check the current directory to automatically generate a folder dist, the folder has a compressed package that is our target file. There is also a record file manifest.
Four, install the module
The hello_module-1.0.tar.gz is the generated Python module. Switch to the My Python virtual environment and install the module.
Install the module using the Python setup.py installation. As can be seen from the path, the module is installed into the standard library under the development path .
V. Use of modules
After installing the module, import the module in Python's interactive environment. The module is the hello.py file that references the Hello_fun () function in the hello.py file.
Setuptools
Setuptools is an enhanced version of Distutils. Setuptools has a entry_points feature that is convenient to start a service like Linux, such as Firefox in the Linux command line to start Firefox.
The command path has no effect until the first test is not installed.
First, create a feature Pack
Create a folder demo, create get_path.py and __init__.py two files in the folder. get_path.py is a function function and __init__.py is the identity file of the package.
get_path.py
Import os def fun (): print "I am in the path:" print OS.GETCWD ()
Second, configuration setup.py file
Create a setup.py file and fill in the necessary packaging information.
setup.py
#-*-encoding:utf-8-*-from setuptools import setupsetup ( name = "Demo", # Package name Version = "0.1", # version information
packages = [' demo '], # to package the project folder include_package_data=true, # automatically pack all the data in the folder Zip_safe=true, # Set the project package as secure without having to detect its security every time install_requires = [ # Install dependent other packages (test data) ' docutils>=0.3 ', ' requests ', ], # Set the entry for the program to path # after installation, the command line execution path is equivalent to calling the fun method in get_path.py entry_points={ ' Console_ Scripts ': [ ' path = Demo.get_path:fun ' ] },)
in the configuration of the module needs to write all the dependencies, the installation of the specified address to download. This simplifies the installation process when it is used, but is not good enough. The best way is to download the PIP automatically.
Three, packaging
Python setup.py sdist
After packaging, two more folders, namely Demo.egg-info and Dist. Demo.egg-info is the necessary installation information, and the dist in the package is the installation package.
View the files after dist/demo-0.1.tar.gz decompression.
Iv. installation Package
V. Use of the Package
After installation, enter path directly on the command line, and enter to see the function fun () in get_path.py called, and the output string.
You can also import and use it.
Advanced use of Setuptools
The above using Setuptools is simply a configuration file setup.py completed the package information. In a real-world development environment, multiple file mates are often used. Take the packaging of OpenStack as an example. The management tools for PBR are introduced in OpenStack .
PBR is an auxiliary tool for Setuptools, originally developed for OpenStack, based on D2to1. PBR reads and filters the contents of the Setup.cfg, and then provides the parsed data to setup.py as a parameter. SETUP.CFG provides default parameters for setup.py and is easy to modify. setup.py parse the Setup.cfg file first, and then execute the relevant command. Includes the following functions:1, get version,authors and changelog information from git 2, Sphinxautodoc. PBR scans project, finds all modules, and generates Stubfiles3, Requirements. To read the Requirements.txt file, generating the Setup function requires a dependency of package 4, long_description. Generate Long_description parameters from Readme.rst, README.txt, or Readmefile
pbr file is simple, as follows. After the configuration will automatically find the directory of the Setup.cfg file, parse the file parameters for setup.py use.
setup.py
From Setuptools import setupsetuptools.setup (setup_requires=[' PBR '],pbr=true)
Setup.cfg
[Metadata]name= keystoneversion= 2015.2summary= OpenStack identitydescription-file=readme.rstauthor= openstackauthor-email= [Email protected]home-page= http://www.openstack.org/classifier=Environment:: Openstackintendedaudience:: Information technologyintendedaudience:: System administratorslicense:: OSI Approved:: Apache software Licenseoperatingsystem:: POSIX:: linuxprogramminglanguage:: pythonprogramminglanguage:: Python:: 2Pro Gramminglanguage:: Python:: 2.7[files]packages=keystone[global]setup-hooks=pbr.hooks.setup_hook[egg_info]tag_ build=tag_date= 0tag_svn_revision= 0[build_sphinx]all_files= 1build-dir= doc/buildsource-dir= doc/source[compile_ catalog]directory= keystone/localedomain= Keystone
Specific parameters need to be further studied ...
Python packaging Tools distutils, Setuptools analysis