Python ImportError: sys.meta_path is None, Python is likely shutting down
Python ImportError: sys.meta_path is None, Python is likely shutting down
使用时__del__
datetime.date.today() 抛出 ImportError: sys.meta_path is None, Python is likely shutling 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()
不同环境(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
为什么会这样?
我需要使用使用 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
将线路切换到
380: is_time_off = datetime.date.fromtimestamp(time.time()) < RECENT_DATE
解决。
OS Linux-5.13.0-41-generic-x86_64-with-glibc2.31
urllib3 1.26.9
我已经尝试重新绑定 __del__
个默认参数
def __del__(self, datetime=datetime):....
有人有想法吗?谢谢
使用 atexit
提供与 __del__
相同的行为,但有效
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()
输出:
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
使用时__del__
datetime.date.today() 抛出 ImportError: sys.meta_path is None, Python is likely shutling 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()
不同环境(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
为什么会这样? 我需要使用使用 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
将线路切换到
380: is_time_off = datetime.date.fromtimestamp(time.time()) < RECENT_DATE
解决。
OS Linux-5.13.0-41-generic-x86_64-with-glibc2.31
urllib3 1.26.9
我已经尝试重新绑定 __del__
个默认参数
def __del__(self, datetime=datetime):....
有人有想法吗?谢谢
使用 atexit
提供与 __del__
相同的行为,但有效
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()
输出:
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