Day: July 25, 2018

Package your python code made simple & Fast

A mini project that create the required python packaging template folders, submit to GitHub & enable pip installation.

  1. Objectives:
      1. Upload a python project to GitHub and enable py-installable.
  2. Required Tools:
      1. Cookie Cutter–  for templating. Use pip install cookiecutter.
      2. GitHub account, Github desktop, Git shell — version control, git command line.
      3. PyPI account — for uploading to pypi so a user can just do “pip install your_project”.
  3. Steps:
      1. Cookie Cutter to set up the template directory and required folders with relevant docs and files (Readme.md, .gitignore, setup.py etc) for uploading. –> See commands section 1 below.
        • use commands in cmd prompt or Git shell  for windows (preferred Git shell if you executing additional git commands in step 2).
      2. Create a folder with same name as the directory name created in step 1 and place the relevant python codes inside.
      3. Use Git commands to upload files to GitHub. The below commands will only work if the repository is first created in your GitHub account. –> See commands section 2 below.
      4. Alternatively, you can use the GUI version for the GitHub instead of command line to submit your project to the repository.
      5. Create a .pypirc in same directory as the setup.py file. This will be used to provide the info to upload to pypi. –> See section 3
      6. Updates:
        1. Ensure setuptools and wheel are up to date and install twine
          • pip install -U setuptools wheel; pip install twine
        2. Package the code
          • python setup.py sdist bdist_wheel
        3. Upload the package
          • twine upload –repository pypi dist/*

Windows Command prompt for step 1

pip install cookiecutter
cookiecutter https://github.com/wdm0006/cookiecutter-pipproject.git
cd projectname

Git Commands for step 3

git init
git add -A
git commit -m 'first commit'
git remote add origin http://repository_url # works only if repository is created in Git. See Git commands for repository url.
git push origin master
git tag {{version}} -m 'adds the version you entered in cookiecutter as the first tag for release, change the version 0.0.1 etc'
git push --tags origin master

.pypirc contents for step 5

[distutils] # this tells distutils what package indexes you can push to
index-servers =
pypi

[pypi]
repository: https://pypi.python.org/pypi
username: {{your_username}}
password: {{your_password}}

Further notes 

  1. Most of the commands above are from Will McGinnis’ post and python packaging tutorial
  2. To create an empty file in windows for the .pypirc, use cmd echo >.pypirc
  3. Uploading to PyPI require a verfiied email address else there will be error uploading.
  4. When encounter “fatal: remote origin already exists.”. See link
  5. Basic GIT commands. See link
  6. Updates: uploading packages to pypi using twine. (link)
  7. Making changes to the code and uploading (link)

Update changes to github

git add -A
git commit -m 'whatever'
git push origin master
git tag {{version}} -m 'adds the version you entered in cookiecutter as the first tag for release, change the version 0.0.1 etc'
git push --tags origin master

Update changes to pypi

Simply upload your new code to github, create a new release, then adapt the setup.py file (new download_url — according to your new release tag, new version), then run the setup.py and the twin command again

python setup.py sdist
twine upload dist
Advertisements