零、说在前面
作为程序猿,平时就喜欢造轮子,
为了提升工作效率,也会写一些脚本,早期用的 PHP,后来用的 Shell,
为了更方便的使用,免去较长的「脚本路径」,所以都是把脚本放到固定目录,然后通过自定义 aliens 来简化使用,例如:
# /Users/mengxinxin/.bash_files
....
# FileArchiver —— 智能文件归档工具
alias fa='sh ~/.bash_files/FileArchiver/file_archiver.sh'
# php markdown_offline_reading.php —— Markdown离线阅读工具
alias md.offline='php ~/.bash_files/MarkdownOfflineReading/markdown_offline_reading.php'
...
当轮子多了,如何更高效的管理、发布版本,就成了问题,而对于包管理平台:
OC/Swift有CocoaPods、Carthagenodejs有npmPython有pipRuby有gem- 此外还有大家熟知的
Homebrew
在经过一些简单的调研,最终我选择了 pip
一、简单介绍
-
PyPI:即Python Package Index,是Python的包管理平台,有2个部分,且是完全独立的(包括包、账号):- pypi.org :负责正式包的管理&索引
- test.pypi.org :负责测试包的管理&索引
-
pip:是Python包的安装器,引用官网的描述就是:pip is the package installer for Python. You can use it to install packages from the Python Package Index and other indexes.
二、重要说明
因为我目前用的是 Python3,所以相关 pip 命令,用的都是 pip3
—— 因为 pip 用的是 Python2
$ python3 --version
Python 3.9.9
三、发布新包
1、包文件说明
本文以创建名为
ampt2的包为例,读者在照着开发时,请务必改用其他名称,以避免重名
请先按照如下示例,建立包的必要目录/文件结构:
(1)目录结构
Andys-MacBook-Pro-2018:ampt2 mengxinxin$ tree
.
├── README.md
├── setup.cfg
├── setup.py
├── src
│ └── ampt2
│ └── __init__.py
└── tests
3 directories, 4 files
对应的文件内容 可参考如下示例:
(2)_init_.py
#!/usr/bin/env python
# encoding=utf-8
def ampt2():
print("Hello ampt2 ~")
(3)setup.py
#!/usr/bin/env python
# coding: utf-8
from setuptools import setup
setup(
name='ampt2',
version='0.0.1',
author='Andy Meng',
author_email='andy_m129@163.com',
url='https://juejin.cn/user/2875978147966855',
description="Andy Meng's Python Test",
packages=['ampt2'],
install_requires=[],
entry_points={
'console_scripts': [
'ampt2=ampt2:ampt2'
]
}
)
(4)setup.cfg
[metadata]
name = ampt2-Andy__M
version = 0.0.1
author = Andy Meng
author_email = andy_m129@163.com
description = Andy Meng's python test package
long_description = file: README.md
long_description_content_type = text/markdown
url = https://juejin.cn/user/2875978147966855
project_urls =
Bug Tracker = https://github.com/pypa/sampleproject/issues
classifiers =
Programming Language :: Python :: 3
License :: OSI Approved :: MIT License
Operating System :: OS Independent
[options]
package_dir =
= src
packages = find:
python_requires = >=3.6
[options.packages.find]
where = src
在完成开发后,可以先把包发布到测试平台(当然 这不是必须的),待确定无误后 再发布到正式的平台,
具体方法如下:
2、发布开发版
注:发布包到测试平台,需要先注册一个测试平台的账号,地址如下:
(1)构建
Andys-MacBook-Pro-2018:ampt2 mengxinxin$ python3 -m build
* Creating venv isolated environment...
* Installing packages in isolated environment... (setuptools >= 40.8.0, wheel)
* Getting dependencies for sdist...
running egg_info
writing src/ampt2.egg-info/PKG-INFO
writing dependency_links to src/ampt2.egg-info/dependency_links.txt
writing entry points to src/ampt2.egg-info/entry_points.txt
writing top-level names to src/ampt2.egg-info/top_level.txt
reading manifest file 'src/ampt2.egg-info/SOURCES.txt'
writing manifest file 'src/ampt2.egg-info/SOURCES.txt'
* Building sdist...
running sdist
running egg_info
writing src/ampt2.egg-info/PKG-INFO
writing dependency_links to src/ampt2.egg-info/dependency_links.txt
writing entry points to src/ampt2.egg-info/entry_points.txt
writing top-level names to src/ampt2.egg-info/top_level.txt
reading manifest file 'src/ampt2.egg-info/SOURCES.txt'
writing manifest file 'src/ampt2.egg-info/SOURCES.txt'
running check
creating ampt2-0.0.1
creating ampt2-0.0.1/src
creating ampt2-0.0.1/src/ampt2
creating ampt2-0.0.1/src/ampt2.egg-info
copying files to ampt2-0.0.1...
copying README.md -> ampt2-0.0.1
copying setup.cfg -> ampt2-0.0.1
copying setup.py -> ampt2-0.0.1
copying src/ampt2/__init__.py -> ampt2-0.0.1/src/ampt2
copying src/ampt2/ampt2.py -> ampt2-0.0.1/src/ampt2
copying src/ampt2.egg-info/PKG-INFO -> ampt2-0.0.1/src/ampt2.egg-info
copying src/ampt2.egg-info/SOURCES.txt -> ampt2-0.0.1/src/ampt2.egg-info
copying src/ampt2.egg-info/dependency_links.txt -> ampt2-0.0.1/src/ampt2.egg-info
copying src/ampt2.egg-info/entry_points.txt -> ampt2-0.0.1/src/ampt2.egg-info
copying src/ampt2.egg-info/top_level.txt -> ampt2-0.0.1/src/ampt2.egg-info
Writing ampt2-0.0.1/setup.cfg
Creating tar archive
removing 'ampt2-0.0.1' (and everything under it)
* Building wheel from sdist
* Creating venv isolated environment...
* Installing packages in isolated environment... (setuptools >= 40.8.0, wheel)
* Getting dependencies for wheel...
running egg_info
writing src/ampt2.egg-info/PKG-INFO
writing dependency_links to src/ampt2.egg-info/dependency_links.txt
writing entry points to src/ampt2.egg-info/entry_points.txt
writing top-level names to src/ampt2.egg-info/top_level.txt
reading manifest file 'src/ampt2.egg-info/SOURCES.txt'
writing manifest file 'src/ampt2.egg-info/SOURCES.txt'
* Installing packages in isolated environment... (wheel)
* Building wheel...
running bdist_wheel
running build
running build_py
creating build
creating build/lib
creating build/lib/ampt2
copying src/ampt2/ampt2.py -> build/lib/ampt2
copying src/ampt2/__init__.py -> build/lib/ampt2
installing to build/bdist.macosx-12-x86_64/wheel
running install
running install_lib
creating build/bdist.macosx-12-x86_64
creating build/bdist.macosx-12-x86_64/wheel
creating build/bdist.macosx-12-x86_64/wheel/ampt2
copying build/lib/ampt2/ampt2.py -> build/bdist.macosx-12-x86_64/wheel/ampt2
copying build/lib/ampt2/__init__.py -> build/bdist.macosx-12-x86_64/wheel/ampt2
running install_egg_info
running egg_info
writing src/ampt2.egg-info/PKG-INFO
writing dependency_links to src/ampt2.egg-info/dependency_links.txt
writing entry points to src/ampt2.egg-info/entry_points.txt
writing top-level names to src/ampt2.egg-info/top_level.txt
reading manifest file 'src/ampt2.egg-info/SOURCES.txt'
writing manifest file 'src/ampt2.egg-info/SOURCES.txt'
Copying src/ampt2.egg-info to build/bdist.macosx-12-x86_64/wheel/ampt2-0.0.1-py3.9.egg-info
running install_scripts
creating build/bdist.macosx-12-x86_64/wheel/ampt2-0.0.1.dist-info/WHEEL
creating '/Users/mengxinxin/Root/Work/Study/Python3/MyProjects/pip_test/ampt2/dist/tmpgm35op7o/ampt2-0.0.1-py3-none-any.whl' and adding 'build/bdist.macosx-12-x86_64/wheel' to it
adding 'ampt2/__init__.py'
adding 'ampt2/ampt2.py'
adding 'ampt2-0.0.1.dist-info/METADATA'
adding 'ampt2-0.0.1.dist-info/WHEEL'
adding 'ampt2-0.0.1.dist-info/entry_points.txt'
adding 'ampt2-0.0.1.dist-info/top_level.txt'
adding 'ampt2-0.0.1.dist-info/RECORD'
removing build/bdist.macosx-12-x86_64/wheel
Successfully built ampt2-0.0.1.tar.gz and ampt2-0.0.1-py3-none-any.whl
Andys-MacBook-Pro-2018:ampt2 mengxinxin$
(2)上传
须确保已安装/升级
twine到正确的版本
- 安装:
python3 -m pip install twine- 升级:
python3 -m pip install --upgrade twine
Andys-MacBook-Pro-2018:ampt2 mengxinxin$ twine upload dist/* --repository testpypi
Uploading distributions to https://test.pypi.org/legacy/
Uploading ampt2-0.0.1-py3-none-any.whl
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 5.06k/5.06k [00:01<00:00, 3.26kB/s]
Uploading ampt2-0.0.1.tar.gz
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4.79k/4.79k [00:00<00:00, 7.33kB/s]
View at:
https://test.pypi.org/project/ampt2/0.0.1/
Andys-MacBook-Pro-2018:ampt2 mengxinxin$
刚发布的包的主页如下:
(3)安装
Andys-MacBook-Pro-2018:ampt2 mengxinxin$ pip3 install --index-url https://test.pypi.org/simple/ --no-deps ampt2
DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621
Looking in indexes: https://test.pypi.org/simple/
Collecting ampt2
Downloading https://test-files.pythonhosted.org/packages/19/6e/e0c1a4a1ce06859c0c8da2e2d6e566390cb2f95e3fab576da0946d9e5b0b/ampt2-0.0.1-py3-none-any.whl (1.9 kB)
Installing collected packages: ampt2
DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621
Successfully installed ampt2-0.0.1
Andys-MacBook-Pro-2018:ampt2 mengxinxin$
(4)测试
Andys-MacBook-Pro-2018:ampt2 mengxinxin$ ampt2
Hello ampt2 ~
Andys-MacBook-Pro-2018:ampt2 mengxinxin$
3、发布正式版
# (1)构建
$ python3 -m build
# (2)上传
$ twine upload dist/*
# (3)安装
$ pip3 install ampt2
# (4)使用
$ ampt2
四、更新包版本
如果修复了某些问题,或是加了某些新功能,就需要升级包 并发布,具体步骤如下:
1、更新代码、包版本
以下仅为主要位置的修改,以做示意
# setup.cfg
version = 0.0.2
# setup.py
version='0.0.2',
# __init__.py
def ampt2():
print("Hello ampt2 (v0.0.2) ~")
2、发布开发版
(1)构建
Andys-MacBook-Pro-2018:ampt2 mengxinxin$ python3 -m build
* Creating venv isolated environment...
* Installing packages in isolated environment... (setuptools >= 40.8.0, wheel)
* Getting dependencies for sdist...
running egg_info
writing src/ampt2.egg-info/PKG-INFO
writing dependency_links to src/ampt2.egg-info/dependency_links.txt
writing entry points to src/ampt2.egg-info/entry_points.txt
writing top-level names to src/ampt2.egg-info/top_level.txt
reading manifest file 'src/ampt2.egg-info/SOURCES.txt'
writing manifest file 'src/ampt2.egg-info/SOURCES.txt'
* Building sdist...
running sdist
running egg_info
writing src/ampt2.egg-info/PKG-INFO
writing dependency_links to src/ampt2.egg-info/dependency_links.txt
writing entry points to src/ampt2.egg-info/entry_points.txt
writing top-level names to src/ampt2.egg-info/top_level.txt
reading manifest file 'src/ampt2.egg-info/SOURCES.txt'
writing manifest file 'src/ampt2.egg-info/SOURCES.txt'
running check
creating ampt2-0.0.2
creating ampt2-0.0.2/src
creating ampt2-0.0.2/src/ampt2
creating ampt2-0.0.2/src/ampt2.egg-info
copying files to ampt2-0.0.2...
copying README.md -> ampt2-0.0.2
copying setup.cfg -> ampt2-0.0.2
copying setup.py -> ampt2-0.0.2
copying src/ampt2/__init__.py -> ampt2-0.0.2/src/ampt2
copying src/ampt2/ampt2.py -> ampt2-0.0.2/src/ampt2
copying src/ampt2.egg-info/PKG-INFO -> ampt2-0.0.2/src/ampt2.egg-info
copying src/ampt2.egg-info/SOURCES.txt -> ampt2-0.0.2/src/ampt2.egg-info
copying src/ampt2.egg-info/dependency_links.txt -> ampt2-0.0.2/src/ampt2.egg-info
copying src/ampt2.egg-info/entry_points.txt -> ampt2-0.0.2/src/ampt2.egg-info
copying src/ampt2.egg-info/top_level.txt -> ampt2-0.0.2/src/ampt2.egg-info
Writing ampt2-0.0.2/setup.cfg
Creating tar archive
removing 'ampt2-0.0.2' (and everything under it)
* Building wheel from sdist
* Creating venv isolated environment...
* Installing packages in isolated environment... (setuptools >= 40.8.0, wheel)
* Getting dependencies for wheel...
running egg_info
writing src/ampt2.egg-info/PKG-INFO
writing dependency_links to src/ampt2.egg-info/dependency_links.txt
writing entry points to src/ampt2.egg-info/entry_points.txt
writing top-level names to src/ampt2.egg-info/top_level.txt
reading manifest file 'src/ampt2.egg-info/SOURCES.txt'
writing manifest file 'src/ampt2.egg-info/SOURCES.txt'
* Installing packages in isolated environment... (wheel)
* Building wheel...
running bdist_wheel
running build
running build_py
creating build
creating build/lib
creating build/lib/ampt2
copying src/ampt2/ampt2.py -> build/lib/ampt2
copying src/ampt2/__init__.py -> build/lib/ampt2
installing to build/bdist.macosx-12-x86_64/wheel
running install
running install_lib
creating build/bdist.macosx-12-x86_64
creating build/bdist.macosx-12-x86_64/wheel
creating build/bdist.macosx-12-x86_64/wheel/ampt2
copying build/lib/ampt2/ampt2.py -> build/bdist.macosx-12-x86_64/wheel/ampt2
copying build/lib/ampt2/__init__.py -> build/bdist.macosx-12-x86_64/wheel/ampt2
running install_egg_info
running egg_info
writing src/ampt2.egg-info/PKG-INFO
writing dependency_links to src/ampt2.egg-info/dependency_links.txt
writing entry points to src/ampt2.egg-info/entry_points.txt
writing top-level names to src/ampt2.egg-info/top_level.txt
reading manifest file 'src/ampt2.egg-info/SOURCES.txt'
writing manifest file 'src/ampt2.egg-info/SOURCES.txt'
Copying src/ampt2.egg-info to build/bdist.macosx-12-x86_64/wheel/ampt2-0.0.2-py3.9.egg-info
running install_scripts
creating build/bdist.macosx-12-x86_64/wheel/ampt2-0.0.2.dist-info/WHEEL
creating '/Users/mengxinxin/Root/Work/Study/Python3/MyProjects/pip_test/ampt2/dist/tmp7vgnxdgl/ampt2-0.0.2-py3-none-any.whl' and adding 'build/bdist.macosx-12-x86_64/wheel' to it
adding 'ampt2/__init__.py'
adding 'ampt2/ampt2.py'
adding 'ampt2-0.0.2.dist-info/METADATA'
adding 'ampt2-0.0.2.dist-info/WHEEL'
adding 'ampt2-0.0.2.dist-info/entry_points.txt'
adding 'ampt2-0.0.2.dist-info/top_level.txt'
adding 'ampt2-0.0.2.dist-info/RECORD'
removing build/bdist.macosx-12-x86_64/wheel
Successfully built ampt2-0.0.2.tar.gz and ampt2-0.0.2-py3-none-any.whl
Andys-MacBook-Pro-2018:ampt2 mengxinxin$
(2)上传
Andys-MacBook-Pro-2018:ampt2 mengxinxin$ twine upload dist/* --repository testpypi
Uploading distributions to https://test.pypi.org/legacy/
Uploading ampt2-0.0.1-py3-none-any.whl
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 5.06k/5.06k [00:01<00:00, 3.14kB/s]
Uploading ampt2-0.0.2-py3-none-any.whl
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 5.05k/5.05k [00:02<00:00, 2.53kB/s]
Uploading ampt2-0.0.1.tar.gz
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4.79k/4.79k [00:00<00:00, 7.99kB/s]
Uploading ampt2-0.0.2.tar.gz
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4.79k/4.79k [00:01<00:00, 3.99kB/s]
View at:
https://test.pypi.org/project/ampt2/0.0.2/
https://test.pypi.org/project/ampt2/0.0.1/
Andys-MacBook-Pro-2018:ampt2 mengxinxin$
(3)安装
安装命令,可以从刚发布的包主页获取
Andys-MacBook-Pro-2018:ampt2 mengxinxin$ pip3 install -i https://test.pypi.org/simple/ ampt2==0.0.2
DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621
Looking in indexes: https://test.pypi.org/simple/
Collecting ampt2==0.0.2
Downloading https://test-files.pythonhosted.org/packages/5e/68/bb549df7e7b9ce5d7d23e2c03683376ae22fcdefa95114e60a3ac6e04169/ampt2-0.0.2-py3-none-any.whl (1.9 kB)
Installing collected packages: ampt2
Attempting uninstall: ampt2
Found existing installation: ampt2 0.0.1
Uninstalling ampt2-0.0.1:
Successfully uninstalled ampt2-0.0.1
DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621
Successfully installed ampt2-0.0.2
(4)测试
Andys-MacBook-Pro-2018:ampt2 mengxinxin$ ampt2
Hello ampt2 (v0.0.2) ~
Andys-MacBook-Pro-2018:ampt2 mengxinxin$
3、发布正式版
# (1)构建
$ python3 -m build
# (2)上传
$ twine upload dist/*
# (3)升级现有版本
$ pip3 install ampt2 --upgrade
DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621
Requirement already satisfied: ampt2 in /usr/local/lib/python3.9/site-packages (0.0.1)
Collecting ampt2
Downloading ampt2-0.0.2-py3-none-any.whl (1.9 kB)
Installing collected packages: ampt2
Attempting uninstall: ampt2
Found existing installation: ampt2 0.0.1
Uninstalling ampt2-0.0.1:
Successfully uninstalled ampt2-0.0.1
DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621
Successfully installed ampt2-0.0.2
# 或是 新安装
$ pip3 install ampt2==0.0.2