Python代码给出了错误,但我没有调用它?

2024-09-30 01:27:56 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我在一个名为P2_small.py的文件中的Python脚本,经过精简和简化,使其不易使用。基本上,每次调用yfinance以获取try/except构造中的数据时,它都会建立一个无限循环。当互联网关闭时,我得到下面的错误。两个可能相关的问题:

  1. 为什么stackdump没有提到我的脚本(正如您所看到的,它引用了各种.py文件,但从来没有引用过P2_small.py
  2. 为什么这个错误没有被我的try/except构造捕获

{}中的代码:

import requests
from tkinter import *
import yfinance as yf

def isInternet(): #Method to check there is a good internet connection
    url = 'http://www.google.com/'
    try:
        _ = requests.get(url, timeout=3)
        return True
    except requests.ConnectionError:
        return False

def startInfiniteLoop():
    try:
        if not isInternet():
            print("No internet connection - waiting...");
        else:
            df = yf.download(tickers="RDSB.L", period="1y", interval="1d", auto_adjust=True, prepost=False)
            if df.empty:
                print("Does not exist")
            else:
                print(df.info)

        # Call startInfiniteLoop() again and again and again, each time after 10 secs
        root.after(10000, startInfiniteLoop)
    except Exception as exception1:
        print("Error1 occured: " + str(exception1))

#Make GUI
root = Tk()
root.geometry("300x3000")

#Start infinite loop and show the GUI
root.after(100, startInfiniteLoop)
root.mainloop()

生成的错误:

Exception in thread Thread-1225:
Traceback (most recent call last):
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connection.py", line 169, in _new_conn
    conn = connection.create_connection(
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\util\connection.py", line 96, in create_connection
    raise err
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\util\connection.py", line 86, in create_connection
    sock.connect(sa)
OSError: [WinError 10065] A socket operation was attempted to an unreachable host

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen
    httplib_response = self._make_request(
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 382, in _make_request
    self._validate_conn(conn)
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 1010, in _validate_conn
    conn.connect()
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connection.py", line 353, in connect
    conn = self._new_conn()
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connection.py", line 181, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x000002518AA6B100>: Failed to establish a new connection: [WinError 10065] A socket operation was attempted to an unreachable host

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\adapters.py", line 439, in send
    resp = conn.urlopen(
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 755, in urlopen
    retries = retries.increment(
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\util\retry.py", line 574, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='query2.finance.yahoo.com', port=443): Max retries exceeded with url: /v8/finance/chart/SAF.PA?range=0d&interval=1d&includePrePost=False&events=div%2Csplits (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000002518AA6B100>: Failed to establish a new connection: [WinError 10065] A socket operation was attempted to an unreachable host'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\threading.py", line 954, in _bootstrap_inner
    self.run()
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\site-packages\multitasking\__init__.py", line 102, in _run_via_pool
    return callee(*args, **kwargs)
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\site-packages\yfinance\multi.py", line 169, in _download_one_threaded
    data = _download_one(ticker, start, end, auto_adjust, back_adjust,
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\site-packages\yfinance\multi.py", line 181, in _download_one
    return Ticker(ticker).history(period=period, interval=interval,
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\site-packages\yfinance\base.py", line 152, in history
    data = self.session.get(
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\mkemp\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='query2.finance.yahoo.com', port=443): Max retries exceeded with url: /v8/finance/chart/SAF.PA?range=0d&interval=1d&includePrePost=False&events=d

Tags: inpylibpackageslocallinesiteconnection

热门问题