PYTHON中易遇到的一些问题的整理与解答1
在学习入门级编程的时候,相信很多朋友会遇到各种报错问题,往往很简单的问题,却能困扰我们几个小时的时间,慢慢消磨我们学习的积极性,因此我在这里以我个人的经验定期收集一些报错信息以及解决办法,希望可以帮助到你。
代码报错问题
问题 1:selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression “//[@id=“layoutMain”]/div[1]/div[2]/div/div/div/header/div/div[1]/button[1]/text()” is: [object Text]. It should be an element.
执行程序的时候,发现正常获取的属性,总是报错,查看报错信息,显示应该为selenium对象现在却是Text对象,简单来说就是之前的属性定位想直接获取text,但是selenium不支持,因此可以通过以下方式改正。
#此条语句报错:
b=driver.find_element(By.XPATH,'//*[@id="layoutMain"]/div[1]/div[2]/div/div/div/header/div/div[1]/button[1]/text()')
print(b)
#修正:
b=driver.find_element(By.XPATH,'//*[@id="layoutMain"]/div[1]/div[2]/div/div/div/header/div/div[1]/button[1]').text
print(b)
#输出正常值为"目录",问题解决
问题 2:AttributeError: partially initialized module ‘ddddocr’ has no attribute ‘DdddOcr’ (most likely due to a circular import). Did you mean: ‘ddddocr’?
当在运行程序时,发现报一下错误,明明记得几天前还是正常运行的,但是今天就是报错,很迷茫,后来分析了一下报错信息,从中发现端倪:
D:\pythonProject\venv\Scripts\python.exe D:\pythonProject\venv\practice.py
Traceback (most recent call last):
File "D:\pythonProject\venv\practice.py", line 6, in <module>
import ddddocr
File "D:\pythonProject\venv\ddddocr.py", line 3, in <module>
ocr = ddddocr.DdddOcr()
AttributeError: partially initialized module 'ddddocr' has no attribute 'DdddOcr' (most likely due to a circular import). Did you mean: 'ddddocr'?
Process finished with exit code 1
报错显示说,循环导入了这个ddddocr的包,我确定包是存在的,但怎么还会有重复的呢,最后发现原来是我手贱,将之前的一个.py文件改成了ddddocr的名称,导致出现引用报错,只需把这个文件名更改即可。
问题 3:ERROR: THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE. If you have updated the package versions, please update the hashes. Otherwise, examine the package contents carefully; someone may have tampered with them.
在下载Pyqt5的时候,遇到如下报错,大致翻译过来的意思就是说这些包与需求文件中的哈希值不匹配,如果已更新包版本,请更新哈希值,否则请仔细检查包装内容物,可能有人篡改他们。导致这样的原因有可能是pip源是国外的,所以下载网络不好导致,这我们就不用管了,先考虑怎么去解决他。
ERROR: THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE. If you have updated the package versions, please update the hashes. Otherwise, examine the package contents carefully; someone may have tampered with them.
PyQt5-Qt5>=5.15.2 from https://pypi.tuna.tsinghua.edu.cn/packages/37/97/5d3b222b924fa2ed4c2488925155cd0b03fd5d09ee1cfcf7c553c11c9f66/PyQt5_Qt5-5.15.2-py3-none-win_amd64.whl#sha256=750b78e4dba6bdf1607febedc08738e318ea09e9b10aea9ff0d73073f11f6962 (from Pyqt5):
Expected sha256 750b78e4dba6bdf1607febedc08738e318ea09e9b10aea9ff0d73073f11f6962
Got 07a3af8fbdcc4d2f3d57ac6bfd51e45f56741f4e24c2297644cadb4c5c7e1543
这种情况,我们可以通过以下命令帮我们解决:--upgrade
表示将包更新到可用的版本上。
C:\Users\Test>pip install --upgrade Pyqt5
执行后问题解决: