Python Packaging Distribution Tool Setuptools

Source: Internet
Author: User
Tags pack

As a Python standard packaging and distribution tool, Setuptools can be said to be quite simple to use. It will be installed on your machine along with Python. You just need to write a short setup.py installation file to pack your Python app

First, if you need to install setuptools separately, you can use the following command:

wget http://peak.telecommunity.com/dist/ez_setup.py

sudo python ez_setup.py

First installation file

Next let's write the installation file, assuming our project name is Setup-demo, the package name is MyApp, and the directory structure is as follows:

setup-demo/  ├setup.py         #  installation file  └myapp/           #  source code      __init__ . py           ...

One of the most basic setup.py files is as follows:

# Coding:utf8  from Import Setup Setup (    name='MyApp',         #  application name    version= ' 1.0 ',        #  version number    packages=['myapp']    # python package included in the installation package )
Execute the installation file

With the setup.py file above, we can hit a variety of packages or install the app in a native Python environment.

Create Egg pack

Python setup.py Bdist_egg

This command creates an egg file in the "dist" directory under the current directory, named "Myapp-1.0-py2.7.egg". The file name format is the "app name-version number-python version. Egg", my native Python version is 2.7. You will also notice that the current directory has more "build" and "Myapp.egg-info" subdirectories.

Similar to the previous example, only the file type created is tar.gz and the file name is "myapp-1.0.tar.gz".

Install the App

Python setup.py Install

This command installs the current Python app into the "site-packages" directory of the current Python environment, so that other programs can import the app's code like a standard library.

Development mode Installation

Python setup.py Develop

If the application changes frequently during the development process, it is cumbersome to remove the original version before each installation. With the "Develop" development method installed, the application code will not really be copied to the local python environment "site-packages" directory, but in the "site-packages" directory to create a link to the current application location. So if the current location of the source code is changed, it will be immediately reflected in the "site-packages".

Introducing non-Python files

In the example above, we will only package the source code under the "MyApp" package, if we want to also package other non-Python files, such as static files (js,css, pictures), what should be done? At this point we will add a "manifest.in" folder under the project directory. Suppose we put all the static files under the "static" subdirectory, now the project structure is as follows:

setup-demo/  ├setup.py         #  installation file  ├manifest.  in      #  manifest file  └myapp/           #  source code      ├static/      #  static file directory          __init__.py           ...

In the manifest file "manifest.in", we list the directory paths that you want to introduce within the package:

Recursive-include myapp/static *recursive-include myapp/xxx *

"Recursive-include" indicates the inclusion of subdirectories. One more thing to do is to set the "Include_package_data" parameter to True in "setup.py":

#Coding:utf8 fromSetuptoolsImportSetup Setup (name='MyApp',#Application Nameversion='1.0',#Version numberpackages=['MyApp'],#python packages included in the installation packageInclude_package_data=true#Enable manifest file manifest.in)

After packaging or installing again, all files in the "myapp/static" directory will be included. If you want to exclude a subset of files, you can use the "exclude_package_date" parameter in setup.py, such as:

Setup (    ..... Include_package_data=true,    #  Enable manifest file manifest.in    exclude_package_date={' : ['. Gitignore']})

The code above excludes all ". Gitignore" files from the package. If the above "Exclude_package_date" object property is not empty, such as "{' MyApp ': ['. Gitignore ']}", it means that only the ". Gitignore" File under the "MyApp" package is excluded.

Automatic installation dependencies

Our app relies on a third-party Python package, although it's possible to ask the user to install the dependency package in the documentation, but after all it's a hassle and the user may have the wrong version. In fact, we can specify a dependency package in the setup.py file, and then when you use Setuptools to install the app, the corresponding version of the dependent package is automatically installed. Let's modify the setup.py file in the previous example to include the "install_requires" parameter:

#Coding:utf8 fromSetuptoolsImportSetup Setup (name='MyApp',#Application Nameversion='1.0',#Version numberpackages=['MyApp'],#python packages included in the installation packageInclude_package_data=true,#Enable manifest file manifest.inexclude_package_date={"':['. Gitignore']}, Install_requires=[#Dependency List        'flask>=0.10',        'flask-sqlalchemy>=1.5,<=2.1'    ])

In the above code, we declare that the application relies on flask 0.10 and later, and Flask-sqlalchemy 1.5 and above, 2.1 and below. Setuptools will first check if there are any dependent packages locally, and if not, you will get an up-to-date package installed locally from PyPI that meets the requirements.

Perform the next test, you will find not only flask 0.10.1 (currently the latest version) is automatically installed, even flask of the dependency package Jinja2 and Werkzeug is automatically installed

If the application depends on a package that cannot be obtained from PyPI, we need to specify its download path:

Setup (    ..... Install_requires=[    #  dependency list        'flask>=0.10',         ' flask-sqlalchemy>=1.5,<=2.1 '     ],    dependency_links=[    #  dependent package download path        '/http/ example.com/dependency.tar.gz'    ])

The path should point to an egg or tar.gz package, or a page containing (an egg package or tar.gz package). Personal advice points directly to the file.

Automatically search Python packages

Previously we specified "packages=[' MyApp '" in setup.py to package the source code under the Python package "MyApp". If our application is large, Python packs a lot of what to do. You see this parameter is a list, we can certainly put all the source packages are listed inside, but certainly a lot of people think this is silly. Indeed, Setuptools provides a "find_packages ()" method to automatically search for Python packages that can be introduced:

#Coding:utf8 fromSetuptoolsImportSetup, find_packages Setup (name='MyApp',#Application Nameversion='1.0',#Version numberPackages=find_packages (),#python packages included in the installation packageInclude_package_data=true,#Enable manifest file manifest.inexclude_package_date={"':['. Gitignore']}, Install_requires=[#Dependency List        'flask>=0.10',        'flask-sqlalchemy>=1.5,<=2.1'    ])

In this way, all Python packages in the current project are automatically searched and introduced into the package. The "Find_packages ()" Method can qualify the path you are searching for, such as using "find_packages (' src ')" to indicate that all python packages are searched only in the "src" subdirectory.

Add
    • Zip_safe parameters

Determines whether the app is installed in the current Python environment as a zip-compressed egg file or as an. Egg-terminated directory installed in the current environment. Because some tools do not support zip files, and the compressed package is not easy to debug, it is recommended to set it to False: "Zip_safe=false".

    • Description information

Some of the parameters provide more details about the current application and have no effect on the package installation, such as:

Setup (... author="Billy He", Author_email="[email protected]", Description="This was a sample package", License="MIT", Keywords="Hello World Example", the URL="http://example.com/HelloWorld/",#Project Homelong_description=__doc__,#get a document comment from your code)

Python Packaging Distribution Tool Setuptools

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.