Skip to content

ZoeHancox/pytesting_and_pypi_tutorial

Repository files navigation

Tutorial for Pytesting and PyPI

Basic tutorial for testing and packaging Python code. Read below on how to work with this repository.

Tests

Check out these slides for more information (click on the image below to view the slides): PDF Slide

Getting Started

To clone this repository:

  • Open Git Bash, your Command Prompt or Powershell
  • Navigate to the directory where you want to clone this repository: cd/path/to/directory
  • Run git clone command: git clone https://github.com/ZoeHancox/pytesting_and_pypi_tutorial.git

To create a suitable environment we suggest using Anaconda:

  • Build conda environment: conda create --name tut_env python=3.8
  • Activate environment: conda activate tut_env
  • Change to your current working directory: cd path\to\pytesting_and_pypi_tutorial
  • Install requirements: pip install -r ./requirements.txt

Pytest Practice

Once you've cloned the repository and set up your IDE as desired, head to pytest_tutorial/pytesting.ipynb and work through this notebook.

TestPyPI Practice

Once you've completed the pytesting.ipynb notebook work through the steps below to create your very own passgen TestPyPI package.

  1. Create your account
  • Create a TestPyPI account here.

  • Get an API token on TestPyPI (Account Settings -> Add API Token). You can name your token how you like.

  • Rename the passgen folder to passgen_yourname.

  • Rename the name="passgen", in setup.py to name="passgen_yourname".

  1. Build your package
  • To build your package in Anaconda with your tut_env environment activated type:
    conda activate tut_env
    pip install --upgrade build
    pip install setuptools wheel twine
    python setup.py sdist bdist_wheel
    
  1. Test your code locally (optional)
  • In your Anaconda window type:

    conda deactivate tut_env
    conda create --name test_local python=3.8
    conda activate test_local
    cd path\to\your\pytesting_and_pypi_tutorial_repo
    pip install dist/passgen_yourname-0.0.1-py3-none-any.whl
    pip list # You should see you package in this list
    
    python
    
    from passgen_yourname.generate_pass import generate_password
    
    generate_password(10)
    
  1. Upload your package to TestPyPI
  • In your command window type:

      ```
      set TWINE_USERNAME=__token__
      set TWINE_PASSWORD=your_api_token_value_here
      twine upload --repository-url https://test.pypi.org/legacy/ dist/*
    
      ```
    

Note

Sometimes when uploading your dist it will say the version already exist, so use twine upload --repository-url https://test.pypi.org/legacy/ --skip-existing dist/* instead.

  • You should be able to find your package here: test.pypi.org/project/passgen_yourname/
  1. Install your TestPyPI package
  • Create a new Python environment in a new Anaconda window:

    conda create --name test_package_env python=3.8
    conda activate test_package_env
    
  • Install your package:

    Because TestPyPI is a bit more buggy than PyPI you might need to install some packages first: pip install ipykernel numpy

    pip install --index-url https://test.pypi.org/simple/ your-package-name

Important

Make sure to include the SPACE between simple/ and you-package-name

  1. Test your TestPyPI package

    python
    
    from passgen_yourname.generate_pass import generate_password
    
    generate_password(10)
    

Here are some resources I found helpful:

Writing README.md files

Getting started with writing and formatting on github

Adding a workflow status badge

Turning your repository into a package

About

Basic tutorial for testing and packaging Python code.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published