PythonTA is a Python program that analyses Python code to help
students find and fix common coding and style errors. Unlike testing
libraries like doctest or pytest, PythonTA
does not actually run your code. Instead, it analyzes the program text
directly, looking for common patterns of code that oftne lead to
errors.PyCharm does something very similar, which is why
you’ll see red or yellow highlighted text in your Python files as you’re
working, before running the file.
Running PythonTA
To run PythonTA on a Python file, put the following code at the
bottom of the file you want to check:
When you run this file, you’ll see a report open up in your web
browser that shows any errors that PythonTA detected. These errors are
divided into two broad categories:
Code Errors or Forbidden Usage: you should fix these immediately, as
they point out logical errors in your code, or a part of your code that
is using some feature that is not allowed for your current
assignment.
Style or Convention Errors: these identify ways to improve the
formatting and design of your Python code. Fixing these is a lower
priority, but you should still fix them before your final
submission. Tip: In PyCharm, you can go to the menu Code ->
Reformat File… to automatically fix common style errors in your current
file.
We recommend running PythonTA regularly as you’re working on an
assignment, as it can be a useful way to check your work and improve the
quality of your code. If you’re ever stuck, try taking a break and
running PythonTA and fixing any errors it finds for you! This is a way
to develop good programming habits and style, which will come in handy
in this course (and all future courses).
Cleaning up
When you run PythonTA, it generates a new report file called
pyta_report.html in the same folder as the file you’re
checking. After you’re done running PythonTA, you can safely delete this
report file.