imports not used in while

Viewed 59

I'm making a bot in python and it works normally, but I need a certain part of the code to be in an infinite loop, so when i put the code inside the "while True" it looks like my imports are unusable. can someone explain to me how i can solve this?´

import datetime
import os
import time
import cv2
import pytesseract
from playwright.sync_api import sync_playwright

caminho = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
data = datetime.datetime.now()
h = data.strftime("%h")
d = data.strftime("%d")
H = data.strftime("%H")
M = data.strftime("%M")
S = data.strftime("%S")
hd = h + d
t = H + M
pasta =f"C:/Kprints/{hd}"
chave = "****"

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto("*****")
    page.locator('xpath=/html/body/div/div[5]/div[2]/div[2]/div/div/div/div/div/div[19]/button[1]').click()
    time.sleep(3)
    page.locator('xpath=/html/body/div/div[5]/div[2]/div[2]/div/div/div/div/div/div[4]/input').click()
    page.fill('xpath=/html/body/div/div[5]/div[2]/div[2]/div/div/div/div/div/div[4]/input', "****")
    page.locator('xpath=/html/body/div/div[5]/div[2]/div[2]/div/div/div/div/div/div[6]/input').click()
    page.fill('xpath=/html/body/div/div[5]/div[2]/div[2]/div/div/div/div/div/div[6]/input', "****")
    page.locator('xpath=/html/body/div/div[5]/div[2]/div[2]/div/div/div/div/div/button').click()
    page.locator('xpath=/html/body/div/div[5]/div[2]/div[2]/div/div[1]/div/div/div[2]/div[1]/img[1]').click()
    page.locator('xpath=/html/body/div/div[5]/div[2]/div[2]/div/div[1]/div/div/div[2]/div[2]/div[2]/div/div[2]').click()
    page.locator('xpath=/html/body/div/div[5]/div[2]/div[2]/div/div/div/div/div/div[2]/div[5]/button').click()
    time.sleep(1)
    while True:
     page.screenshot(path=f"{pasta}/{t}.png")
    time.sleep(1)
img = cv2.imread(f"{pasta}/{t}.png")
time.sleep(1)
pytesseract.pytesseract.tesseract_cmd = caminho
texto = pytesseract.image_to_string(img)
time.sleep(1)
if not os.path.isdir(pasta):
    os.mkdir(pasta)
list = os.listdir(pasta)
for arquivo in list:
    if chave not in texto:
        os.remove(f"{pasta}/{arquivo}")
 Errors:

Unused import statement 'import os'
Unused import statement'import cv2'
Unused import statement 'import pytesseract'
This code is unreachable

2 Answers

Your analyzer is saying the code using them can't be reached because your while loop, being infinite, renders it impossible to reach any code after it. Either:

  1. The loop never ends, or
  2. The calls inside it raise an exception, that (thanks to no try blocks being involved) will bubble up (skipping the remaining code) and end the program.

Either way, as written, the code after the loop never runs, so those imports are effectively unused.

The question for you is: Do you expect/need the code after that loop to run? If so, the loop cannot be infinite and you need to change the code so it can end. If not, why is the code after the loop even there?

Those are not errors. Those should be just warnings.

Can you try re-run and post actual output here?

You code gets stuck in that loop so imports not used and yes pytesseract can not be reached.

Practice this code snippet first:

key = ""
while key != "q":
    key = input("Enter a Key: ")
    if key == "q":
        break
    print(f"You Entered: {key}")
Related