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
print(datetime.date.today())
throw this error by itself? Have you checked ifsys.meta_path
is indeed None in your Python installation? – Wimermeta_path
correctly (notNone
) in all theprint_meta_path
calls and the correctdatetime
at the end. Maybe it's platform dependent – Petrapetraccasys.meta_path
should not be None, so it is likely to be some problem with your Python installation. – Wimer