Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Simple Python Packaging Tips

Adina
November 13, 2019

Simple Python Packaging Tips

Adina

November 13, 2019
Tweet

More Decks by Adina

Other Decks in Programming

Transcript

  1. Outline 1. Setting up a GitHub respository 2. Accessing your

    repository locally 3. Creating basic documentation 4. Creating Build the Docs 5. Using PyPI 6. Package basics !2
  2. 1. Setting up a GitHub respository 2. Accessing your repository

    locally 3. Creating basic documentation 4. Creating Build the Docs 5. Using PyPI 6. Package basics Outline !3
  3. 1. Setting up a GitHub respository 2. Accessing your repository

    locally 3. Creating basic documentation 4. Creating Build the Docs 5. Using PyPI 6. Package basics Outline !6
  4. 2. Accessing your repository locally There are two ways to

    get your repository on your machine: 1. You can use git clone $ git clone username@host:/path/to/repository !7
  5. The shortcuts you need to know 1. Proposing changes to

    given file(s) $ git add <filename> 2. Committing your changes to the repository $ git commit -m “Commit message” 3. Sending your changes to your remote repository $ git push origin master 4. Checking if there are any updates to the code (should always be done before making changes or else you might have to deal with merge conflicts!) $ git pull 2a. Using the Git commands: Basics !9
  6. The shortcuts you need to know 1. Proposing changes to

    given file(s) $ git add <filename> 2. Committing your changes to the repository $ git commit -m “Commit message” 3. Sending your changes to your remote repository $ git push origin master 4. Checking if there are any updates to the code (should always be done before making changes or else you might have to deal with merge conflicts!) $ git pull 2a. Using the Git commands: Basics !9
  7. The shortcuts you need to know 1. Proposing changes to

    given file(s) $ git add <filename> 2. Committing your changes to the repository $ git commit -m “Commit message” 3. Sending your changes to your remote repository $ git push origin master 4. Checking if there are any updates to the code (should always be done before making changes or else you might have to deal with merge conflicts!) $ git pull 2a. Using the Git commands: Basics !9
  8. The shortcuts you need to know 1. Proposing changes to

    given file(s) $ git add <filename> 2. Committing your changes to the repository $ git commit -m “Commit message” 3. Sending your changes to your remote repository $ git push origin master 4. Checking if there are any updates to the code (should always be done before making changes or else you might have to deal with merge conflicts!) $ git pull 2a. Using the Git commands: Basics !9
  9. The shortcuts you need to know 1. Proposing changes to

    given file(s) $ git add <filename> 2. Committing your changes to the repository $ git commit -m “Commit message” 3. Sending your changes to your remote repository $ git push origin master 4. Checking if there are any updates to the code (should always be done before making changes or else you might have to deal with merge conflicts!) $ git pull 2a. Using the Git commands: Basics !9
  10. If you’re updating software that other people use, it is

    recommended to make changes in a new branch and then merge branches. 1. Creating a new branch $ git checkout -b <branch-name> 2. Pushing the branch to GitHub $ git push origin 3. Switching to a branch that was created on GitHub $ git checkout <branch-name> 2a. Using the Git commands: Branches !10
  11. If you’re updating software that other people use, it is

    recommended to make changes in a new branch and then merge branches. 1. Creating a new branch $ git checkout -b <branch-name> 2. Pushing the branch to GitHub $ git push origin 3. Switching to a branch that was created on GitHub $ git checkout <branch-name> 2a. Using the Git commands: Branches !10
  12. If you’re updating software that other people use, it is

    recommended to make changes in a new branch and then merge branches. 1. Creating a new branch $ git checkout -b <branch-name> 2. Pushing the branch to GitHub $ git push origin 3. Switching to a branch that was created on GitHub $ git checkout <branch-name> 2a. Using the Git commands: Branches !10
  13. If you’re updating software that other people use, it is

    recommended to make changes in a new branch and then merge branches. 1. Creating a new branch $ git checkout -b <branch-name> 2. Pushing the branch to GitHub $ git push origin 3. Switching to a branch that was created on GitHub $ git checkout <branch-name> 2a. Using the Git commands: Branches !10
  14. There are two ways to get your repository on your

    machine: 2. You can download the GitHub desktop app (for macOS & Windows) https://desktop.github.com/ 2. Accessing your repository locally !11
  15. 1. Setting up a GitHub respository 2. Accessing your repository

    locally 3. Creating basic documentation 4. Creating Build the Docs 5. Using PyPI 6. Package basics Outline !15
  16. 1. Setting up a GitHub respository 2. Accessing your repository

    locally 3. Creating basic documentation 4. Creating Build the Docs 5. Using PyPI 6. Package basics Outline !18
  17. !19 4. Using Read the Docs Read the Docs &

    Sphinx are documentation generating softwares that make it easy to keep your documentation up-to-date. In order to use the software, you must create a Read the Docs account and link it to your GitHub page: https://readthedocs.org/dashboard/ After importing your repository you will get a https://xx.readthedocs.io website.
  18. !20 4. Using Read the Docs In order to use

    Sphinx to then build your documentation, you need a directory in your GitHub repository called docs and within docs you simply run the command $ sphinx-quickstart To see what your documentation looks like, you can run $ make html
  19. !21 4a. Proper documentation format Sphinx reads in a very

    specific format for your documentation.
  20. !21 4a. Proper documentation format Sphinx reads in a very

    specific format for your documentation. class Source(object): ”””A single source observed by TESS. Parameters ---------- tic : int, optional The TIC ID of the source. gaia : int, optional The GAIA DR2 source_id. coords : tuple, astropy.coordinates.SkyCoord The (RA, Dec) coords of the obj. Attributes ---------- tess_mag : float The TESS magnitude from the TIC. “”” def __init__(self, tic=None, gaia=None, coords=None): self.tic = tic self.gaia = gaia self.coords = coords
  21. !21 4a. Proper documentation format Sphinx reads in a very

    specific format for your documentation. class Source(object): ”””A single source observed by TESS. Parameters ---------- tic : int, optional The TIC ID of the source. gaia : int, optional The GAIA DR2 source_id. coords : tuple, astropy.coordinates.SkyCoord The (RA, Dec) coords of the obj. Attributes ---------- tess_mag : float The TESS magnitude from the TIC. “”” def __init__(self, tic=None, gaia=None, coords=None): self.tic = tic self.gaia = gaia self.coords = coords )
  22. !22 4b. Setting up tabs: API Your api.rst file allows

    your comments to be built to to the website. These should include all of the classes in your package.
  23. !23 4b. Setting up tabs: Homepage Your index.rst file will

    serve as the homepage to your website.
  24. !23 4b. Setting up tabs: Homepage Your index.rst file will

    serve as the homepage to your website.
  25. !23 4b. Setting up tabs: Homepage Your index.rst file will

    serve as the homepage to your website.
  26. !23 4b. Setting up tabs: Homepage Your index.rst file will

    serve as the homepage to your website.
  27. !23 4b. Setting up tabs: Homepage Your index.rst file will

    serve as the homepage to your website.
  28. !24 4b. Setting up tabs: getting_started package/docs/getting_started will host additional

    documentation and tutorial Jupyter notebooks. Each tab is a different .rst or .ipynb file.
  29. !25 4b. Setting up tabs: Useful formatting guide 1. Creating

    headers ============= 2. Creating subheaders ------------------------ 3. Bullet points that are highlighted blue * <bullet-header> 4. Tabbing for inline code tab + 5 spaces
  30. !25 4b. Setting up tabs: Useful formatting guide 1. Creating

    headers ============= 2. Creating subheaders ------------------------ 3. Bullet points that are highlighted blue * <bullet-header> 4. Tabbing for inline code tab + 5 spaces
  31. !25 4b. Setting up tabs: Useful formatting guide 1. Creating

    headers ============= 2. Creating subheaders ------------------------ 3. Bullet points that are highlighted blue * <bullet-header> 4. Tabbing for inline code tab + 5 spaces
  32. !25 4b. Setting up tabs: Useful formatting guide 1. Creating

    headers ============= 2. Creating subheaders ------------------------ 3. Bullet points that are highlighted blue * <bullet-header> 4. Tabbing for inline code tab + 5 spaces
  33. !26 4b. Setting up tabs: Index Your index.rst file will

    also be where you build your index.
  34. !27 4c. Travis CI You can also set up automatic

    tests which run with every GitHub push!
  35. !28 4c. Travis CI Travis CI depends on test files

    that are located in /<package-name>/<package-name>/tests You should have tests for every class in your package. With every GitHub push, Travis will build package and run all of your tests. The progress can be tracked on https://travis-ci.org You have to make an account with Travis CI and link it to your GitHub.
  36. !29 4d. Linking docs to a personal website Your docs

    should be copied to a new branch called something like gh-pages
  37. !29 4d. Linking docs to a personal website Your docs

    should be copied to a new branch called something like gh-pages
  38. 1. Setting up a GitHub respository 2. Accessing your repository

    locally 3. Creating basic documentation 4. Creating Build the Docs 5. Using PyPI 6. Package basics Outline !30
  39. !31 5. PyPI: Making packages easy to install You know

    when you want to download a new package and you do pip install <package-name>? This is how to make your code do that! PyPI = Python Package Index https://pypi.org You have to register for an account to get a PyPI package name.
  40. !31 5. PyPI: Making packages easy to install You know

    when you want to download a new package and you do pip install <package-name>? This is how to make your code do that! PyPI = Python Package Index https://pypi.org You have to register for an account to get a PyPI package name.
  41. !31 5. PyPI: Making packages easy to install You know

    when you want to download a new package and you do pip install <package-name>? This is how to make your code do that! PyPI = Python Package Index https://pypi.org You have to register for an account to get a PyPI package name.
  42. !32 5a. Creating your setup.py file The name of the

    directory where your packaged files are
  43. !32 5a. Creating your setup.py file The name of the

    directory where your packaged files are A description for the PyPI website
  44. !33 5a. Creating your setup.py file The packages you’re creating

    with your setup file Packages yours is dependent on and any specific versions needed
  45. !34 5b. Versions Your package directory should always have a

    version.py file. This is all that’s in that file: __version__ = "0.0.1" It’s good practice to do a pre-release before every version update. Pre-releases can be pip installed by specifying the version number. Then, once you test everything you want, you can upload the new version for real. Pre-releases are specified by __version__ = “0.0.1rc1" Version numbers have to be unique and always increasing!
  46. !35 5c. Uploading to PyPI You need to install twine

    to upload first python -m pip install —user —upgrade twine To setup, you’ll need to login to your PyPI account python twine —repository-url https://test.pypi.org/legacy/ dist/* From there, every update is simple: python setup.py sdist python setup.py sdist —upload
  47. !35 5c. Uploading to PyPI You need to install twine

    to upload first python -m pip install —user —upgrade twine To setup, you’ll need to login to your PyPI account python twine —repository-url https://test.pypi.org/legacy/ dist/* From there, every update is simple: python setup.py sdist python setup.py sdist —upload
  48. !35 5c. Uploading to PyPI You need to install twine

    to upload first python -m pip install —user —upgrade twine To setup, you’ll need to login to your PyPI account python twine —repository-url https://test.pypi.org/legacy/ dist/* From there, every update is simple: python setup.py sdist python setup.py sdist —upload
  49. 1. Setting up a GitHub respository 2. Accessing your repository

    locally 3. Creating basic documentation 4. Creating Build the Docs 5. Using PyPI 6. Package basics Outline !36
  50. !37 6a. Files every package should have version.py This will

    keep track of the version you’re on. __version__ = “0.0.1” This will need to be manually updated before each PyPI update.
  51. !38 6a. Files every package should have __init__.py This will

    load in all of your classes to be easily called import os PACKAGEDIR = os.path.abspath(os.path.dirname(__file__)) from .version import __version__ from .targetdata import * from .source import *
  52. !38 6a. Files every package should have __init__.py This will

    load in all of your classes to be easily called import os PACKAGEDIR = os.path.abspath(os.path.dirname(__file__)) from .version import __version__ from .targetdata import * from .source import *
  53. !39 6a. Files every package should have from .source import

    * Imports package path Imports classes/functions from other files in <package>
  54. !39 6a. Files every package should have from .source import

    * Imports package path Imports classes/functions from other files in <package> Identifies classes/ functions to import in *