Python 导入错误:没有名为 netifaces 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19332554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:28:57  来源:igfitidea点击:

ImportError: No module named netifaces

pythonpython-2.7

提问by Thu Ra

I'm trying to get the IP address and MAC address of my pc's network card by python. I got some code from here

我试图通过python获取我电脑网卡的IP地址和MAC地址。我从这里得到了一些代码

I create a proj "getip". create "main.py". And I modify the code of "main.py" as follow

我创建了一个项目“getip”。创建“main.py”。我修改了“main.py”的代码如下

from netifaces import interfaces, ifaddresses, AF_INET

def ip4_addresses():
    ip_list = []
    for interface in interfaces():
        for link in ifaddresses(interface)[AF_INET]:
            ip_list.append(link['addr'])

    return ip_list

def main():
    print ip4_addresses()


if __name__ == "__main__":
    main()

and I create "app.yaml"

我创建了“app.yaml”

application: getip
version: 1
runtime: python
api_version: 1

handlers:
- url: .*
  script: main.py

and when I run the main.py at console as "python main.py", I got the ip addresses.

当我在控制台将 main.py 作为“python main.py”运行时,我得到了 IP 地址。

and when I run as "dev_appserver.py getip", the server is setup. When I browse the page as localhost:8080, the web page is white screen and I got the following error at console.

当我作为“dev_appserver.py getip”运行时,服务器已设置。当我以 localhost:8080 浏览页面时,网页是白屏,我在控制台收到以下错误。

from netifaces import interfaces, ifaddresses, AF_INET

ImportError: No module named netifaces

从 netifaces 导入接口、ifaddresses、AF_INET

导入错误:没有名为 netifaces 的模块

How can I solve the problem?

我该如何解决问题?

回答by mislavcimpersak

just install netifaces

只需安装 netifaces

pip install netifacesif you have pip installed, or download the source, unpack it run and python setup.py install

pip install netifaces如果您安装了 pip 或下载了源代码,请解压并运行 python setup.py install

warning: this will install it globally on your system, so use caution, or use virtualenv

警告:这将在您的系统上全局安装它,因此请谨慎使用,或使用virtualenv

回答by Leonardo.Z

It seems that you have installed netifacesin your local development environment. But Google App Engine does not recognize it.

看来你已经安装netifaces在你本地的开发环境中了。但是 Google App Engine 无法识别它。

If you run your script with python main.py, Python interpreter will look for your libraries in the PYTHONPATH. GAE does not follow that rule.

如果您使用 运行脚本python main.py,Python 解释器将在 PYTHONPATH 中查找您的库。GAE 不遵循该规则。

To install a library in GAE, usually you just need to put the library module directory in the root of your app path(whee the app.yaml is). But I don't think Google will allow you to install libraries that can get hardware information in their PaaS for security reasons.

要在 GAE 中安装库,通常只需将库模块目录放在应用程序路径的根目录中(即 app.yaml 所在)。但我认为出于安全原因,Google 不会允许您安装可以在其 PaaS 中获取硬件信息的库。

Updates:

更新:

Becaue you just need a web server to output the result, I recommend you to choose a simple, well documented, micro Python web framework, like Flaskor bottle.

因为你只需要一个网络服务器来输出结果,我建议你选择一个简单的、有据可查的、微型 Python 网络框架,比如FlaskBottle

Installation: pip install Flaskor easy_install Flask

安装: pip install Flaskeasy_install Flask

code:

代码:

from flask import Flask
from netifaces import interfaces, ifaddresses, AF_INET
app = Flask(__name__)

def ip4_addresses():
    ip_list = []
    for interface in interfaces():
        for link in ifaddresses(interface)[AF_INET]:
            ip_list.append(link['addr'])

    return ip_list

@app.route("/")
def main():
    return str(ip4_addresses())

if __name__ == "__main__":
    app.run()

Run: python main.py

运行:python main.py

回答by Chris gibbs

Actually, the problem here is you must be root when installed with pip, or it will not install globally. Therefore would not be able to find the module unless in the same directory or path as the module directory

其实这里的问题是pip安装时必须是root用户,否则无法全局安装。因此,除非在与模块目录相同的目录或路径中,否则将无法找到模块

so you need this:

所以你需要这个:

sudo pip install netifaces

or on windows install with an elevated command prompt!

或使用提升的命令提示符在 Windows 上安装!

回答by Sapnesh Naik

If you are using ubuntu:

如果您使用的是ubuntu

sudo apt install python3-netifaces

回答by plumSemPy

Came here for the same question but in my case pip installwould say that requirement is already satisfied. However:

来这里是为了同样的问题,但在我的情况下pip install会说要求已经得到满足。然而:

pip uninstall netifaces && pip install netifaces

pip uninstall netifaces && pip install netifaces

fixed it.

修复。

Leaving this here for posterity. Use sudoif you need to.

把这个留给子孙后代。sudo如果需要,请使用。