Packaging

  • pip等为packaging的前端;setuptools则为后端

  • 目前的规范为:工程的元数据都放在pyproject.toml文件

CLI

等价于pip install -e . (可编辑模型/开发模型:需要频繁地改动,本地的修改执行反应到安装的包上)
python setup.py develop
删除所有编译文件(build目录)
python setup.py clean --all
build
python -m build
install
pip install dist/...whl
pip install dist/...tar.gz

NOTE


Usage

简例

from setuptools import find_packages, setup
setup(
    name='包名',
    version='0.0.1',
    packages=['ros_numpy'],  # 指定要打包的包,或者由程序find_package()寻找
    author='Natsu_Akatsuki',
    description='...'
)
  • find_package返回的是module list

img

重写命令行

import shutil
from distutils.command.clean import clean
from pathlib import Path
from distutils.cmd import Command
from setuptools import find_packages, setup
import os

package_name = "am-utils"


class UninstallCommand(Command):
    description = "uninstall the package and remove the egg-info dir"
    user_options = []

    # This method must be implemented
    def initialize_options(self):
        pass

    # This method must be implemented
    def finalize_options(self):
        pass

    def run(self):
        os.system("pip uninstall -y " + package_name)
        dirs = list((Path('.').glob('*.egg-info')))
        if len(dirs) == 0:
            print('No egg-info files found. Nothing to remove.')
            return

        for egg_dir in dirs:
            shutil.rmtree(str(egg_dir.resolve()))
            print(f"Removing dist directory: {str(egg_dir)}")


class CleanCommand(clean):
    """
    Custom implementation of ``clean`` setuptools command."""

    def run(self):
        """After calling the super class implementation, this function removes
        the dist directory if it exists."""
        self.all = True  # --all by default when cleaning
        super().run()
        if Path('dist').exists():
            shutil.rmtree('dist')
            print("removing 'dist' (and everything under it)")
        else:
            print("'dist' does not exist -- can't clean it")


setup(
    name=package_name,
    packages=find_packages(),
    author='anomynous',
    cmdclass={'uninstall': UninstallCommand, # 重写命令行选项
              'clean': CleanCommand},
)

指定安装的依赖

setup(
 install_requires=[
    'rospkg==0.5.0'
        'numpy>=0.3.0',
        'setuptools>=1.0.0,<2.0.0'
 ]
)

添加pip包的url

指定package的搜索位置

Reference