Guest User

Untitled

a guest
Nov 15th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. # Python Get Started Guide
  2. This is a minimal introduction to a python package setup, describing some technologies and available method to achieve the *get started* goal.
  3.  
  4. **PyPA** is the Python Package Authority.
  5.  
  6. **PyPUG** is the Python Package User Guide.
  7.  
  8. A python **module** is the code saved in a file.
  9.  
  10. There are different type of **packages**:
  11. - (Import) Package, i.e., a namespace.
  12. - (Distrubition) Package which is a shareable/installable package (source vs build).
  13.  
  14. The **distribution** can be:
  15. - source distrubtion: sdist
  16. - build distribution:
  17. * bdist_egg (deprecated),
  18. * bdist_whell
  19.  
  20. **Wheel** (distribution format) can be:
  21. - Unversal = file installable and usable by python: no compilation, python read code.
  22. - Pure Python = python 2.x or 3.x specific, even this is only code.
  23. - Platform = Linux, Windows.
  24.  
  25. **PyPI** is the Python Packages Index: anyone can upload a package to it. Note that this is not pypy (JIT - Just In Time (compilation) - implementation for python)
  26.  
  27. ## Package Components
  28. ```
  29. root
  30. |-- data
  31. | |-- data_file
  32. |-- DESCRIPTION.rts
  33. |-- MANIFEST.in
  34. |-- README.rts
  35. |-- sample
  36. | |-- __init__.py
  37. | |-- package_data.dat
  38. |-- setup.cfg
  39. |-- setup.py
  40. |-- tests
  41. |-- __init__.py
  42. |-- simple_test.py
  43. ```
  44.  
  45.  
  46. ## Code
  47.  
  48. ### setup.py
  49. Contains information about package.
  50. It is an executable to complete package tast (build, upload...).
  51.  
  52. ```
  53. from setuptools import setup, find_packages
  54.  
  55. setup(
  56. name='',
  57. version='' # PEP440
  58. descrition=
  59. long_description
  60. url
  61. author
  62. author_email
  63. license='MIT'
  64. classifier=[
  65. 'Devlop Status :: 3 - Alpha',
  66. 'Indented Audience :: Developers',
  67. 'License :: OSI Approved :: MIT License',
  68. 'Programming Language :: Python :: 3.7'
  69. ],
  70. keywords
  71. packages=find_packages(exclude=['docs','tests*'] # used to install all containded in the package directory, excluding only some files.
  72. install_requires=['requests'], # list dependencies, can even specify version...
  73. package_data={ # ship extra data with package (not python code but needed), package use this data.
  74. 'sample': ['package_data.dat']
  75. }
  76. data_files=None
  77. # scripts = , # should be ignored.
  78. entry_point={
  79. 'console_scripts': [ # defines script available in path.
  80. 'hello=blabla:say_hello', # create a wrapper executable for say_hello.
  81. ]
  82. }
  83. )
  84. ```
  85.  
  86. It is platform indipendet.
  87.  
  88. ### setup.cfg
  89. Configuration for wheel.
  90.  
  91. ### MANIFEST.in
  92. Has a listing of anything in package that is needed by it.
  93.  
  94. ### README.rst
  95. Used as index page.
  96. Can be used in setup.py.
  97.  
  98. ### DESCRIPTION.rts
  99. Description for PyPI listing.
  100.  
  101. ### LICENSE
  102. Not a secret.
  103.  
  104. ### Tests
  105. `./tests`
  106.  
  107. Uses tox (`tox.ini`) as test runner. It runs tests in multiple env.
  108.  
  109. ### Docs
  110. `./docs`
  111.  
  112. ### CI
  113. `.travis.yml`
  114. Travis is the most famous service for continuos integration. It can be integrate in GitHub. It allows to specify in what env run tests.
  115.  
  116. ### requirements.txt
  117. List all the requirements needed from the package. Can be useful to install all the package requirements.
  118.  
  119. ### .gitignore
  120. Not a secret.
  121.  
  122. ### Other files
  123. - `HISTORY.rst`
  124. - `CHANGES.rst` or `CHANGELOG.rst`
  125. - `CONTRIBUTING.rst`
  126. - `AUTHORS.rst`
  127.  
  128.  
  129. ## Setup
  130.  
  131. ### Requirements
  132. ```
  133. pip install whell
  134. pip install twine
  135. pip install tox
  136. ```
  137.  
  138. ### Boilerplate? CookieCutter
  139. `pip install cookiecutter`
  140.  
  141. Allows to download preconfigured repo, setup and modify it.
  142.  
  143. ### Virtualenv
  144. From virtualenvwrapper
  145. ```
  146. mkvirtualenv abc
  147. ```
  148.  
  149. ### Run cookoue cutter
  150. ```
  151. cookiecutter git_url
  152. ...
  153. configuration questions from cookiecutter...
  154. ```
  155.  
  156. ### Git init
  157. Setup your git repo.
  158.  
  159. ### Add code
  160. ```
  161. ...
  162.  
  163. def say_hello(): ...
  164. def hello(): ...
  165. ```
  166.  
  167. ### Add tests
  168. Code the unit tests.
  169.  
  170. ### Run test
  171. Tests can be run with *tox*.
  172.  
  173. `tox.ini` is automatically created by cookiecutter, tox read the ini files and execute the tests.
  174. Note: tox creates venv on the fly and run tests in it.
  175.  
  176.  
  177. # Services
  178.  
  179. - git: github, bitbucket...
  180. - Continuos integration:
  181. ```
  182. add repo to travis-ci.org
  183. click sync button
  184. ```
  185. - git push
  186. * triggers travis-ci
  187. * tox run tests (e.g. 2.7, 3.3, pypy)
  188. - read the docs
  189. * link repo to read the docs
  190. * generate documentation (scheleton generated by cookiecutter)
  191. - PyPI
  192. * create account (can be done py setup.py but it does not use ssl)
  193. * save PyPI setting in `$HOME/.pypirc`
  194. * the file is:
  195. ```
  196. [distutils]
  197. index-server=pypi
  198.  
  199. [pypi]
  200. repository = pypy.python.org/pypi
  201. username = ...
  202. password = ...
  203. ```
  204.  
  205. ### Build
  206. With PyPI
  207. ```
  208. ./setup.py sdist
  209. ./setup.py bdest_wheel
  210. ```
  211.  
  212. ### Sign distribution
  213. ```
  214. gpg --detach-sign -a
  215. ```
  216.  
  217. ### Register pacage
  218.  
  219. ### Upload
  220. ```
  221. twine upload dist/*
  222. ```
  223.  
  224. ### Develop mode
  225. Allows to distribute package and on PyPI but relate the local distribution version to the local source version. In order to do this python creates a symlink to source file in install directory.
  226.  
  227. You can use package immediatly after updates.
  228.  
  229. ```
  230. ./setup.py develop
  231. or
  232. pip install -e
  233. ```
  234.  
  235. ### Python versioneer
  236. GitHub project.
  237. Dynamically manage version based on git tag. If you do not specify tag in git, it automatically takes the git hash and handle it in package version.
  238.  
  239.  
  240. ## Other tips
  241.  
  242. ### Closed source?
  243. - jankings ??? continuos integration
  244. - githash ??? intesad of github
  245. - dev py -> run pyPI internally -> local cache, repo of our stuff
  246.  
  247. ### RPM for python tingh
  248. There are some tools: they are not automated, but exists, so...
Add Comment
Please, Sign In to add comment