A Python package for version control.
Project description
kangtools
kangtools
- git remote
- renew verison.
- upload add date.
创建并发布自己的Python包到PyPI需要以下步骤:
-
项目结构:在你的项目目录下创建以下文件和目录结构:
your_project/ ├── your_package/ │ ├── __init__.py │ ├── your_module.py # 将你的版本控制代码放在这个文件里 ├── setup.py ├── README.md ├── LICENSE
其中,
your_package
是你的Python包的名字,your_module
是模块的名字。__init__.py
是一个必需的空文件,它告诉Python这个目录是一个包。 -
setup.py
文件:这个文件用于定义你的包的元数据,包括包的名字,版本,作者,许可证等。下面是一个例子:from setuptools import setup setup( name='your-package', # 包的名字 version='0.0.1', # 包的版本 author='Your Name', # 你的名字 author_email='your.email@example.com', # 你的邮箱 packages=['your_package'], # 包含的包,这是一个列表,如果你的项目包含多个包,都需要列在这里 license='LICENSE', # 许可证文件 description='A Python package for version control.', # 包的简短描述 long_description=open('README.md').read(), # 包的详细描述,通常从 README.md 文件读取 long_description_content_type="text/markdown", # 描述的格式,使用markdown格式 )
-
README.md
和LICENSE
文件:README.md
文件包含了你的包的详细信息和使用方法。LICENSE
文件包含了你的包的许可证信息。 -
注册PyPI账户:访问 https://pypi.org/ 并注册一个账户。
-
安装工具:使用pip安装
twine
,wheel
和setuptools
,这些工具用于构建和上传你的包:pip install twine wheel setuptools
-
构建包:在你的项目目录下运行以下命令,它会创建
dist/
目录,并在该目录下生成你的包的源码和wheel格式的发行版本:python setup.py sdist bdist_wheel
-
上传包:使用
twine
将你的包上传到PyPI:twine upload dist/*
这时你需要输入你的PyPI账户的用户名和密码。
-
安装包:现在你就可以使用pip从PyPI安装你的包了:
pip install your-package
你需要将
your-package
替换为你的包的名字。
以上就是创建并发布Python包到PyPI的基本流程。请注意,每次发布新的版本时,需要在 setup.py
文件中更新版本号,然后重新
pip install twine wheel setuptools
您可以将 version_control.py
作为一个命令行工具来使用,有两种方法可以实现这一点。
-
在python文件中添加shebang行: 在你的
version_control.py
文件的最顶端添加一行#!/usr/bin/env python3
,这是一个指定解释器的shebang。然后你需要给这个文件添加执行权限,你可以使用chmod +x version_control.py
命令来做这件事。这样你就可以像一个脚本一样运行它了,例如./version_control.py
。 -
将python代码封装为命令行工具: Python 提供了一些库如 argparse, click 可以帮助你创建自己的命令行工具,然后你可以在
setup.py
中使用entry_points
来指定这个工具的名字和对应的函数。
示例:
在 setup.py
中添加以下代码:
entry_points={
'console_scripts': [
'kangtools_version_control=kangtools.version_control:main',
],
},
然后你需要在 version_control.py
中添加一个 main
函数,把上面的代码封装在这个函数里。main
函数可以接收命令行参数。例如:
def main():
# your existing code goes here...
...
if __name__ == "__main__":
main()
现在你可以重新安装你的包,然后你可以在任何地方使用 kangtools_version_control
这个命令行工具了。例如:
kangtools_version_control
这将会运行 version_control.py
文件中的 main
函数。
添加了内容。- add date- kang.
将version_control.py 更新为 commit_version_to_git.py 因为原命令的名称,并不清晰。 越清晰,我们越好操作。也不容易忘记。
----# 关于参数引用
这个错误的原因是,你在commit_version_to_git.py
的main
函数中定义了一个名为args
的参数,但是当你在命令行中调用kangtools_commit_version_to_git
命令时,并没有传递这个参数。从错误信息中可以看出,当调用main()
函数时,它期望有一个位置参数(positional argument),但是没有得到。
当你直接在Python中运行commit_version_to_git.py
时,例如通过python commit_version_to_git.py -h
,参数-h
被传递给了main
函数,所以没有问题。
然而,当你作为一个命令行工具安装和运行kangtools_commit_version_to_git
时,你的main
函数没有得到任何参数,因为setuptools
并不知道它需要传递这些参数。
为了解决这个问题,你可以修改main
函数的定义,使其不需要任何位置参数。然后,你可以在main
函数的内部,使用argparse
或者其他方式来解析命令行参数。
例如,你可以修改你的main
函数如下:
def main():
parser = argparse.ArgumentParser(description='...')
parser.add_argument('-m', '--message', help='commit message')
args = parser.parse_args()
# the rest of your main function, using args.message, etc.
这样,当你运行kangtools_commit_version_to_git
命令时,argparse
会帮你处理命令行参数,你就不会再看到那个错误信息了。
记住,更改之后,你需要重新安装你的包以使得这些改动生效。你可以通过以下命令来完成:
python3 setup.py install
然后,你应该可以正常地使用kangtools_commit_version_to_git
命令了。
当你使用argparse
库创建命令行参数时,它会自动为你生成一个-h/--help
选项。所以,一旦你在main
函数中设置了参数解析(如我在上一个回复中的代码片段所示),你就可以使用-h
选项来获取命令的帮助信息。例如,运行kangtools_commit_version_to_git -h
会显示关于如何使用kangtools_commit_version_to_git
的帮助信息。
这是因为argparse
库默认会添加一个-h/--help
选项,用于显示命令的使用方法和你已定义的命令行参数。如果你在argparse.ArgumentParser
函数中添加了description
参数,该描述信息也会在-h/--help
的输出中显示。
--
这三行代码的作用是为Python脚本添加命令行参数。他们是利用Python的argparse
库来实现的。
下面是对这三行代码的具体分析:
-
parser = argparse.ArgumentParser(description='...')
: 这一行创建了一个ArgumentParser
对象。ArgumentParser
对象会保存所有你需要处理的命令行参数信息,以及如何处理这些参数。在这个例子中,你为ArgumentParser
对象提供了一个描述信息(description='...'
),这个描述信息会在你执行python script.py -h
时显示。 -
parser.add_argument('-m', '--message', help='commit message')
: 这一行添加了一个命令行选项参数。-m
是该选项的短格式,--message
是该选项的长格式。使用这个选项时,必须提供一个值,这个值在脚本内部可以通过args.message
来访问。help='commit message'
定义了当执行python script.py -h
时,该选项的帮助信息。 -
args = parser.parse_args()
: 这一行解析命令行参数。在这个例子中,当你执行python script.py -m "my message"
时,argparse
会解析-m "my message"
,然后你可以在脚本中通过args.message
来访问这个值(在这个例子中,args.message
的值会是"my message"
)。
当你定义一个函数,比如 def main():
,括号内部是你要给这函数传递的参数。在这个例子中,main
函数没有需要传入的参数,因此括号内为空。
然而,在 main()
函数内部,你可以定义其他的变量或者使用其他函数,这些函数可能需要参数。在你的脚本中,你使用了 argparse
库来处理命令行参数。虽然这些参数没有直接传递给 main()
函数,但是在 main()
函数内部,你调用了 argparse
的 parse_args()
函数,这个函数可以读取命令行参数并返回一个对象(在你的脚本中是 args
对象),这个对象包含了所有的命令行参数值。因此,尽管这些参数没有直接传给 main()
函数,你仍然可以在 main()
函数内部访问它们。
这样设计的目的是为了让 main()
函数可以独立于外部环境运行,使得代码更加模块化,更容易测试和复用。只有当你明确需要让函数接受外部参数时,你才在函数定义中添加参数。
我将argparse的代码移到main()函数内部,并去掉了main(args)。这是因为我们希望main()函数可以处理其自己的命令行参数,而不需要从外部传递参数。
kang@Love-Grace williampolicy.github.io$ kangtools_commit_version_to_git -h usage: kangtools_commit_version_to_git [-h] [-m COMMIT_MESSAGE]
This script handles versioning and git commit operations. The version number is stored in a text file, incremented, and then a new entry with the version number, a user-provided commit message, the user name, and the current timestamp is appended. These details are also used in the git commit message.
optional arguments:
-h, --help show this help message and exit
-m COMMIT_MESSAGE, --commit_message COMMIT_MESSAGE
Specify the commit message that will be used in both the version text file and the git commit. If not provided, the script will prompt for it.
kang@Love-Grace williampolicy.github.io$ kangtools_commit_version_to_git -m "good commit pip install kangtools. it works. Great! and with help"
[main 58179fc] V.0.7 - good commit pip install kangtools. it works. Great! and with help ©KANG - 2023-06-26
1 file changed, 1 insertion(+)
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 445 bytes | 445.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/williampolicy/williampolicy.github.io.git
3a398e3..58179fc main -> main
Committed Version V.0.7
kang@Love-Grace williampolicy.github.io$ cat version.txt
V.0.3V.0.4
V.0.4 - add function to write history in version.text
V.0.5
V.0.5 - this is a great work for upload_new_version_v1.py
V.0.5
V.0.5 - try to make a test
V.0.5
V.0.5 - try to make a test
V.0.5
V.0.5 - test more
V.0.6 - test kangtools_version_control ©KANG - 2023-06-26
V.0.7 - good commit pip install kangtools. it works. Great! and with help ©KANG - 2023-06-26
------it works.
下面我们尝试 setup 版本的自动更新。 Try.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for kangtools-0.0.15-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6702649dcd81600a3c8cec332378d626e5ae13c04da007cfcc10f208cc0c1c9a |
|
MD5 | df8aef7d5304716809fc223736091ca0 |
|
BLAKE2b-256 | 537b940418ba2a7f3c990b829fa00ac5fe7969d6b5f3892db9c3a7fe9c7e7e72 |