[英]How to solve module problem in Windows 10?
I have a code as below.我有一个代码如下。
I run this code using python 3.7
idle, which runs successfully.我使用
python 3.7
idle 运行此代码,该代码运行成功。 But when I save it as file.py
and run it from using cmd, it pops import module
error.但是当我将它保存为
file.py
并使用 cmd 运行它时,它会弹出import module
错误。
My code:我的代码:
import requests
from lxml import html
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.3'}
# url to scrape data from
link = 'https://www.bhaskar.com/sports/'
# path to particular element
path = '//*[@id="top-nav1"]'
response = requests.get(link,headers)
byte_string = response.content
# get filtered source code
source_code = html.fromstring(byte_string)
print(source_code)
# jump to preferred html element
tree = source_code.xpath(path)
print(tree.text_content())
Error: cannot import name 'html' from 'lxml'
错误:无法从“lxml”导入名称“html”
I can not understand when both are running on the same python files why such Error pop!!!我不明白当两者都在同一个 python 文件上运行时,为什么会弹出这样的错误!!!
You're confusing Python's import machinery by having named your script lxml.py
, the same name as the lxml
package you're importing.您通过将脚本命名为
lxml.py
来混淆 Python 的导入机制,该脚本与您正在导入的lxml
package 同名。
Rename it to, say lxml_test_thing.py
and it'll work.将其重命名为
lxml_test_thing.py
即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.