Question: When using __del__ datetime.date.today() throws ImportError: sys.meta_path is None, Python is likely shutting down import datetime import time import sys class Bug(object): def __init__(self): print_meta_path() def __del__(self): print_meta_path() try_date('time') try_date('datetime') def print_meta_path(): print(f'meta_path: {sys.meta_path}') def try_date(date_type): try: print('----------------------------------------------') print(date_type) if date_type == 'time': print(datetime.date.fromtimestamp(time.time())) if date_type == 'datetime': print(datetime.date.today()) except Exception as ex: print(ex) if __name__ == '__main__': print(sys.version) bug = Bug()  output with different envs (3.10, 3.9, 3.7): 3.10.4 | packaged by conda-forge | (main, Mar 24 2022, 17:39:04) [GCC 10.3.0] meta_path: [<_distutils_hack.DistutilsMetaFinder object at 0x7ff8731f6860>, <class '_frozen_importlib.BuiltinImporter'>, <class '_frozen_importlib.FrozenImporter'>, <class '_frozen_importlib_external.PathFinder'>] meta_path: None ---------------------------------------------- time 2022-05-17 ---------------------------------------------- datetime sys.meta_path is None, Python is likely shutting down  3.9.12 | packaged by conda-forge | (main, Mar 24 2022, 23:22:55) [GCC 10.3.0] meta_path: [<_distutils_hack.DistutilsMetaFinder object at 0x7fb01126e490>, <class '_frozen_importlib.BuiltinImporter'>, <class '_frozen_importlib.FrozenImporter'>, <class '_frozen_importlib_external.PathFinder'>] meta_path: None ---------------------------------------------- time 2022-05-17 ---------------------------------------------- datetime sys.meta_path is None, Python is likely shutting down  3.7.12 | packaged by conda-forge | (default, Oct 26 2021, 06:08:53) [GCC 9.4.0] meta_path: [<class '_frozen_importlib.BuiltinImporter'>, <class '_frozen_importlib.FrozenImporter'>, <class '_frozen_importlib_external.PathFinder'>] meta_path: None ---------------------------------------------- time 2022-05-17 ---------------------------------------------- datetime sys.meta_path is None, Python is likely shutting down  3.8.10 (default, Mar 15 2022, 12:22:08) [GCC 9.4.0] meta_path: [<class '_frozen_importlib.BuiltinImporter'>, <class '_frozen_importlib.FrozenImporter'>, <class '_frozen_importlib_external.PathFinder'>] meta_path: None ---------------------------------------------- time 2022-05-17 ---------------------------------------------- datetime sys.meta_path is None, Python is likely shutting down  Why is that happening? I need to use requests which use urllib3 connection.py 380: is_time_off = datetime.date.today() < RECENT_DATE  File "/home/liron/mambaforge/envs/dm-sdk-dev/lib/python3.10/site-packages/requests/api.py", line 117, in post File "/home/liron/mambaforge/envs/dm-sdk-dev/lib/python3.10/site-packages/requests/api.py", line 61, in request File "/home/liron/mambaforge/envs/dm-sdk-dev/lib/python3.10/site-packages/requests/sessions.py", line 529, in request File "/home/liron/mambaforge/envs/dm-sdk-dev/lib/python3.10/site-packages/requests/sessions.py", line 645, in send File "/home/liron/mambaforge/envs/dm-sdk-dev/lib/python3.10/site-packages/requests/adapters.py", line 440, in send File "/home/liron/mambaforge/envs/dm-sdk-dev/lib/python3.10/site-packages/urllib3/connectionpool.py", line 703, in urlopen File "/home/liron/mambaforge/envs/dm-sdk-dev/lib/python3.10/site-packages/urllib3/connectionpool.py", line 386, in _make_request File "/home/liron/mambaforge/envs/dm-sdk-dev/lib/python3.10/site-packages/urllib3/connectionpool.py", line 1040, in _validate_conn File "/home/liron/mambaforge/envs/dm-sdk-dev/lib/python3.10/site-packages/urllib3/connection.py", line 380, in connect ImportError: sys.meta_path is None, Python is likely shutting down  switching the line to 380: is_time_off = datetime.date.fromtimestamp(time.time()) < RECENT_DATE solve it. OS Linux-5.13.0-41-generic-x86_64-with-glibc2.31 urllib3 1.26.9  I already tried to rebind __del__ arguments default def __del__(self, datetime=datetime):.... Does anyone have an idea? thanks

Answer: Using atexit provide the same behavior as __del__ but works import datetime import time import sys import atexit class Bug(object): def __init__(self): print_meta_path() atexit.register(self.__close) def __close(self): print_meta_path() try_date('time') try_date('datetime') def print_meta_path(): print(f'meta_path: {sys.meta_path}') def try_date(date_type): try: print('----------------------------------------------') print(date_type) if date_type == 'time': print(datetime.date.fromtimestamp(time.time())) if date_type == 'datetime': print(datetime.date.today()) except ImportError: print('') if __name__ == '__main__': print(sys.version) bug = Bug()  output: 3.10.4 | packaged by conda-forge | (main, Mar 24 2022, 17:38:57) [GCC 10.3.0] meta_path: [<_distutils_hack.DistutilsMetaFinder object at 0x7fd912112860>, <class '_frozen_importlib.BuiltinImporter'>, <class '_frozen_importlib.FrozenImporter'>, <class '_frozen_importlib_external.PathFinder'>] meta_path: [<_distutils_hack.DistutilsMetaFinder object at 0x7fd912112860>, <class '_frozen_importlib.BuiltinImporter'>, <class '_frozen_importlib.FrozenImporter'>, <class '_frozen_importlib_external.PathFinder'>] ---------------------------------------------- time 2022-05-17 ---------------------------------------------- datetime 2022-05-17 Process finished with exit code 0