Python disutils Create a bundle

Source: Internet
Author: User

The Distutils package in Python is primarily used to create shared packages, install packages, and use the following commands when you normally install a Python module:

Python setup.py Install

In fact, the above code is the function provided by the DISTUITLS package, directly using the setup.py to install a package, in this way after installing the package, you can directly in the system import this module.

The main point is to install the package into the Python class library, so that you can directly import, it is equivalent to the path of the module into the Python search module path.

This article is mainly used to describe how to use the Distutils module in Python to make an installation package.

1, several key parameters function

Packages: A list of strings consisting of a Python package that disutils needs to manipulate

Package_dir: The directory name corresponding to the package, a dictionary format

Scripts: A separate script file used to create and insert a list of string components

Data_files: Need to insert the data file, a list, mainly used to put some configuration files, in each record in the list is a tuple, (directory, file), the directory represents the place to be inserted, the file is expressed as what files need to be inserted into the directory, Data_ File name cannot be changed.


2, about why to use the package

In the packaging, there are two ways to use, one is to use the package, that is, we want to talk about the way, one is the use of modules, the general use of the module, it is best to have a module or a few modules in the same package.

When more than one module needs to be packaged, and multiple modules are in different packages, the package can be packaged in such a way that the packages is specified, and when the packages are used, there must be a file in the catalog for __init__.py , which means that this is a package in which the contents can be empty.

3. Create setup.py based on directory structure

The following is an example of the directory structure, as follows:

[Email protected] ~]# tree ansibleansible|--readme|--bin|   '--ansible|--lib|   '--ansible|       '--__init__.py|--library|   '--Kel '--setup.py4 directories, 5 files

The root directory is ansible, then there is a setup.py file in it, there is a package for lib/ansible/, in this package there is a specified init file, so this is a package, there is a script file for bin/ansible, in which the class library is not library/ Kel, so that you can write the contents of the setup.py file as follows:

From Distutils.core import setupsetup (        name= ' ansible ',        version= ' 1.0 ',        description= ' This is a test of the Disutils ',        packages=[' ansible '],        package_dir={' ansible ': ' lib/ansible '},        data_files=[('/usr/share/ ', [' Library/kel ']),],        scripts=[' bin/ansible '])
The above means: name indicates the name of this module, version represents a release, description is represented as a descriptive information, packages represents a package, Package_dir represents the directory for the package, Data_files represents the configuration file that needs to be copied, The above means copy the Kel file in the Library directory into the/usr/share directory, scripts executable script to ask Bin/ansible, remember basically is relative path, except to copy the place is/usr/share

The package commands are as follows:

[[email  protected] ansible-1.0]# python setup.py sdistrunning sdistrunning checkwarning:check:missing required Meta-data : Urlwarning:check:missing Meta-data:either (author and Author_email) or (maintainer and maintainer_email) must be supp Liedwarning:sdist:manifest template ' manifest.in ' does not exist (using default file list) Writing manifest file ' Manifes T ' creating ansible-1.0creating ansible-1.0/bincreating ansible-1.0/libcreating ansible-1.0/lib/ansiblecreating Ansible-1.0/librarymaking hard links in Ansible-1.0...hard linking README-Ansible-1.0hard linking setup.py-ans Ible-1.0hard linking bin/ansible, ansible-1.0/binhard linking lib/ansible/__init__.py ansible-1.0/lib/ Ansiblehard linking Library/kel-ansible-1.0/librarycreating distcreating tar archiveremoving ' ansible-1.0 ' (and Everything under it 
After packaging, the new folder name is dist in the current directory, in which a compressed package name is ansible-1.0.tar.gz, which means name plus the version number, unzip, and then install, as follows:

[[email protected] dist]# ls-ltotal 4-rw-r--r--1 root root 694 may 09:05 ansible-1.0.tar.gz[[email protecte D] dist]# tar-zxf ansible-1.0.tar.gz [[email protected] dist]# CD ansible-1.0[[email protected] ansible-1.0] # lspkg-info README bin Lib library setup.py[[email protected] ansible-1.0]# python setup.py installrunning Inst Allrunning buildrunning build_pycreating buildcreating build/libcreating build/lib/ansiblecopying lib/ansible/__init __.py, build/lib/ansiblerunning build_scriptscreating build/scripts-2.7copying and adjusting bin/ansible build/scripts-2.7changing mode of build/scripts-2.7/ansible from 644 to 755running install_librunning Install_ Scriptscopying build/scripts-2.7/ansible-/usr/local/python/binchanging mode of/usr/local/python/bin/ansible to 755running install_datarunning install_egg_inforemoving/usr/local/python/lib/python2.7/site-packages/ Ansible-1.0-py2.7.egg-infowriting/usr/local/python/lib/python2.7/site-packAges/ansible-1.0-py2.7.egg-info 

In the above information can be seen, the script file ansible has been copied to the/usr/local/python bin/ansible, this directory is mainly to see the location of Python installation, my Python is installed in/usr/local/python, The default copy is copied to the Python installation path.

When you can see the copy configuration file, you do not see the exact copy information, but in fact you have inserted the relevant class library configuration file, as follows:

[Email protected] other-2.0]# ls-l/usr/share/kel-rw-r--r--1 root root  9 09:23/usr/share/kel

The class library file is also inserted.


Summarize:

In Python using the Setup method in module Distutils,core to package, packaging can also be packaged in RPM format, can also be packaged as EXE Windows format, the specific view of official documents, the URL is:

Https://docs.python.org/2/distutils/index.html#distutils-index

When packaging, choose how to use the package or module, if you use the package, then there are several modules distributed in different packages, if the mode of using the module, then the module is in a package in the case.

The instructions executed at the time of packaging are Python setup.py sdist, when the package is installed, unzip, and then execute the python setup.py install

In the packaging, the main is the setup.py file, note that the path of the package is a relative path, the type of the parameters can be see the official document:

Https://docs.python.org/2/distutils/apiref.html#module-distutils.core






Python disutils Create a bundle

Related Article

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.