# Allow \`python -m json\` to work (in addition to \`python -m json.tool\`)

**URL:** <https://discuss.python.org/t/allow-python-m-json-to-work-in-addition-to-python-m-json-tool/59835>\
**Category:** Ideas\
**Created:** [August 2, 2024, 8:42pm UTC](https://discuss.python.org/t/allow-python-m-json-to-work-in-addition-to-python-m-json-tool/59835 "2024-08-02T20:42:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![trey](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/trey/32/20518_2.png) [@trey](https://discuss.python.org/u/trey)\
**Post date:** [August 2, 2024, 8:43pm UTC](https://discuss.python.org/t/allow-python-m-json-to-work-in-addition-to-python-m-json-tool/59835/1 "2024-08-02T20:43:00Z")

</div>

After writing a blog post about Python’s many CLI tools recently, I wondered why the `json` module cannot be run as a script (`python -m json.tool` works but `python -m json` does not).

I [did some digging](https://treyhunner.com/2024/08/why-does-python-m-json-not-work/) and found that this decision was made because at the time the `json` package was added, there was no way to run a package as a script (only modules could be run as scripts until Python 2.7).

I propose either:

1. Allowing `python -m json` and `python -m json.tool` to both work and update the documentation to note the use of `python -m json` _only_
2. Allowing both to work, but triggering a `DeprecationWarning` when `python -m json.tool` is used

I [made a branch](https://github.com/python/cpython/compare/main...treyhunner:cpython:json-script) to see what these changes might look like. That branch includes the `DeprecationWarning` approach in particular.

I can see a downside to the deprecation approach, if users may be piping both `stderr` and `stdout` to a process/file or if users might accidentally copy-paste the deprecation warning text. I can’t think of any downside to making both approaches work (outside of the usual “now there are 2 ways to do it”).

---

<div class="post-metadata">

**Author:** ![hugovk](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hugovk/32/14505_2.png) [@hugovk](https://discuss.python.org/u/hugovk)\
**Post date:** [August 3, 2024, 12:20pm UTC](https://discuss.python.org/t/allow-python-m-json-to-work-in-addition-to-python-m-json-tool/59835/2 "2024-08-03T12:20:27Z")

</div>

Sounds like a good idea.

I agree about making sure `python -m json.tool` still does the same thing as `python -m json` to allow a smooth transition.

I have a slight preference for option 2 (deprecation warning) over option 1 (deprecation in docs). In any case, there’s no rush to remove the old one.

> [@trey](#):
>
> I can see a downside to the deprecation approach, if users may be piping both `stderr` and `stdout` to a process/file or if users might accidentally copy-paste the deprecation warning text.

Well, `stderr` is the place for warnings and errors.

> [@trey](#):
>
> I can’t think of any downside to making both approaches work (outside of the usual “now there are 2 ways to do it”).

Two ways to do it, but only one _obvious_ way to do it: `python -m json`

---

<div class="post-metadata">

**Author:** ![ncoghlan](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ncoghlan/32/14266_2.png) [@ncoghlan](https://discuss.python.org/u/ncoghlan)\
**Post date:** [August 8, 2024, 1:32pm UTC](https://discuss.python.org/t/allow-python-m-json-to-work-in-addition-to-python-m-json-tool/59835/3 "2024-08-08T13:32:26Z")

</div>

As @trey notes, deprecation warnings in CLI tools, especially ones where you’re likely to be capturing the output for consumption elsewhere get painful fast. Keeping error output on `stderr` definitely helps, but not as much as simply not emitting the warning in the first place (e.g. I had to give up on emitting the locale coercion warnings because too many CI processes treated “text was emitted on stderr” as a failure condition).

Given we’d be keeping `json.tool` around anyway to hold the CLI implementation, there’s no real downside to keeping the `if __name__ == " __main__"` clause at the end and just having a “version changed” notice in the docs that says “CLI invocation changed to `python -m json`. Compatibility support for `python -m json.tool` will be retained indefinitely.”

---

<div class="post-metadata">

**Author:** ![trey](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/trey/32/20518_2.png) [@trey](https://discuss.python.org/u/trey)\
**Post date:** [August 9, 2024, 11:00pm UTC](https://discuss.python.org/t/allow-python-m-json-to-work-in-addition-to-python-m-json-tool/59835/4 "2024-08-09T23:00:55Z")

</div>

Thanks @hugovk and @ncoghlan for your feedback.

I have now opened [an issue for this idea](https://github.com/python/cpython/issues/122873).

---

<div class="post-metadata">

**Author:** ![hugovk](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hugovk/32/14505_2.png) [@hugovk](https://discuss.python.org/u/hugovk)\
**Post date:** [August 13, 2024, 8:49pm UTC](https://discuss.python.org/t/allow-python-m-json-to-work-in-addition-to-python-m-json-tool/59835/5 "2024-08-13T20:49:27Z")

</div>

This is now done in [python/cpython#122884](https://github.com/python/cpython/pull/122884) (by keeping `json.tool`) and will be in Python 3.14, thank you @trey!
