[关闭]
@songying 2018-07-18T20:20:49.000000Z 字数 2833 阅读 1176

setup.py -- 一个优雅的模板

Git


目录结构

  1. - setup.py
  2. -- mypackage
  3. -- __init__.py
  4. -- __version__.py
  5. -- core.py
  6. -- LICENSE
  7. -- MANIFEST.in
  8. -- README.md
  9. -- setup.py

setup.py

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Note: To use the 'upload' functionality of this file, you must:
  4. # $ pip install twine
  5. import io
  6. import os
  7. import sys
  8. from shutil import rmtree
  9. from setuptools import find_packages, setup, Command
  10. # Package meta-data.
  11. """ 基本信息"""
  12. NAME = 'mypackage'
  13. DESCRIPTION = 'My short description for my project.'
  14. URL = 'https://github.com/me/myproject'
  15. EMAIL = 'me@example.com'
  16. AUTHOR = 'Awesome Soul'
  17. REQUIRES_PYTHON = '>=3.6.0'
  18. VERSION = None
  19. # What packages are required for this module to be executed?
  20. REQUIRED = [
  21. # 'requests', 'maya', 'records',
  22. ]
  23. """The rest you shouldn't have to touch too much :)"""
  24. # ------------------------------------------------
  25. # Except, perhaps the License and Trove Classifiers!
  26. # If you do change the License, remember to change the Trove Classifier for that!
  27. here = os.path.abspath(os.path.dirname(__file__))
  28. # 导入README.md文件并作为描述
  29. # Note: this will only work if 'README.md' is present in your MANIFEST.in file!
  30. with io.open(os.path.join(here, 'README.md'), encoding='utf-8') as f:
  31. long_description = '\n' + f.read()
  32. # Load the package's __version__.py module as a dictionary.
  33. about = {}
  34. if not VERSION:
  35. with open(os.path.join(here, NAME, '__version__.py')) as f:
  36. exec(f.read(), about)
  37. else:
  38. about['__version__'] = VERSION
  39. class UploadCommand(Command):
  40. """Support setup.py upload."""
  41. description = 'Build and publish the package.'
  42. user_options = []
  43. @staticmethod
  44. def status(s):
  45. """Prints things in bold."""
  46. print('\033[1m{0}\033[0m'.format(s))
  47. def initialize_options(self):
  48. pass
  49. def finalize_options(self):
  50. pass
  51. def run(self):
  52. try:
  53. self.status('Removing previous builds…')
  54. rmtree(os.path.join(here, 'dist'))
  55. except OSError:
  56. pass
  57. self.status('Building Source and Wheel (universal) distribution…')
  58. os.system('{0} setup.py sdist bdist_wheel --universal'.format(sys.executable))
  59. self.status('Uploading the package to PyPi via Twine…')
  60. os.system('twine upload dist/*')
  61. self.status('Pushing git tags…')
  62. os.system('git tag v{0}'.format(about['__version__']))
  63. os.system('git push --tags')
  64. sys.exit()
  65. # Where the magic happens:
  66. setup(
  67. name=NAME,
  68. version=about['__version__'],
  69. description=DESCRIPTION,
  70. long_description=long_description,
  71. long_description_content_type='text/markdown'
  72. author=AUTHOR,
  73. author_email=EMAIL,
  74. python_requires=REQUIRES_PYTHON,
  75. url=URL,
  76. packages=find_packages(exclude=('tests',)),
  77. # If your package is a single module, use this instead of 'packages':
  78. # py_modules=['mypackage'],
  79. # entry_points={
  80. # 'console_scripts': ['mycli=mymodule:cli'],
  81. # },
  82. install_requires=REQUIRED,
  83. include_package_data=True,
  84. license='MIT',
  85. classifiers=[
  86. # Trove classifiers
  87. # Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers
  88. 'License :: OSI Approved :: MIT License',
  89. 'Programming Language :: Python',
  90. 'Programming Language :: Python :: 3',
  91. 'Programming Language :: Python :: 3.6',
  92. 'Programming Language :: Python :: Implementation :: CPython',
  93. 'Programming Language :: Python :: Implementation :: PyPy'
  94. ],
  95. # $ setup.py publish support.
  96. cmdclass={
  97. 'upload': UploadCommand,
  98. },
  99. )
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注