Python setuptools 使用

Python setuptools 使用

参考文档:

https://blog.csdn.net/gongdiwudu/article/details/118726517

创建测试目录和文件

创建文件夹

# 创建文件加
mkdir demo
cd demo

创建所需文件

touch __init__.py
touch setup.py

编辑 setup.py

编写 setup.py 文件

from setuptools import setup, find_packages

setup(
    name="demo",  		# 打包后的包名 
    version="1.1.1",    # 版本名称
    package=find_packages(),  # 自动寻找当前目录的项目包(项目包含 __init__.py 文件)
)

在当前目录下执行

python3 setup.py bdist_egg

说明: 用来创建一个二进制分发版本,经常用来替代基于bdist生成的模式

当前目录结构

── demo
    ├── build
    │   └── bdist.linux-x86_64
    ├── data_socket
    │   ├── data_socket
    │   │   ├── __init__.py
    │   │   ├── settings.py
    │   │   ├── urls.py
    │   │   └── wsgi.py
    │   └── manage.py
    ├── demo.egg-info
    │   ├── dependency_links.txt
    │   ├── PKG-INFO
    │   ├── SOURCES.txt
    │   └── top_level.txt
    ├── dist
    │   └── demo-1.1.1-py3.8.egg
    ├── __init__.py
    └── setup.py

再一个包文件目录

说明: 这个目录是存放我们打包后可以导入的代码 或者说 是我们的项目代码。

mkdir demo
touch __init__.py

创建一个py文件

# vim start.py

class Start:

    def __init__(self):
        self.host = "127.0.0.1"
        self.port = 6789

    def get_host(self):
        return self.host

    def get_post(self):
        return self.port

当前项目结构

── demo
    ├── build
    │   └── bdist.linux-x86_64
    ├── data_socket
    │   ├── data_socket
    │   │   ├── __init__.py
    │   │   ├── settings.py
    │   │   ├── urls.py
    │   │   └── wsgi.py
    │   └── manage.py
    ├── demo
    │   ├── __init__.py
    │   ├── __pycache__
    │   │   ├── __init__.cpython-38.pyc
    │   │   └── start.cpython-38.pyc
    │   └── start.py
    ├── demo.egg-info
    │   ├── dependency_links.txt
    │   ├── PKG-INFO
    │   ├── SOURCES.txt
    │   └── top_level.txt
    ├── dist
    │   └── demo-1.1.1-py3.8.egg
    ├── __init__.py
    └── setup.py

打包并测试

打包项目

# 在 setup.py 同级目录下执行
python3 setup.py install

查看是否生成 pip 包

pip3 list | grep demo

测试是的可以

>> from demo import start
>> obj = start.Start()
>> obj.host
"127.0.0.1"

Python pbr 打包使用

编写一个测试文件夹

flaskProject/

├── README.md
├── setup.cfg
├── setup.py
├── src
│   ├── app.py
│   └── __init__.py
├── tools
│   ├── __init__.py
│   ├── pbr_hooks.py

flaskProject/setup.py

# -*- coding: utf-8 -*-

from setuptools import setup

setup(
    setup_requires=['pbr'],
    pbr=True,
)

flaskProject/setup.cfg

[metadata]
name = xxsg
home-page = http://127.0.0.1
author = hanlearned
author-email = hanlearned99@gamil.com
description-file = README.md
license = autoai
classifier =
    Development Status :: 1 - Planning
    Environment :: Web Environment
    Intended Audience :: Developers
    License :: Other/Proprietary License
    Operating System :: OS Independent
    Programming Language :: Python :: 3.6
keywords =
    setup
    distutils

[global]
setup-hooks =
    tools.pbr_hooks.setup_hook

[files]
packages =
    src
data_files =
    src/static = src/static/*
    src/templates = src/templates/*

[entry_points]
# console_scripts 这个是添加到 /usr/bin 目录下的命令
# 此处把 hxc_admin 命令添加到 /usr/bin 目录下
# 当执行 hxc_admin 命令时, 会触发 src/app.py 中的 www 对象
console_scripts =
    hxc_admin = src.app:www

flaskProject/src/app.py

# Flask 用于创建主 app
from flask import Flask
from flask import request
from flask import jsonify


# 创建主 app  =>  django 中的 项目配置文件夹
app = Flask(__name__)


@app.route('/index/', methods=['GET'])
def index():
    print("这是一个测试函数")
    return jsonify({})

www = app.run(host='0.0.0.0', port=9999)

flaskProject/tools/pbr_hooks.py

# Copyright 2013 Hewlett-Packard Development Company, L.P.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from distutils.command import install


def setup_hook(config):
    """Filter config parsed from a setup.cfg to inject our defaults."""
    # Tell distutils not to put the data_files in platform-specific
    # installation locations. See here for an explanation:
    # https://groups.google.com/forum/#!topic/comp.lang.python/Nex7L-026uw
    for scheme in install.INSTALL_SCHEMES.values():
        scheme['data'] = scheme['purelib']

打包

sudo python3 setup.py install
hxc_admin
posted @ 2022-06-19 20:45  隔江千万里  阅读(338)  评论(0)    收藏  举报