Why does the Python runtime handle warnings this way?
Question: Here's a traceback from a project I'm working on:

    
    
    /usr/lib/python3/dist-packages/apport/report.py:13: PendingDeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
      import fnmatch, glob, traceback, errno, sys, atexit, locale, imp
    Traceback (most recent call last):
      ...
      File "./mouse16.py", line 1050, in _lit_string
        rangeof = range(self.idx.v, self.idx.v + result.span()[1])
    AttributeError: 'NoneType' object has no attribute 'span'
    

Now, there's a since-fixed bug in my code that caused the traceback itself;
whatever.

I'm interested in the first line: the `PendingDeprecationWarning` for not-my-
code. I use Ubuntu (as one can tell from `apport`'s existence in the path),
which is well-known for packaging and relying on Python for many things,
notably things like package management and bug reporting (`apport` / `ubuntu-
bug`).

[`imp` is indeed deprecated: _"Deprecated since version 3.4: The imp package
is pending deprecation in favor of
importlib."_](https://docs.python.org/3.6/library/imp.html#module-imp). My
machine runs at least Python 3.4.3+ or better and it takes time and a lot of
work to modernise and update software completely, so this warning is
understandable.

But [my program](https://github.com/catb0t/mouse16) doesn't go anywhere _near_
`imp`, `importlib` or `apport`, so my question is, why isn't a warning
deriving from `apport`'s source written to `apport`'s logs or certainly
collected by `stderr` on `apport`'s parent process?

If I had to take a guess at this, it's because the devs decided to buffer --
but never flush nor write -- `apport`'s `stderr`, and so the next time a
`python` child process on the system opens `stderr` for writing (as an error
in my program did), apport's buffered `stderr` is written too.

This isn't supported by what I (think I) know about Unix -- why would two
separate Python instances interact in this way?

* * *

Upon request, here's the best I can do for an MCVE: a list of module-level
imports.

    
    
    import readline
    import os
    import sys
    import warnings
    import types
    import typing
    

Is it because I import `warnings`? But... I still don't touch `apport`.

* * *

I think this question is more on-topic and will get better answers here on SO
than [AskUbuntu](https://askubuntu.com) or [Unix &
Linux](https://unix.stackexchange.com); flag it for migration if you feel
strongly otherwise but I think the mods will agree with me.


Answer: > so my question is, why isn't a warning deriving from apport's source written
> to apport's logs or certainly collected by stderr on apport's parent
> process?

The apport python library isn't running in a separate process here. Sure the
actual apport process is separate, but you are interacting/binding to it with
library that is local to your code/process.

Since this Python library is using a deprecated module, that is running inside
of your process, Python is correctly warning you.

As per Andrew's answer, the apport library is automatically invoked with an
uncaught exception.

