首页 > 解决方案 > ImportError: 无法从 'os' 导入名称 'wait' (C:\Python38\lib\os.py)

问题描述

我正在开发一个基于用户输入抓取网站的 Python 脚本。为了练习,我正在使用一个示例网站,我从中抓取数据。我遇到的问题是我使用了 wait(10) 方法。代码如下:

from os import wait
from lxml import html, etree
import requests

def select_PA_city(self):
    pa_city_list = ["Philadelphia", "Pittsburgh", "Harrisburg", "Erie", "Scranton", "Allentown"]
    print("The major cities in Pennsylvania are Philadelphia, Pittsburgh, Harrisburg,"
          "Erie, Scranton, and Allentown.")
    city_found = False
    while not city_found:
        pa_city = input("Select a Pennsylvania city from this list:")
        for city in pa_city_list:
            if pa_city == city:
                print("You selected " + pa_city)
                city_found = True
            else:
                pass
        if city_found:
            print("That city is one of the top 6 listed.")
        else:
            print("That city (" + pa_city + "} is not in the top 6.  Please try again.")
            os.system.wait(10)
    return pa_city

现在我使用 PyCharm 社区作为我的 Python IDE,并且第一个 import 语句没有被识别。我检查了我的 c:\Python38\Lib\ 目录中的 os.py 模块,但在其中找不到等待方法。如何在另一个 Python 模块中找到实际的 wait() 方法定义,或者是否有另一个 Python 模块包含 wait() 方法?如果不是,我应该使用什么替代方法来启动 10 秒延迟?

标签: pythonoperating-systemwait

解决方案


推荐阅读