简体   繁体   English

如何解决 Windows 10 中的模块问题?

[英]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 文件上运行时,为什么会弹出这样的错误!!! 在此图像中,您可以看到没有抛出错误

我将代码保存为 lxml.py,执行时抛出错误

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.

相关问题 如何解决在 Windows 10 上安装 lightgbm gpu 的问题? - How can i solve problem with installing lightgbm gpu on windows 10? 如何使用 Pulp Python 模块解决这个 MILP 问题? - How solve this MILP problem with Pulp Python module? 类型错误:“模块”object 不可调用。 如何解决这个问题呢 - TypeError: 'module' object is not callable . how to solve this problem 如何解决问题“ModuleNotFoundError: No module named 'mac” - How to solve problem "ModuleNotFoundError: No module named 'mac" 我该如何解决这个“没有命名模块”的问题? - how can i solve this 'no module named' problem? 我在 Windows 10 中使用 python 2.7,在 juptyr 笔记本中出现名称错误,我该如何解决这个问题? - I am using python 2.7 in windows 10 and i am getting name error in juptyr notebook how can i solve this problem? 如何在 Windows 10 的 python 中安装模块 pybluez - How to install module pybluez in python for windows 10 如何在 Windows 10 中安装 pdfkit(python 模块)? - How to install pdfkit (python module) in windows 10? 如何解决在 Windows 10 上安装 dlib 的相关问题? - How to solve issue related to installation of dlib on Windows 10? 如何解决 Windows 10(Python 64 位)中的 Numpy 安装错误? - How to solve Numpy installing error in windows 10, (Python 64 bit)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM