Python ImportError: sys.meta_path is None, Python is likely shutting down
Asked Answered
P

1

8

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

Phira answered 14/5, 2022 at 8:18 Comment(8)
Seems related to #69193828Andrew
Thanks, this does not solve my problem though.Phira
@Phira can you post more detailed code? Since I cannot reproduce the error in my system (Windows, Python 3.9). Or maybe it's due to Python 3.10Petrapetracca
So does print(datetime.date.today()) throw this error by itself? Have you checked if sys.meta_path is indeed None in your Python installation?Wimer
I've edited the question. added reproduce script and outputs from different envs @KhanhLuongPhira
Yes, it is None. I've added prints and script output @KlasŠ.Phira
For me on Windows 11 64 bit, python 3.9.6, it's still printing the meta_path correctly (not None) in all the print_meta_path calls and the correct datetime at the end. Maybe it's platform dependentPetrapetracca
I would say that sys.meta_path should not be None, so it is likely to be some problem with your Python installation.Wimer
P
3

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
Phira answered 17/5, 2022 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.