Skip to main content

A Python package for version control.

Project description

kangtools

kangtools

  • git remote
  • renew verison.
  • upload add date.

创建并发布自己的Python包到PyPI需要以下步骤:

  1. 项目结构:在你的项目目录下创建以下文件和目录结构:

    your_project/
    ├── your_package/
    │   ├── __init__.py
    │   ├── your_module.py  # 将你的版本控制代码放在这个文件里
    ├── setup.py
    ├── README.md
    ├── LICENSE
    

    其中,your_package 是你的Python包的名字,your_module 是模块的名字。__init__.py 是一个必需的空文件,它告诉Python这个目录是一个包。

  2. 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格式
    )
    
  3. README.mdLICENSE 文件README.md 文件包含了你的包的详细信息和使用方法。LICENSE 文件包含了你的包的许可证信息。

  4. 注册PyPI账户:访问 https://pypi.org/ 并注册一个账户。

  5. 安装工具:使用pip安装 twinewheelsetuptools,这些工具用于构建和上传你的包:

    pip install twine wheel setuptools
    
  6. 构建包:在你的项目目录下运行以下命令,它会创建 dist/ 目录,并在该目录下生成你的包的源码和wheel格式的发行版本:

    python setup.py sdist bdist_wheel
    
  7. 上传包:使用 twine 将你的包上传到PyPI:

    twine upload dist/*
    

    这时你需要输入你的PyPI账户的用户名和密码。

  8. 安装包:现在你就可以使用pip从PyPI安装你的包了:

    pip install your-package
    

    你需要将 your-package 替换为你的包的名字。

以上就是创建并发布Python包到PyPI的基本流程。请注意,每次发布新的版本时,需要在 setup.py 文件中更新版本号,然后重新

pip install twine wheel setuptools

您可以将 version_control.py 作为一个命令行工具来使用,有两种方法可以实现这一点。

  1. 在python文件中添加shebang行: 在你的 version_control.py 文件的最顶端添加一行 #!/usr/bin/env python3,这是一个指定解释器的shebang。然后你需要给这个文件添加执行权限,你可以使用 chmod +x version_control.py 命令来做这件事。这样你就可以像一个脚本一样运行它了,例如 ./version_control.py

  2. 将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.pymain函数中定义了一个名为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库来实现的。

下面是对这三行代码的具体分析:

  1. parser = argparse.ArgumentParser(description='...'): 这一行创建了一个ArgumentParser对象。ArgumentParser对象会保存所有你需要处理的命令行参数信息,以及如何处理这些参数。在这个例子中,你为ArgumentParser对象提供了一个描述信息(description='...'),这个描述信息会在你执行python script.py -h时显示。

  2. parser.add_argument('-m', '--message', help='commit message'): 这一行添加了一个命令行选项参数。-m是该选项的短格式,--message是该选项的长格式。使用这个选项时,必须提供一个值,这个值在脚本内部可以通过args.message来访问。help='commit message'定义了当执行python script.py -h时,该选项的帮助信息。

  3. 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() 函数内部,你调用了 argparseparse_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. ----learn from /Users/kang/1.live_wit_GPT4/code_project/demand_forcast_retail/release_pypi/clear_clean_go_upgrage

更改一个我们清晰的名字-->

upgrade_pypi_package_pip_install

kang@Love-Grace code_process$ cat creat_blog_post_git_commit_push.py #!/usr/bin/env python3

这个命令很好。我们当做 将 .md 发布blog .try.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kangtools-0.0.20.tar.gz (13.4 kB view hashes)

Uploaded Source

Built Distribution

kangtools-0.0.20-py3-none-any.whl (11.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page