ImportError: No module named netifaces - python

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?

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

If you are using ubuntu:
sudo apt install python3-netifaces

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.

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!

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

Related

Can't connect python to looker

I've downloaded the looker_sdk for python.
Wrote a very simple program:
from looker_sdk import client, models
def test_looker():
sdk = client.setup("./looker.ini")
if __name__ == "__main__":
test_looker()
However, when I'm running it I'm getting the error:
ImportError: cannot import name 'client' from 'looker_sdk'.
I do see the models and was able to perform:
sdk = looker_sdk.init31()
what am I missing?
Thanks,
Nir.
It seems there may be a missing client.py file in looker_sdk version 0.1.3b8, or in your installation - I tested this on 0.1.3b4 and found no such issue.
I recommend that you uninstall the package and re-install the latest version from PyPI.

How to restart windows service through Python script?

I am creating a script to restart windows service through Python script but getting the below error:
Script error
Code:
import win32serviceutil
class serviceRestart:
serviceName = "MySQL57"
win32serviceutil.RestartService(serviceName)
if __name__ == '__main__':
lm = serviceRestart()
The error message pretty much says it all. You need to install the module.
http://sourceforge.net/projects/pywin32/files/pywin32/
If you have pip installed then you can use "pip install pypiwin32"

How to resolve cannot import name pages (django/wagtail)

Recently I have been trying to get wagtail to work with my existing Django application. I was experiencing an error: -
ImportError: No module named wagtail
unable to load app 0 (mountpoint='') (callable not found or import error)
After much troubleshooting I managed to fix this, by copying the folder wagtail from:
/usr/local/lib/python2.7/dist-packages/
into here
/opt/django/src/
Having resolved this error, I received another about a different module, and another... each time I copied the folder from /usr/local/lib/python2.7/dist-packages/ into /opt/django/src/ and it eventually resolved the issues I was having and uWSGI started.
Now when I access the homepage of my app, I receive this error
ImportError at /
cannot import name pages
Request Method: GET
Request URL: http://example.com
Django Version: 1.9
Exception Type: ImportError
Exception Value:
cannot import name pages
Exception Location: ./wagtail/wagtailadmin/urls/__init__.py in <module>, line 4
Python Executable: /usr/local/bin/uwsgi
Python Version: 2.7.3
Python Path:
['.',
'',
'/opt/django/src',
'/root/.python',
'/opt/django/env/lib/python2.7',
'/opt/django/env/lib/python2.7/plat-linux2',
'/opt/django/env/lib/python2.7/lib-tk',
'/opt/django/env/lib/python2.7/lib-old',
'/opt/django/env/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk',
'/opt/django/env/local/lib/python2.7/site-packages',
'/opt/django/env/lib/python2.7/site-packages']
I have checked the file referenced (./wagtail/wagtailadmin/urls/__init__.py) and it looks like this:
from django.conf.urls import url, include
from django.views.decorators.cache import cache_control
from wagtail.wagtailadmin.urls import pages as wagtailadmin_pages_urls
from wagtail.wagtailadmin.urls import collections as wagtailadmin_collections_urls
from wagtail.wagtailadmin.urls import password_reset as wagtailadmin_password_reset_urls
from wagtail.wagtailadmin.views import account, chooser, home, pages, tags, userbar
from wagtail.wagtailadmin.api import urls as api_urls
from wagtail.wagtailcore import hooks
from wagtail.utils.urlpatterns import decorate_urlpatterns
from wagtail.wagtailadmin.decorators import require_admin_access
The offending line is the first wagtail 'from' ...
from wagtail.wagtailadmin.urls import pages as wagtailadmin_pages_urls
How can I resolve this error?
I've ran a few tests on wagtail setup. I made each test in a fresh Ubuntu 14 install (each test in a new workspace on Cloud9 IDE).
Test 1: Straight python 2.7
sudo pip install wagtail
ERRO: error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
Comment: Lots of x86_64-linux-gnu-gcc: error: build/temp.linux-x86_64-2.7/libImaging/codec_fd.o: No such file or directory errors were output.
Didn't work.
Test 2 - Python 2.7 with virtualenv
sudo pip install virtualenv
virtualenv env
source env/bin/activate
Now from their github https://github.com/wagtail/wagtail
pip install wagtail
out: Successfully installed Django-1.10.7 Pillow-4.1.1 Unidecode-0.4.20 Willow-0.4 beautifulsoup4-4.5.3 django-modelcluster-3.1 django-taggit-0.22.1 django-treebeard-4.1.0 djangorestframework-3.6.2 html5lib-0.999999999 olefile-0.44 pytz-2017.2 requests-2.13.0 wagtail-1.9.1 webencodings-0.5.1
wagtail start mysite
cd mysite
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
I had to edit the settings/dev.py and include ALLOWED_HOSTS = ['*'] because I'm on cloud 9. Also needed the $IP:$PORT after runserver. WORKED!
Test3 - Python 3.4 with virtualenv
sudo pip3 install virtualenv
virtualenv -p python3 env
source env/bin/activate
pip install wagtail
Successfully installed Django-1.10.7 Pillow-4.1.1 Unidecode-0.4.20 Willow-0.4 beautifulsoup4-4.5.3 django-modelcluster-3.1 django-taggit-0.22.1 django-treebeard-4.1.0 djangorestframework-3.6.2 html5lib-0.999999999 olefile-0.44 pytz-2017.2 requests-2.13.0 wagtail-1.9.1 webencodings-0.5.1
Rest of the command are the same. And it WORKED!
Test 4 - Python 3.4 no virtualenv
sudo pip3 install wagtail
out: Successfully installed wagtail django-taggit requests Unidecode Django djangorestframework Pillow beautifulsoup4 Willow django-modelcluster django-treebeard olefile pytz
wagtail start mysite
python3 manage.py migrate
python3 manage.py createsuperuser
python3 manage.py runserver
EDIT: Made a mistake. Should've run with python3, not python in this case. Did it again and worked!
Conclusion: Looks that you need a virtualenv for this to work on Ubuntu 14 and python2. With python3 it works with and without virtualenv. I could not reproduce your error, but the idea is the same: it does not work because it looks for stuff in the wrong place.
Comments: There are two things you mention that I could not test:
I have been trying to get wagtail to work with my existing Django
application
With this big amount of heavy dependencies, (Pillow, Django rest, beatifullsoup and others I've never heard of) it would be strange if it worked out of the box with a previous setup. I mean, Django is a dependency for wagtail. So I would try the other way around. Start fresh with wagtail and port your code there (with python3 for the sake of the community ;) ). Also this manual copy of files is like programmers worst nightmare (at least to me). This looks like a version problem. Try to post the versions of everything involved, and the exact steps to reproduce the issue. I mean, did you pip install wagtail in the operating system that django was running?
I was having and uWSGI started.
So you have your app deployed already? This complicates things. If versions and virtualenv do not solve your problem, community needs setup details of deploy. Also you haven't mentioned your operational system.
I hope this helps! Good Luck!
I ran into the
cannot import name pages
error at:
from wagtail.wagtailadmin.urls import pages as wagtailadmin_pages_urls
while trying to deploy a wagtail app to Google App Engine and could not duplicate the problem on my local dev server.
A seemingly unrelated error that I saw only intermittently in GAE involved an import error in the PIL library. I was able to resolve BOTH errors by deleting PIL and pillow related folders from my vendored packages in my /lib directory. I had to import PIL through the app.yaml file instead due to the platform specific binary, but running pip install -t lib/ -r requirements-vendor.txt caused PIL to be installed into the lib folder as well (as a dependency of Wagtail)

how to import a python module before installing it?

So I'm trying to create a setup.py file do deploy a test framework in python.
The library has dependencies in pexpect and easy_install. So, after installing easy_install, I need to install s3cmd which is a tool to work with Amazon's S3.
However, to configure s3cmd I use pexpect, but if you want to run setup.py from a fresh VM, so we run into an ImportError:
import subprocess
import sys
import pexpect # pexpect is not installed ... it will be
def install_s3cmd():
subprocess.call(['sudo easy_install s3cmd'])
# now use pexpect to configure s3cdm
child = pexpect.spawn('s3cmd --configure')
child.expect ('(?i)Access Key')
# ... more code down there
def main():
subprocess.call(['sudo apt-get install python-setuptools']) # installs easy_install
subprocess.call(['sudo easy_install pexpect']) # installs pexpect
install_s3cmd()
# ... more code down here
if __name__ == "__main__":
main()
I know of course I could create a another file, initial_setup.py to have easy_install and pexpect installed, before using setup.py, but my question is: Is there a way to import pexpect before having it installed? The library will be installed before using it, but does the Python interpreter will accept the import pexpect command?
It won't accept it like that, but Python allows you to import things everywhere, not only in the global scope. So you can postpone the import until the time when you really need it:
def install_s3cmd():
subprocess.call(['easy_install', 's3cmd'])
# assuming that by now it's already been installed
import pexpect
# now use pexpect to configure s3cdm
child = pexpect.spawn('s3cmd --configure')
child.expect ('(?i)Access Key')
# ... more code down there
EDIT: there is a peculiarity with using setuptools this way, since the .pth file will not be reloaded until Python relaunches. You can enforce reloading though (found here):
import subprocess, pkg_resources
subprocess.call(['easy_install', 'pexpect'])
pkg_resources.get_distribution('pexpect').activate()
import pexpect # Now works
(Unrelated: I'd rather assume that the script itself is called with the needed privileges, not use sudo in it. That will be useful with virtualenv.)

Import error in twilio

I am having the same problem as this thread regarding twilio-python:
twilio.rest missing from twilio python module version 2.0.8?
However I have the same problem but I have 3.3.3 installed. I still get "No module named rest" when trying to import twilio.rest.
Loading the library from stand alone python script works. So I know that pip installing the package worked.
from twilio.rest import TwilioRestClient
def main():
account = "xxxxxxxxxxxxxxxx"
token = "xxxxxxxxxxxxxxxx"
client = TwilioRestClient(account, token)
call = client.calls.create(to="+12223344",
from_="+12223344",
url="http://ironblanket.herokuapp.com/",
method="GET")
if __name__ == "__main__":
main()
but this does not work:
from twilio.rest import TwilioRestClient
def home(request):
client = TwilioRestClient(account, token)
Do you have any idea what I can try next?
I named a python file in my project twilio.py. Since that file was loaded first, then subsequent calls to load twilio would reference that file instead of the twilio library.
TLDR: just don't name your python file twilio.py
Check which versions of pip and python you are running with this command:
which -a python
which -a pip
pip needs to install to a path that your Python executable can read from. Sometimes there will be more than one version of pip like pip-2.5, pip-2.7 etc. You can find all of them by running compgen -c | grep pip. There can also be more than one version of Python, especially if you have Macports or brew or multiple versions of Python installed.
Check which version of the twilio module is installed by running this command:
$ pip freeze | grep twilio # Or pip-2.7 freeze etc.
The output should be twilio==3.3.3.
I hope that helps - please leave a comment if you have more questions.
This Worked For me : (Windows)
Python librarys are in G:\Python\Lib
(Python is installed at G:, it might be different for you)
Download Twilio from github at paste the library at >> G:\Python\Lib <<
import problem gone :)
I had the same issue and it drove me crazy. Finally I figured it out. When you get the error:
AttributeError: module 'twilio' has no attribute 'version'
Look 2 lines above and the error is telling you where it expects to find the twilio file. So I moved it from where it was to where it was asking it to be.
Installed to:
c:\users\rhuds\appdata\local\programs\python\python37-32\lib\site-packages
Moved it to:
Traceback (most recent call last):
File "", line 1, in
import twilio
File "C:\Users\rhuds\AppData\Local\Programs\Python\Python37-32\twilio.py", line 2, in
Now I can import twilio. Besides that, the only other thing I did was uninstall old versions of Python, but I don't think that really mattered.

Categories