7

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

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

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"

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.

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.

from netifaces import interfaces, ifaddresses, AF_INET

ImportError: No module named netifaces

How can I solve the problem?

Community
  • 1
  • 1
Thu Ra
  • 2,013
  • 12
  • 48
  • 76

5 Answers5

10

just install netifaces

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

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

mislavcimpersak
  • 2,880
  • 1
  • 27
  • 30
5

If you are using ubuntu:

sudo apt install python3-netifaces
Sapnesh Naik
  • 11,011
  • 7
  • 63
  • 98
4

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

pip uninstall netifaces && pip install netifaces

fixed it.

Leaving this here for posterity. Use sudo if you need to.

maininformer
  • 967
  • 2
  • 17
  • 31
  • This lead me to my actual issue. I had netifaces installed multiple times. Tried to pip uninstall and it said `Not uninstalling netifaces at /usr/lib/python3/dist-packages, outside environment /usr`. Switched to root and did `apt remove python3-netifaces` then the pip works as expected – Martin Dec 01 '22 at 11:09
1

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

so you need this:

sudo pip install netifaces

or on windows install with an elevated command prompt!

Zoe
  • 27,060
  • 21
  • 118
  • 148
0

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

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.

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.

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 Flask or bottle.

Installation: pip install Flask or easy_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

Leonardo.Z
  • 9,425
  • 3
  • 35
  • 38
  • your answer is nearly correct. Actually, I want to create an application for both win and Linux. But I'm a newbie to Python. I'm coming from web (LAMP) environment. That why, I don't know how to create an application by python. If you can, pls knowledge share to me. – Thu Ra Oct 12 '13 at 09:46
  • If your want to learn/create a python web application, I suggest your choose some web frameworks like Flask (or bottle, Django), not Google App engine. – Leonardo.Z Oct 12 '13 at 09:53
  • Thank @glasslion, I'll try one of them, may be Django. But I want to create a software application not web application. – Thu Ra Oct 12 '13 at 10:01
  • I install Flask by your. And when I run python main.py, I got this **ImportError: No module named flask**. – Thu Ra Oct 12 '13 at 10:29