I am building an API client wrapper for some application. Therefore I created a Client class that will handle the requests. I was trying to send a logout request in the destructor, such that logout can not be forgotten if the program is ended.
Here is what I tried:
import requests
class Client:
def __init__(self):
# some stuff
def login(self):
requests.get(login_url_path, headers=some_headers)
def logout(self):
requests.post(logout_url_path, headers=some_headers)
def __del__(self):
self.logout()
if __name__ is '__main__':
client = Client()
client.login()
This results in:
File "/Users/me/Library/Python/3.9/lib/python/site-packages/requests/api.py", line 115, in post
File "/Users/me/Library/Python/3.9/lib/python/site-packages/requests/api.py", line 59, in request
File "/Users/me/Library/Python/3.9/lib/python/site-packages/requests/sessions.py", line 577, in request
File "/Users/me/Library/Python/3.9/lib/python/site-packages/requests/sessions.py", line 759, in merge_environment_settings
File "/Users/me/Library/Python/3.9/lib/python/site-packages/requests/utils.py", line 825, in get_environ_proxies
File "/Users/me/Library/Python/3.9/lib/python/site-packages/requests/utils.py", line 809, in should_bypass_proxies
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 2647, in proxy_bypass
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 2624, in proxy_bypass_macosx_sysconf
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 2566, in _proxy_bypass_macosx_sysconf
ImportError: sys.meta_path is None, Python is likely shutting down
The problem appears to be that some imports no longer exit when the destructor is called as python is shutting down. Is this an expected issue? Is there a work around?