Introduction of python setuptools module
Articles Catalogue
-
Module Introduction
Setuptools is an enhanced version of python distutils, making it easier for developers to build and publish python packages, especially when packages depend on other packages. Packages built and published with setuptools are similar to those published by distutils. Users of a package can use it without installing setuptools.
-
Configure setup file
from setuptools import setup, find_packages import sys, os import re import time with open('README.md', 'rb') as f: readme = f.read().decode('utf-8') install_requires = [] with open('requirements.txt', 'rb') as f: for req in f.readlines(): install_requires.append(req.strip()) setup( name='Personas', #Package name version='0.0.1', #Version information description='persons_tools', long_description=readme, author='xxx', author_email='xxx@xxx.com', install_requires=install_requires, # Install other packages that depend on packages= find_packages(exclude=['ez_setup', 'examples', 'tests']), #Project folders to be included (exclude, not packaged) package_data = { 'conf':'conf/*', 'service.personas_flask.models.incom':'service/personas_flask/models/incom/*', 'service.personas_flask.models.industry':'service/personas_flask/models/industry/*', 'service.personas_flask.models.profession':'service/personas_flask/models/profession/*', 'personas.labels_pool.conf':'personas/labels_pool/conf/*', }, scripts=["personas/personas_calculate.py"], zip_safe=False, # Setting the project package as unsafe requires testing its security every time include_package_data=True, # Automatic Packing of All Data in Folder )
-
Description of parameters
3.1 find_packages
The parameters of find_packages are: a source directory, an include package name directory, and an exclude package name directory. If these parameters are ignored, the source directory defaults to the directory where the setup.py script resides. This function returns a list that can be assigned to the package parameter.
Some projects may use SRC or lib directories as subdirectories of source trees, so in these projects, we need to use "src" or "lib" as the first parameter of find_packages(), of course, in this case we also need to set package_dir={":"lib"}, otherwise we will report errors.
from setuptools import setup, find_packages setup( name = "HelloWorld", version = "0.1", package_dir = {'':'lib'}, packages = find_packages('lib'), )
The find_packages() function traverses the target directory, filters it according to the include parameter, and finds the python package. For Python 3.2 and previous versions, only directories containing _init_ py files are considered packages. Finally, the results are filtered to remove the package matching exclude parameters.
The include and exclude parameters are a list of package names, and the "." in the package name represents the parent-child relationship.
lib/ foo.py __init__.py bar/ __init__.py bar.py
Then find_packages(exclude=["lib"]) (or packages = find_packages(include=["lib"]), just exclude (or include) lib packages, but not exclude (include) lib.bar packages.
3.2 package_data
Method 1)
setup { package_data = ['', ['*.txt']] }
The goal is to put the txt file in the package
Method 2)
from setuptools import setup, find_packages setup( ... package_data = { # Any package that contains *. txt or *.rst files can be added to handle a multi-tier package directory structure '': ['*.txt', '*.rst'], # If there is a *. msg file under the hello package, add it as well. 'hello': ['*.msg'], } )
Method 3: Automatic recognition, multi-layer package can be processed
from setuptools import setup, find_packages setup( ... include_package_data = True )
It searches all data files for all package s, which is a relatively easy way to do so.
Method 4:
The data is located in a subdirectory of the package, which can be set manually, such as in the data directory of mypkg.
from setuptools import setup, find_packages setup( ... packages = find_packages('src'), # include all packages under src package_dir = {'':'src'}, # tell distutils packages are under src package_data = { # If any package contains *.txt files, include them: '': ['*.txt'], # And include any *.dat files found in the 'data' subdirectory # of the 'mypkg' package, also: 'mypkg': ['data/*.dat'], } )
-
Installation and execution
- Installation: python setup.py install
- Execution: python setup.py sdist generates the compressed package of the project in the dist folder
- Execution: python setup.py bdist_wininst generates exe installation files