Python Package Management Tools Setuptools Detailed (Basic) __python

Source: Internet
Author: User
Tags mkdir
Original article: Click to open the link
0. What is setuptools

Setuptools is a collection of Python distutils enhancements that can help us create and distribute Python packages more simply, especially with dependencies. When a user creates a package using Setuptools, there is no need to install setuptools, as long as one boot module is available.

Function Highlights: Use Easyinstall to automatically find, download, install, upgrade the dependency package to create a Python Eggs contains the data files in the package directory automatically contains all the packages in the package directory, without having to enumerate all the relevant files in the setup.py that automatically contain and publish the package. Instead of creating a manifest.in file to automatically generate a packaged script or Windows execution file support Pyrex, you can list the. pyx file in setup.py, and end users can deploy development patterns without installing Pyrex support for uploading to PyPI. To enable the project to extend distutils with new commands or setup () parameters in Sys.path, for multiple project publish/reuse extensions to simply declare entry points in Project setup (), creating applications and frameworks that can automatically discover extensions

In short, setuptools is more usable than distutils, basically meet the installation and release of large projects 1. Install Setuptools

1 The simplest installation, assuming that under Ubuntu

sudo apt-get install Python-setuptools

2) Startup script installation

wget http://peak.telecommunity.com/dist/ez_setup.py
sudo python ez_setup.py
2. Create a simple package

With Setuptools, creating a package is basically a brain-free operation.

Cd/tmp 
mkdir demo
CD Demo

Create a setup.py file in the demo, write

From Setuptools Import setup, find_packages

setup (
    name = "Demo",
    Version = "0.1",
    packages = Find_ Packages (),
    )


You can pack a test package by executing the python setup.py bdist_egg.

Demo
| |   '--bdist.linux-x86_64 | |
demo.egg-info
|   | --Dependency_links.txt
|   | --Pkg-info
|   | --SOURCES.txt
|   '--top_level.txt
| | dist |   '--Demo-0.1-py2.7.egg
'--setup.py

The egg package is generated in the Dist

File Dist/demo-0.1-py2.7.egg
dist/demo-0.1-py2.7.egg:zip archive data, at least v2.0 to extract

Look at the generated. Egg file, which is a zip package, untie it and look first.

Upzip-l Dist/demo-0.1-py2.7.egg

Archive:  dist/demo-0.1-py2.7.egg
  Length      Date    time    Name
---------  ---------------   ----
        1  2013-06-07 22:03   egg-info/dependency_links.txt
        1  2013-06-07 22:03   egg-info/zip-safe  2013-06-07 22:03   egg-info/sources.txt
        1  2013-06-07 22:03   egg-info/top_level.txt
      176  2013-06-07   22:03 egg-info/pkg-info
---------                     -------
      299                     5 Files

As we can see, the inside is a series of automatically generated files. Now you can introduce the parameters in Setup () the name Package name version number packages contains other packages

To publish to PyPI, you need to add additional parameters, which you can refer to in the official documentation. 3. Add content to the package

There is no real content in the egg generated above, obviously no one can use, now we slightly palette, add a little content.

Execute mkdir Demo in demo, then create a directory, in this demo directory to create a __init__.py file, indicating that this directory is a package, and then write:

#!/usr/bin/env python

def Test ():
    print ' Hello world! '

if __name__ = = ' __main__ ':
    Test ()


The current home directory structure is:

Demo
| |   '--__init__.py
'--setup.py

After performing the Python setup.py Bdist_egg again, look at the egg package

Archive:  dist/demo-0.1-py2.7.egg
  Length      Date    time    Name
---------  ---------------   ----
        1  2013-06-07 22:23   egg-info/dependency_links.txt
        1  2013-06-07 22:23   Egg-info/zip-safe
      137  2013-06-07 22:23   egg-info/sources.txt
        5  2013-06-07   22:23 Egg-info/top_level.txt
      176  2013-06-07  22:23   egg-info/pkg-info 2013-06-07 demo/__init__.py
      338  2013-06-07 22:23   demo/__init__.pyc
---------                     -------
      753                     7 files

This back in the package more than the demo directory, obviously already have our own things, installation experience.

Python setup.py Install

This command will say that we created the egg installed into the Python dist-packages directory, where I am located in

Tree/usr/local/lib/python2.7/dist-packages/demo-0.1-py2.7.egg

Look at its structure:

/usr/local/lib/python2.7/dist-packages/demo-0.1-py2.7.egg |   | | --__init__.py
|   '--__init__.pyc
'--egg-info
    | dependency_links.txt
    | | pkg-info
    | SOURCES.txt
    | top_ Level.txt
    '--zip-safe

Open the Python terminal or Ipython, and import our package directly.

>>> Import Demo
>>> demo.test ()
Hello world!
>>>

Well, the execution was successful.

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.