简体   繁体   English

Python 3 - 在使用 pip(非 root)安装 lxml 后获取“No module named 'lxml'”

[英]Python 3 - getting "No module named 'lxml' " after installing lxml with pip (non-root)

I just want to say, I have already seen this question at Pip is already installed: but I am getting no module named lxml and have seen the one answer about installing it as non-root, that's what I did, and that did not help me.我只想说,我已经在Pip 上看到了这个问题已经安装:但是我没有得到名为 lxml 的模块,并且看到了关于将其安装为非 root 的一个答案,这就是我所做的,但没有帮助我。

I just installed lxml, here is how I installed it:我刚刚安装了 lxml,这是我的安装方法:

[ec2-user@ip-xxx-xx-xx-xxx newslookup]$ pip install --user lxml
Collecting lxml
  Using cached https://files.pythonhosted.org/packages/89/51/a8a6cdb8a084d32dbc9bda94623dc35310ae2002be57de8702a1703c0026/lxml-4.3.3-cp27-cp27mu-manylinux1_x86_64.whl
Installing collected packages: lxml
Successfully installed lxml-4.3.3

So everything went well with the installation.所以安装一切顺利。

Here is the python script:这是python脚本:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from lxml import html
import requests
from time import sleep
import json
import argparse
from random import randint

Here is the output when I run the script:这是我运行脚本时的输出:

[ec2-user@ip-xxx-xx-xx-xxx newslookup]$ python3 nasdaq_scrape_sec.py aapl
Traceback (most recent call last):
  File "nasdaq_scrape_sec.py", line 4, in <module>
    from lxml import html
ModuleNotFoundError: No module named 'lxml'

Additionally, I can't find a wheel installation for this.此外,我找不到用于此的车轮安装。

您使用的是什么版本的python,您使用的是python2.7 安装pip else python3 您应该使用什么版本的pip3

Try upgrading.尝试升级。 pip install --upgrade lxml

In my case in windows, this happened as I had multiple python.exe instances installed.就我在 Windows 中的情况而言,这是因为我安装了多个 python.exe 实例。 Package was installed on one instance, but running the script used the other one.软件包安装在一个实例上,但运行脚本使用了另一个实例。 So you want to run the specific python.exe instance where the pip was installed.所以你想运行安装了 pip 的特定 python.exe 实例。 You can find out where it is installed by trying to install the same package twice.您可以通过尝试安装相同的软件包两次来找出它的安装位置。 Second time it gives you a message like this:第二次它会给你这样的消息:

Requirement already satisfied: lxml in c:\\users\\blahuser\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages (4.3.4)需求已经满足:lxml in c:\\users\\blahuser\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages (4.3.4)

Check this related post for more info: ModuleNotFoundError: No module named 'requests' after pip install查看此相关帖子以获取更多信息: ModuleNotFoundError: pip install 后没有名为“requests”的模块

You should be able to run successfully with a commnad line this:您应该能够使用以下命令行成功运行:

c:\\Users\\blahuser\\AppData\\Local\\Programs\\Python\\Python36-32\\python.exe c:\\test\\scripts\\blah.py c:\\Users\\blahuser\\AppData\\Local\\Programs\\Python\\Python36-32\\python.exe c:\\test\\scripts\\blah.py

I guess you can also fix the environment variables to trigger the right python.exe to start with and the issue will go away too, or even force install the pip on the default instance of python (See the link above)我想你也可以修复环境变量以触发正确的 python.exe 开始,问题也会消失,甚至强制在 python 的默认实例上安装 pip(见上面的链接)

I had a similar problem and I solved it我有一个类似的问题,我解决了它

in my code it was在我的代码中是

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

I replaced all the files with this我用这个替换了所有文件

#!/usr/bin/env python
# -*- coding: utf-8 -*-

try to remove this line maybe it will help you尝试删除这一行也许它会帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM