Python3基础之构建setup.py
技术背景
在上一篇博客中,我们介绍了如何使用pyinstaller将python项目打包成一个可执行文件,并且放在系统目录下,让系统用户可以直接识别到我们构造的项目。而python项目中常见的setup.py
其实也是在执行类似的构建的功能,通过setup.py文件可以将python包按照指定的方案进行构建,构建出来的可执行文件是一个egg
文件。最后将这个egg文件转移到python包的统一管理路径下,这样我们就可以在系统内任一位置的python文件中调用我们构建好的这个python库。
python项目示例
首先我们构造一个大概的目录结构,项目的路径如下所示:
[dechin@dechin-manjaro test_setup]$ tree
.
├── requirements.txt
├── setup.py
└── ts
└── __init__.py
1 directory, 3 files
在一个名为test_setup
的路径下,作为我们最上层的项目根目录。然后在根目录下有需求配置文件requirements.txt
,我们可以在这个文件中添加我们的python库所依赖的其他python库,如numpy
、scipy
等。而setup.py
就是我们这里的安装文件,在后面的章节中会着重提到。最后是我们的项目的核心路径ts
,里面包含了我们的核心代码。
用__init__.py文件构造的简单项目
在一个普通的python项目中,我们可以用目录.模块名.函数名
的形式来构造python项目的引用方法。但是对于一些比较简单的库而言,比如定义一个二叉树的数据结构这种简单的项目,我们可以直接在__init__.py
文件里面直接定义好所有的项目函数及内容。当然,对于一些比较大型的比较规范的项目而言,也会用__init__.py
文件作为一个统一的函数入口,以提升模块化项目的可用性。在本测试用例中,我们也定义了一个简单的py核心代码文件如下:
[dechin@dechin-manjaro test_setup]$ cat ts/__init__.py
# __init__.py
def p2(number):
return number ** 2
def p3(number):
return number ** 3
这个名为ts
的项目具有两个函数功能:p2
用于计算输入参数的平方,以及p3
用于计算输入参数的立方。
构造setup文件
我们主要是基于setuptools来实现一个python项目的构建,以下直接展示本项目的构建方法:
# setup.py
import os
from setuptools import setup, find_packages
__version__ = '1.0' # 版本号
requirements = open('requirements.txt').readlines() # 依赖文件
setup(
name = 'ts', # 在pip中显示的项目名称
version = __version__,
author = 'Dechin',
author_email = 'dechin.phy@gmail.com',
url = '',
description = 'ts: Test Setup',
packages = find_packages(exclude=["tests"]), # 项目中需要拷贝到指定路径的文件夹
python_requires = '>=3.5.0',
install_requires = requirements # 安装依赖
)
在这个构建方法中,我们配置了项目的版本号(版本管理)、依赖库、项目名称以及需要进行构建的文件夹。比如这里我们加了一个exclude
的选项排除了tests
目录(虽然本项目中并没有这个目录,但是一般我们都要剔除测试目录)。当然我们也可以用指定目录进行构建的方法,但是这里不做过多的赘述。
依赖包配置文件
python之所以这么火,很大程度上就得益于其强大的生态,而这些生态都是靠别人搭建好的轮子来支撑起来的。因此大部分的python项目都会依赖于第三方的python包,在安装的时候我们可以仅用一个文件就进行配置:
[dechin@dechin-manjaro test_setup]$ cat requirements.txt
numpy==1.20.1
在requirements.txt
的配置文件中,我们最好是能够指定一个固定的版本号,这样可以确保软件的兼容性。
执行安装
按照上述的方法对我们的python项目进行编写后,就可以开始执行构建,如果需要测试编译可以先运行python3 setup.py build
来进行测试,在安装成功后再执行install
指令,当然我们也可以直接一步执行python3 setup.py install
指令来进行安装:
[dechin@dechin-manjaro test_setup]$ python3 setup.py install
running install
running bdist_egg
running egg_info
writing ts.egg-info/PKG-INFO
writing dependency_links to ts.egg-info/dependency_links.txt
writing requirements to ts.egg-info/requires.txt
writing top-level names to ts.egg-info/top_level.txt
reading manifest file 'ts.egg-info/SOURCES.txt'
writing manifest file 'ts.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build/lib
creating build/lib/ts
copying ts/__init__.py -> build/lib/ts
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/ts
copying build/lib/ts/__init__.py -> build/bdist.linux-x86_64/egg/ts
byte-compiling build/bdist.linux-x86_64/egg/ts/__init__.py to __init__.cpython-38.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying ts.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying ts.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying ts.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying ts.egg-info/requires.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying ts.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating 'dist/ts-1.0-py3.8.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing ts-1.0-py3.8.egg
Copying ts-1.0-py3.8.egg to /home/dechin/anaconda3/lib/python3.8/site-packages
Adding ts 1.0 to easy-install.pth file
Installed /home/dechin/anaconda3/lib/python3.8/site-packages/ts-1.0-py3.8.egg
Processing dependencies for ts==1.0
Searching for numpy==1.20.1
Best match: numpy 1.20.1
Adding numpy 1.20.1 to easy-install.pth file
Installing f2py script to /home/dechin/anaconda3/bin
Installing f2py3 script to /home/dechin/anaconda3/bin
Installing f2py3.8 script to /home/dechin/anaconda3/bin
Using /home/dechin/anaconda3/lib/python3.8/site-packages
Finished processing dependencies for ts==1.0
安装完成后,我们可以在pip的管理包目录下找到我们所构建的python包:
[dechin@dechin-manjaro test_setup]$ python3 -m pip list
Package Version
---------------------------------- -------------------
ts 1.0
同时在执行完build指令之后,本地目录下会生成一系列的编译构建目录,如build和dist等:
[dechin@dechin-manjaro test_setup]$ tree
.
├── build
│ └── bdist.linux-x86_64
├── dist
│ └── ts-1.0-py3.8.egg
├── requirements.txt
├── setup.py
├── ts
│ ├── __init__.py
│ └── __pycache__
│ └── __init__.cpython-38.pyc
└── ts.egg-info
├── dependency_links.txt
├── PKG-INFO
├── requires.txt
├── SOURCES.txt
└── top_level.txt
6 directories, 10 files
而dist目录下的egg文件在执行完install指令之后,会被拷贝到系统指定的python包管理路径下,我们可以在系统中搜索到这个文件:
[dechin-root test_setup]# find / -name *ts-1.0-py3.8.egg
/home/dechin/anaconda3/lib/python3.8/site-packages/ts-1.0-py3.8.egg
/home/dechin/projects/2021-python/setup/test_setup/dist/ts-1.0-py3.8.egg
这里我们可以看到第一个路径就是python包管理路径。
软件包功能测试
在安装完成后,我们可以在任意的路径下引用到我们构建好的ts
项目,比如这里我们可以用ipython来测试一下:
[dechin@dechin-manjaro test_setup]$ ipython
Python 3.8.5 (default, Sep 4 2020, 07:30:14)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from ts import p2, p3
In [2]: p2(4)
Out[2]: 16
In [3]: p3(4)
Out[3]: 64
测试结果表明,我们成功的从编译构建好的ts项目中引用了平方和立方的计算函数。
安装包的删除
跟其他的python包一样,我们可以用pip来统一管理,也可以用pip来直接删除我们自己安装的ts项目:
[dechin@dechin-manjaro test_setup]$ python3 -m pip uninstall ts
Found existing installation: ts 1.0
Uninstalling ts-1.0:
Would remove:
/home/dechin/anaconda3/lib/python3.8/site-packages/ts-1.0-py3.8.egg
Proceed (y/n)? y
Successfully uninstalled ts-1.0
总结概要
一个完善的python项目,不仅需要梳理好核心代码的软件架构,还需要定义好依赖文件、编译构建文件、API接口文档、编码规范门禁等。这里我们介绍了如何用setup.py文件来完善一个最简单的python项目,这也是每一个python开源项目所必须要具备的条件。
版权声明
本文首发链接为:https://www.cnblogs.com/dechinphy/p/setup.html
作者ID:DechinPhy
更多原著文章请参考:https://www.cnblogs.com/dechinphy/
Python3基础之构建setup.py的更多相关文章
- Python3安装turtle提示错误:Command "python setup.py egg_info" failed with error code 1
Python3安装turtle提示错误:Command "python setup.py egg_info" failed with error code 1 Python3.5安 ...
- python的构建工具setup.py
一.构建工具setup.py的应用场景 在安装python的相关模块和库时,我们一般使用“pip install 模块名”或者“python setup.py install”,前者是在线安装,会安 ...
- rqalpha探究 1 setup.py
rqalpha是难得几个好的做量化交易的开源项目,不过由于自己python用的实在不多,看起来还是觉得很复杂. 因此准备抽取出框架,从最简单的搭建. 思路 从setup着手,看一下如何建立一个发布工程 ...
- python 使用 setup.py 方式安装及包的卸载
安装: 可通过 --home 或 --prefix 指定安装目录 --prefix=xx/xxx 选择安装目录 --record files.txt 记录所有安装文件的路径 ...
- Command "python setup.py egg_info" failed with error code 10
1:今天系统重装以后,下载了新的版本的python3.6.1.然后想通过pycurl模块测试URL,突然发现windows10下我无法通过pip安装pycurl模块了,报错内容如下 Collectin ...
- Command "python setup.py egg_info" failed with error code 1一种问题的解决方法
问题描述:无论是你在pycharm中直接使用import and install命令,还是pip的时候出现了Command "python setup.py egg_info" f ...
- python3基础视频教程
随着目前Python行业的薪资水平越来越高,很多人想加入该行业拿高薪.有没有想通过视频教程入门的同学们?这份Python教程全集等你来学习啦! python3基础视频教程:http://pan.bai ...
- python2 使用pip安装psycopg2出现错误:Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-mvzdNj/psycopg2/
公司业务需求,开发语言python2,需要使用数据库:postgresql,需要安装模块psycopg2这个模块, 使用pip install psycopg2 报错: Command "p ...
- edgedb 内部pg 数据存储的探索 (三) 源码包setup.py 文件
edgedb 是基于python开发的,同时集成了cython 以下为包的setup.py 配置,从里面我们可以看到关于edgedb 的一些依赖 以及构建过程 setup.py 源码 整体配置不算很多 ...
随机推荐
- 2021 从零开始打造一个自己的 UI 组件库
2021 从零开始打造一个自己的 UI 组件库 refs GUI https://github.com/xgqfrms/gui/ https://www.npmjs.com/package/@xgqf ...
- reCAPTCHA OCR 详解 , 验验证, OCR(光学自动识别)
WEB安全专题 reCAPTCHA的诞生及意义 CMU(卡耐基梅隆大学)设计了一个名叫reCAPTCHA的强大系统,让电脑去向人类求助.具体做法是:将OCR(光学自动识别)软件无法识别的文字扫 ...
- rm -rf .git/gc.log
rm -rf .git/gc.log ➜ test git:(abc) gp Auto packing the repository in background for optimum perform ...
- css animation & animationend event & onanimationend
css animation & animationend event & onanimationend https://developer.mozilla.org/en-US/docs ...
- HTTP in depth
HTTP in depth https://developer.mozilla.org/en-US/docs/Web/HTTP https://developer.mozilla.org/en-US/ ...
- Linux & bash & tcpdump
Linux & bash & tcpdump Linux & tcpdump https://www.tecmint.com/12-tcpdump-commands-a-net ...
- SVG & gradient & color
SVG & gradient & color https://developer.mozilla.org/zh-CN/docs/Web/SVG/Tutorial/Gradients & ...
- 稳定币USDN的算法调控
在NGK公链的稳定币系统中,USDN的价格有时会出现一定幅度的波动.正如我们会看到USDT有时会是0.99美元,有时是1.01美元一样.那么,要保障USDN在二级市场的价格基本稳定,要如何调节供需呢? ...
- django学习-5.获取url参数和name的作用
1.前言 假如我们要打开这两个博客园地址:[https://www.cnblogs.com/xiamen-momo/archive/2020/11.html].[https://www.cnblogs ...
- 03_MySQL重置root密码
重设root密码