Python build tool setup. py method example, pythonsetup. py

Source: Internet
Author: User

Python build tool setup. py method example, pythonsetup. py

This article introduces the python build tool setup. py, which is described as follows:

1. application scenarios of setup. py

When installing python-related modules and libraries, we generally use "pip install Module name" or "python setup. "py install", the former is online installation, will install the relevant dependent package of this package; the latter is to download the source package and then install it locally, will not install the relevant dependent package of this package. Therefore, it is quite easy to use pip when installing a common python package. However, in the following scenarios, using python setup. py install is more suitable:

How does python package and release together with dependency packages when writing related systems?

If I develop a program on the local machine, I need to use the redis and mysql modules of python and the redis_run.py module compiled by myself. How can I publish the system on the server? How can I package the dependency module and the Self-compiled module redis_run.py to implement one-click installation? At the same time, install the self-compiled redis_run.py module in the exe file format to the python global execution path C: \ Python27 \ Scripts?

In this application scenario, the pip tool does not seem to be useful. You can only use the python build tool setup. this build tool can meet the requirements of the above application scenarios. in The py file, specify the dependent libraries and versions, and then use python setup on the target machine. install py install.

II. Introduction to setup. py

from setuptools import setup, find_packages  setup(  name = "test",  version = "1.0",  keywords = ("test", "xxx"),  description = "eds sdk",  long_description = "eds sdk for python",  license = "MIT Licence",   url = "http://test.com",  author = "test",  author_email = "test@gmail.com",   packages = find_packages(),  include_package_data = True,  platforms = "any",  install_requires = [],   scripts = [],  entry_points = {   'console_scripts': [    'test = test.help:main'   ]  } )

Parameters of setup. py are described as follows:

-- Name package name

-- Version (-V) package version

-- Author of the author Program

-- Author_email address of the author of the program

-- Maintainer

-- Maintainer_email address of the maintainer

-- Url program official website address

-- License application authorization information

-- Brief description of the description Program

-- Long_description detailed description of the program

-- List of software platforms applicable to the platforms Program

-- Category list of the classifiers Program

-- Keywords keyword list

-- Packages: directory of the packages to be processed (including the _ init _. py folder)

-- Py_modules: List of python files to be packaged

-- Download_url

-- Keep class

-- Data files, slices, and configuration files to be packaged during data_files Packaging

-- Scripts step list to be executed during installation

-- Package_dir tells setuptools which directories of files are mapped to which source package. For example, package_dir = {'': 'lib'} indicates that all modules in" root package "are in the lib directory.

-- Requires

-- Provides defines which modules can provide Dependencies

-- Find_packages () for a simple project, it is easy to manually add the packages parameter. We just used this function, which defaults to setup. in the same directory of py, search for each file containing _ init __. py package.

In fact, the package can be stored in a srcdirectory. In addition, the package can contain aaa.txt files and data folders. In addition, some specific packages can be excluded.

find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"])

-- Install_requires = ["requests"] dependent package to be installed

-- Entry_points: dynamically discovers services and plug-ins. The following describes in detail:

In the following entry_points: lele_scripts indicates the name of the command line tool; in "redis_run = RedisRun. in redis_run: main, the toolkit name is specified before the equal sign, and the content after the equal sign specifies the entry address of the program.

 entry_points={'console_scripts': [   'redis_run = RedisRun.redis_run:main', ]}

Here there can be multiple records, so that a project can create multiple command line tools, such:

setup( entry_points = {  'console_scripts': [   'foo = demo:test',   'bar = demo:test',  ]})

Iii. Sample Code of setup. py

#! /Usr/bin/env python # coding = utf-8from setuptools import setup ''' package the redis service into C: \ Python27 \ Scripts exe file ''' setup (name = "RedisRun", # name in pypi, pip or easy_install used for installation, or generate the egg file name version = "1.0", author = "Andreas Schroeder", author_email = "andreas@drqueue.org", description = ("This is a service of redis subscripe "), license = "GPLv3", keywords = "redis subscripe", url = "https://ssl.xxx.org/redmine/projects/RedisRun", packages = ['redisrun'], # list of directories to be packaged # install_requires = ['redis> = 2.10.5 ', 'setuptools> = 661',], # Add this option, generate an exe file in the scripts directory of Python in windows # Note: the module and function are separated by a colon: entry_points = {'console _ scripts': ['redis _ run = RedisRun. redis_run: main',]}, # long_description = read ('readme. md '), classifiers = [# program category list "Development Status: 3-Alpha", "Topic: Utilities", "License: OSI Approved :: GNU General Public License (GPL) ",], # this parameter is required. Otherwise, windows error zip_safe = False is reported when uninstalling the tool)

4. Modified project code (in this case, the RedisRun module is a sub-module of the DrQueue module, because it needs to import some public modules)

#! /Usr/bin/env python # coding = utf-8from setuptools import setup ''' package the redis service into C: \ Python27 \ Scripts exe file ''' setup (name = "RedisRun", # name in pypi, version = "1.0 ", author = "Andreas Schroeder", author_email = "andreas@drqueue.org", description = ("This is a service of redis subscripe"), license = "GPLv3", keywords = "redis subscripe ", url = "https://ssl.xxx.org/redmine/projects/RedisRun", packages = ['drqueue '], # list of directories to be packaged # dependencies to install install_requires = ['redis> = 2.10.5',], # Add this option to generate an exe file in the scripts directory of Python in windows # Note: the module and function are separated by a colon: entry_points = {'console _ scripts ': ['redis _ run = DrQueue. redisRun. redis_run: main',]}, # long_description = read ('readme. md '), classifiers = [# program category list "Development Status: 3-Alpha", "Topic: Utilities", "License: OSI Approved :: GNU General Public License (GPL) ",], # this parameter is required. Otherwise, windows error zip_safe = False is reported when uninstalling the tool)

The directory structure of the project is as follows:

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.