为什么要打包

  • 可以在任意位置运行模块和包
  • 不用输入python xxx.py,输入软件名称就可以直接运行

怎么打包

  • 使用setuptools

    打包hello.py

参考:Setuptools Integration — Click Documentation (7.x)

  1. 准备工作
    1. mkdir hello & cd hello
    2. touch setup.py hello.py
  1. setup.py内容 ```python from setuptools import setup

setup( name=’hello’, version=’0.1’, py_modules=[‘hello’], install_requires=[ ‘Click’, ], entry_points=’’’ [console_scripts] hello=hello:cli ‘’’, )

  1. 3. hello.py内容
  2. ```python
  3. import click
  4. @click.command()
  5. @click.option('-s', '--string', default='World',help="This is the thing that is greeted")
  6. @click.option('-n', '--number', default=1, help="This is the number of times to greet")
  7. @click.argument('output', type=click.File('w'), default='-',required=False)
  8. def cli(string,number,output):
  9. """This scripts greets you."""
  10. for x in range(number):
  11. click.echo(f'Hello {string}', file=output)
  1. 然后运行下面这段代码,以把该脚本添加到环境
    1. pip install --editable .
  1. 现在直接输入hello就可以运行了 ```shell $ hello —help Usage: hello [OPTIONS] [OUTPUT]

    This scripts greets you.

Options: -s, —string TEXT This is the thing that is greeted -n, —number INTEGER This is the number of times to greet —help Show this message and exit.

$ hello —string “Achuan” —number 3 ./output.txt

  1. <a name="eca0d553"></a>
  2. ## 打包pygit
  3. 尝试用python实现git操作
  4. setup.py内容
  5. ```python
  6. from setuptools import setup
  7. setup(
  8. name='pygit',
  9. version='0.1',
  10. py_modules=['pygit'],
  11. install_requires=[
  12. 'Click',
  13. ],
  14. entry_points='''
  15. [console_scripts]
  16. pygit=pygit:cli
  17. ''',
  18. )

pygit.py内容

  1. import os
  2. import click
  3. from git.cmd import Git
  4. import datetime
  5. git = Git(os.getcwd())
  6. @click.group()
  7. def cli():
  8. """
  9. git 命令行
  10. """
  11. pass
  12. @cli.command()
  13. def lazy():
  14. """
  15. 直接提交 add . commit Today
  16. """
  17. # git add
  18. cmd = ['git', 'add','.']
  19. output = git.execute(cmd)
  20. # git commit
  21. msg = ":memo:"+str(datetime.date.today())
  22. cmd = ['git', 'commit', '-m', msg]
  23. output = git.execute(cmd)
  24. # git push
  25. cmd = ['git', 'push']
  26. output = git.execute(cmd)
  27. click.secho('done',fg='green')
  28. @cli.command()
  29. def status():
  30. """
  31. 处理 status 命令
  32. """
  33. cmd = ['git', 'status']
  34. output = git.execute(cmd)
  35. click.echo(output)
  36. @cli.command()
  37. @click.argument('pathspecs', nargs=-1, metavar='[PATHSPEC]...')
  38. def add(pathspecs):
  39. """
  40. 处理 add 命令
  41. """
  42. cmd = ['git', 'add'] + list(pathspecs)
  43. output = git.execute(cmd)
  44. click.echo(output)
  45. @cli.command()
  46. @click.option('-m', 'msg')
  47. def commit(msg):
  48. """
  49. 处理 -m <msg> 命令
  50. """
  51. cmd = ['git', 'commit', '-m', msg]
  52. output = git.execute(cmd)
  53. click.echo(output)
  54. @cli.command()
  55. def push():
  56. """
  57. 处理 push 命令
  58. """
  59. cmd = ['git', 'push']
  60. output = git.execute(cmd)
  61. click.echo(output)
  62. if __name__ == '__main__':
  63. cli()

之后就能pygit lazy快速提交当日代码了

  1. $ pygit --help
  2. Usage: pygit [OPTIONS] COMMAND [ARGS]...
  3. git 命令行
  4. Options:
  5. --help Show this message and exit.
  6. Commands:
  7. add 处理 add 命令
  8. commit 处理 -m <msg> 命令
  9. lazy 直接提交 add .
  10. push 处理 push 命令
  11. status 处理 status 命令
  12. $pygit lazy