Skip to content
Advertisement

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. But when I save it as file.py and run it from using cmd, it pops import module error.

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’

I can not understand when both are running on the same python files why such Error pop!!! In this image you can see no error thrown

i saved code as lxml.py, throws error while executing

Advertisement

Answer

You’re confusing Python’s import machinery by having named your script lxml.py, the same name as the lxml package you’re importing.

Rename it to, say lxml_test_thing.py and it’ll work.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement