Skip to main content

chkpkg

Checks a Python package intended to be published on PyPi:

  • can we build a .whl distribution from it?
  • сan we install a package from the newly built .whl?
  • can we import the installed package into the code?

Thus, we test the correctness of setup.py or setup.cfg.

chkpkg can be used as part of CI pipeline. The check can be run from a .py script, which is as cross-platform as Python itself.


chkpkg supports Python 3.6+ on Linux, macOS and Windows.

Install

pip3 install chkpkg

Use

from chkpkg import Package

with Package() as pkg:
    pkg.run_python_code('import mypackage; mypackage.myfunc()')
    pkg.run_shell_code('mypackage_cli --version')
    
print("Package is OK!")

This test script creates a distribution from project sources, installs the package from the distribution into a virtual environment, tries importing and running the installed package from python and command line.

If any results in an error, an exception is thrown. The absence of exceptions means that the package is fine.

By default, we assume that the setup.py or setup.cfg is located in the current working directory. You can specify a different path using the argument Package(project_dir=...)

Steps

Without context, the test script would look like this:

from chkpkg import Package

pkg = Package()

try:
    # step 1
    pkg.init()
    
    # step 2   
    pkg.run_python_code('import mypackage; mypackage.myfunc()')
    pkg.run_shell_code('mypackage_cli --version')

finally:
    # step 3
    pkg.cleanup()    

Step 1: Build, Verify, Install

pkg.init()

The init method:

  • Creates a temporary virtual environment capable of building .whl files
    • Creates a distribution as a .whl file (python -m build)
    • Verifies the package source (twine check --strict)
  • Creates another temporary virtual environment without preinstalled packages
    • Installs the package from the newly created .whl into the clean virtual environment

Step 2: Import, Run

pkg.run_python_code('import mypackage')

The run_python_code method allows you to check that the package is installed and can be imported without errors.

You can also run some functions from the imported package and check the output.

output = pkg.run_python_code('import mypackage; print(mypackage.plus(2, 3))')
assert output == "5"

If the package must be installed as a CLI program, this can be tested with the run_shell_code. This function calls cmd.exe on Windows and bash on other systems.

output = pkg.run_shell_code('mypackage_cli --version')
assert output[0].isdigit()

The current working directory will be a random temporary one. If mypackage_cli can be run, then it really is available as a shell command from any directory.

However, such tests are best done on a clean system. If we run the tests on development machine, it may turn out that we are running a command that was in the system before the package was installed.

Step 3: Cleanup

pkg.cleanup()

The cleanup method removes all temporary directories created during building and testing.

License

Copyright © 2021 Arteom iG. Released under the MIT License.

Metadata

Release files for chkpkg 0.5.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chkpkg 0.5.2
File Size Uploaded
chkpkg-0.5.2.tar.gz 10.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for chkpkg 0.5.2
File Interpreter ABI Platform
chkpkg-0.5.2-py3-none-any.whl Python 3 none any Details

Total release size: 20.9 kB

Release files / chkpkg-0.5.2.tar.gz

Download URL chkpkg-0.5.2.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
73b038752b725d499f378edd5235b6c6d15c039d34e5232bd80663b20d5247bc
BLAKE2b-256 checksum
How to use checksums
55e1cb1967c83f65ee387c12dfcbb495804200fe20067c7ae2ab9821d6a48b3c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.8

Release files / chkpkg-0.5.2-py3-none-any.whl

Download URL chkpkg-0.5.2-py3-none-any.whl
Size 10.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
022eec53aec374b8423eeb9f303a3cf887c9dab85827600457d211ebc46af5b5
BLAKE2b-256 checksum
How to use checksums
eb97947611a9ffc776e871439814c2e244a680f3399c8237917f3aa1da98ad2d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.8

Release history Release notifications | RSS feed

This release

0.5.2 This release

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page