建立和使用Python自定义模块:打包+pip安装

66 篇文章 8 订阅
34 篇文章 2 订阅

《建立和使用Python自定义模块》里面,我把自定义的模块拷贝到 site-packagesdist-packages 目录。
可是 Ctrl-C + Ctrl-V 总归不是正常的动作,所以我还是想把模块做成安装包,这样便于管理版本。

(零)拷目录-无法卸载

顺便说一下,之前的模块因为是拷进去的。
虽然pip list和show都能用,但uninstall则找不到安装信息,如下:

> pip uninstall ACTP
Found existing installation: ACTP   1.0.3
Can't uninstall 'ACTP  '. No files were found to uninstall.

(一)打包结构

之前的目录结构:
在这里插入图片描述

需要扩充成类似下面的结构,也就是包ACTP需要上一层有固定的结构。

TopPath/
├── README.txt
├── setup.py
└── ACTP/
	├── __init__.py
	└── crossPlat.py
	└── DTStringFinder.py
	└── mylogger.py
	└── tnu.py

其中README.txt在Windwos下是个Ansi编码的文本文件。
可以加入这个自定义包比较详细的说明内容,打包后会贴在元数据的后面。

另外还需要修改:

(1.1)__init__.py

之前这个文件是空的,现在添加包的import和版本信息。

from .crossPlat import *
from .DTStringFinder import *
from .mylogger import *
from .tnu import *

__version__ = '1.0.3'

(1.2)setup.py

新建这个文件,加入一些包的信息。
这些就是pip show 的时候显示的(以及安装包的)内容。

from setuptools import setup
from setuptools import find_packages

setup(
    name="ACTP",
    version="1.0.3",
    author="曾如石",
    author_email="ddrfan@163.com",
    url='http://www.scaocheng.com/',
    description="在奥诚科技的任务调度平台中更加方便的开发Python类型业务规则",
    long_description=open('README.txt').read(),
    long_description_content_type="text/markdown",
    python_requires=">=3.6.0",
    license="Private",
    install_requires=[""],
    # packages=["crossPlat","tnu",],
    packages=find_packages(),                           # 打包目录下的全部模块
    # packages=find_packages(exclude=["A", "B"])		# 打包除了指定模块的全部模块
    # include_package_data = True,						# 打包路径下的其他文件
    platforms="any",
)

(二)开始打包

进入setup.py所在的上层目录,
执行:python setup.py bdist_wheel

PS: D:\TopPath> python setup.py bdist_wheel
running bdist_wheel
running build
running build_py
creating build
creating build\lib
creating build\lib\ACTP
copying ACTP\crossPlat.py -> build\lib\ACTP
copying ACTP\DTStringFinder.py -> build\lib\ACTP
copying ACTP\mylogger.py -> build\lib\ACTP
copying ACTP\tnu.py -> build\lib\ACTP
copying ACTP\__init__.py -> build\lib\ACTP
\Python\Python310\lib\site-packages\setuptools\command\install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
  warnings.warn(
installing to build\bdist.win-amd64\wheel
running install
running install_lib
creating build\bdist.win-amd64
creating build\bdist.win-amd64\wheel
creating build\bdist.win-amd64\wheel\ACTP
copying build\lib\ACTP\crossPlat.py -> build\bdist.win-amd64\wheel\.\ACTP
copying build\lib\ACTP\DTStringFinder.py -> build\bdist.win-amd64\wheel\.\ACTP
copying build\lib\ACTP\mylogger.py -> build\bdist.win-amd64\wheel\.\ACTP
copying build\lib\ACTP\tnu.py -> build\bdist.win-amd64\wheel\.\ACTP
copying build\lib\ACTP\__init__.py -> build\bdist.win-amd64\wheel\.\ACTP
running install_egg_info
running egg_info
creating ACTP.egg-info
writing ACTP.egg-info\PKG-INFO
writing dependency_links to ACTP.egg-info\dependency_links.txt
writing top-level names to ACTP.egg-info\top_level.txt
writing manifest file 'ACTP.egg-info\SOURCES.txt'
reading manifest file 'ACTP.egg-info\SOURCES.txt'
writing manifest file 'ACTP.egg-info\SOURCES.txt'
Copying ACTP.egg-info to build\bdist.win-amd64\wheel\.\ACTP-1.0.3-py3.10.egg-info
running install_scripts
creating build\bdist.win-amd64\wheel\ACTP-1.0.3.dist-info\WHEEL
creating 'dist\ACTP-1.0.3-py3-none-any.whl' and adding 'build\bdist.win-amd64\wheel' to it
adding 'ACTP/DTStringFinder.py'
adding 'ACTP/__init__.py'
adding 'ACTP/crossPlat.py'
adding 'ACTP/mylogger.py'
adding 'ACTP/tnu.py'
adding 'ACTP-1.0.3.dist-info/METADATA'
adding 'ACTP-1.0.3.dist-info/WHEEL'
adding 'ACTP-1.0.3.dist-info/top_level.txt'
adding 'ACTP-1.0.3.dist-info/RECORD'
removing build\bdist.win-amd64\wheel
PS: D:\TopPath>

然后在生成的dist目录中,
出现了打好的安装包ACTP-1.0.3-py3-none-any.whl

可以通过pip来安装了:

...\dist> pip install .\ACTP-1.0.3-py3-none-any.whl
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Processing d:\workshwc\ac_dcm\publicpython\dist\actp-1.0.3-py3-none-any.whl
Installing collected packages: ACTP
Successfully installed ACTP-1.0.3

(2.1)命令出错?

error: invalid command 'bdist_wheel'

那么就先:

> pip install wheel
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting wheel
  Downloading https://mirrors.aliyun.com/pypi/packages/61/86/cc8d1ff2ca31a312a25a708c891cf9facbad4eae493b3872638db6785eb5/wheel-0.40.0-py3-none-any.whl (64 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 64.5/64.5 kB 290.4 kB/s eta 0:00:00
Installing collected packages: wheel
Successfully installed wheel-0.40.0

(三)测试

(3.1)安装

pip install ACTP-1.0.3-py3-none-any.whl :前面测试了,正常。

(3.2)依赖程序

执行依赖ACTP的程序,OK。

在这里插入图片描述

(3.3)pip list

看得到。

> pip list
Package            Version
------------------ --------
ACTP               1.0.3
certifi            2023.5.7
charset-normalizer 3.1.0
distlib            0.3.6
filelock           3.12.0
idna               3.4
piexif             1.1.3
Pillow             9.5.0
pip                23.1.2
platformdirs       3.5.1
py4j               0.10.9.2
pyspark            3.2.0
requests           2.30.0
setuptools         65.5.0
urllib3            2.0.2
virtualenv         20.23.0
wheel              0.40.0

(3.4)pip show

正常显示信息。
但show看不见我们在README.txt里面的写内容。

> pip show ACTP
Name: ACTP
Version: 1.0.3
Summary: 在奥诚科技的任务调度平台中更加方便的开发Python类型业务规则
Home-page: http://www.scaocheng.com/
Author: 曾如石
Author-email: ddrfan@163.com
License: Private
Location: ...\python310\lib\site-packages
Requires:
Required-by:

(3.5)pip uninstall

能卸载。

> pip uninstall ACTP
Found existing installation: ACTP 1.0.3
Uninstalling ACTP-1.0.3:
  Would remove:
    ...\python310\lib\site-packages\actp-1.0.3.dist-info\*
    ...\python310\lib\site-packages\actp\*
Proceed (Y/n)? y
  Successfully uninstalled ACTP-1.0.3
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值