global name 'GLib2Reactor' is not defined - python

I'm struggling to get some python code using the python-brisa framework to work, the code is not written by me but should be straight forward.
from brisa.core.reactors import install_default_reactor
reactor = install_default_reactor()
from brisa.core.threaded_call import run_async_function
import xml.etree.ElementTree as ET
from time import sleep
import sys, os
import sonos
import knx
Howewer after installing the frameworks I get
Traceback (most recent call last):
File "knxsonos.py", line 24, in <module>
reactor = install_default_reactor()
File "/usr/local/lib/python2.7/dist-packages/brisa/core/reactors/__init__.py", line 14, in install_default_reactor
return GLib2Reactor()
NameError: global name 'GLib2Reactor' is not defined
I have been looking both stack overflow, and googling for days without finding a solution.
Anyone??, help would be greatly appreciated...

Here's some possibilities:
GLib2Reactor doesn't return anything - Then your code is wrong
GLib2Reactor is not declared - try this:
x = GLib2Reactor()
return x
GLib2Reactor has to be imported - just import it
my best advice: read the docs

When I saw this problem on an Ubuntu Trusty (14.04) system, it was caused by import gobject failing. The fix was to install the correct package:
sudo apt-get install python-gobject-2
This package is marked as deprecated, so on newer distributions it might be necessary to install the python-gi package instead, and then modify the callers to use the new names:
from gi.repository import GObject

Related

What is meant by `import coding`?

I am working with elffile python module/library. There is one line of code that is giving the following error
Traceback (most recent call last):
File "elffile.py", line 36, in <module>
import coding
ImportError: No module named coding
I could not find any python file or class named coding. Also, I didn't find anything on the internet regarding import coding.
Import is when you import modules to use in your coding.
If it cant find the module you probably have to install it. The easiest way to do this is to use pip.
Write: pip install coding (or pip3) and it will install the module.

import libvirt in python but ImportError on Ubuntu

My os is Ubuntu 12.04.
I install the kvm virt-manager and python.
I want to use python code to control the vm on kvm.
but my code import libvirt has something wrong...
Here is my code :
import libvirt
import time
import threading
import paramiko
import os
import commands
import signal
numVM = 1
hostname=['VM-01']
port=22
VMLoadAve={}
def monitor():
.....
while True:
monitor()
time.sleep(MAXDURATION)
Traceback (most recent call last): File "test1.py", line 1, in
import libvirt ImportError: No module named libvirt
I have installed the python-libvirt, libvirt-bin
what other reasons may do this happened?
I am a newest for this. thanks for your help ~ :)
For the sake of historicaly record, 'python-libvirt' is the correct deb package for the python 2 binding, while python3-libvirt gives you the corresponding python 3 binding to libvirt.

Import pexpect in child module - 'no module named pexpect'

I'm seeing weird behavior that I don't understand, so I'm turning to the experts here on SO for help. I have looked at similar questions, without finding anything that looked helpful.
I'm writing a program that imports two custom modules.
If I run the main program using the cli "./ld_config_automation.py" I get the following error:
laptop$ ./ld_config_automation.py
Traceback (most recent call last):
File "./ld_config_automation.py", line 34, in <module>
from modules.networkcomponent import NetworkComponent
File "/path/to/pprograms/ssh_telnet_automation/modules/networkcomponent.py", line 7, in <module>
import pexpect
ImportError: No module named pexpect
However, if I run the main program using the cli "python ld_config_automation.py" everything works fine and the program runs.
My main program uses the following import statements.
import sys
import getopt
from modules.networkcomponent import NetworkComponent
from modules.recordclass import Record
The module I'm having trouble with "NetworkComponent" uses the following import statements:
import re
import pexpect
Also, if I remove import pexpect from the NetworkComponent module it works as expected without python in front (up to when it has to use pexpect. of course). So it doesn't seem to be a problem with importing modules, as re works ok, just not pexpect.
I'm developing this on mac os X but will implement on CentOS.

ImportError: cannot import name process (Twisted)

I'm using Mac OS X 10.9.1, and I remember installing Python some time ago (I can't remember why, given that Mac OS X comes with it). For some reason Twisted wasn't installed, so I installed Zope and Twisted.
I'm following this tutorial: http://www.raywenderlich.com/3932/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server
The problem is, when I run this code:
from twisted.internet.protocol import Factory,Protocol
from twisted.internet import reactor
class IphoneChat(Protocol):
def connectionMade(self):
print "a client connected"
factory = Factory()
factory.protocol = IphoneChat
reactor.listenTCP(80, factory)
print "Iphone Chat server started"
reactor().run
I get this error:
Traceback (most recent call last):
File "/Users/Mattieman/Desktop/server.py", line 4, in <module>
from twisted.internet import reactor
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/twisted/internet/reactor.py", line 38, in <module>
from twisted.internet import default
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/twisted/internet/default.py", line 56, in <module>
install = _getInstallFunction(platform)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/twisted/internet/default.py", line 52, in _getInstallFunction
from twisted.internet.selectreactor import install
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/twisted/internet/selectreactor.py", line 18, in <module>
from twisted.internet import posixbase
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/twisted/internet/posixbase.py", line 53, in <module>
from twisted.internet import process, _signals
ImportError: cannot import name process
So I took a look in /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/twisted/internet
...and it appears that process.py is missing.
What do I do?
There seem to be other files missing as well, like _baseprocess.py
I used setup3.py, if it helps...
You're using Python 2.6. You shouldn't use setup3.py. You also shouldn't ever use a distutils setup.py-type script to install Python libraries into OS paths (like /Library/Frameworks/Python.framework/Versions/2.6/).
You may have a randomly corrupted Twisted installation on your system now (OS X itself ships with a copy of Twisted). Or you may just have some garbage in /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/twisted that you need to clean up. It's difficult to say which (which is why you should never use setup.py to install things - it leaves a big mess).
After you clean up that mess (I can't give you many tips on how to do this apart from "reinstall your OS X", sorry), you have a few options:
Use the version of Twisted distributed with your OS. Don't try to install a different version.
Treat your home directory as a simpler "virtual environment" and run setup.py install --user (--user tells it to leave its mess inside ~/.local where at least it won't ruin your OS install).
Create a virtualenv and install a new version of Twisted there
Of these, you probably want to go with virtualenv.
Also note that you shouldn't run setup3.py unless you're trying to install Twisted for Python 3. And if you're trying to do that then I'll warn you that only part of Twisted is ported - and it's a very small part. For example, it's not process support (hence the import error you're getting).

Scapy.all import * does not work

So, I wrote a little script in Ubuntu for scapy.
#!/usr/bin/env python
import sys
#from scapy.all import *
try
import scapy
except ImportError:
del scapy
from scapy import all as scapy
i= IP()
t= TCP()
i.dst='192.168.56.100'
t.dport=22
pakket=i/t
answered,unanswered=sr(pakket)
answered.nsummary()
i wrote the 'try' because of another topic here (tried it as a solution).
My current output with this code is the following
Traceback (most recent call last):
File "./scapy.py", line 5, in <module>
import scapy
File "/home/nicholas/scapy.py", line 9, in <module>
i=IP()
NameError: name 'IP' is not defined
when trying it just with from scapy.all import * withouth the 'try'.
Traceback (most recent call last):
File "./scapy.py", line 3, in <module>
from scapy.all import *
File "/home/nicholas/scapy.py", line 3, in <module>
from scapy.all import *
ImportError: No module named all
I tried different ways of importation found on Google but it still doesn't work. Could anyone please tell me what I'm doing wrong? (don't mind the indentation of this post)
From looking at scapy source, the scapy package doesn't appear to import anything or define an __all__ in __init__. As a result, you need to explicitly import scapy.all (or from scapy import all) before you can from scapy.all import anything else from it, as it won't be in sys.modules yet. Note that this only has to happen once in your program flow though, as after the interpreter imports the module, it will be available to all code that executes from then on, regardless of where it is. Take a look at the Python docs on modules and how import, and specifically importing a package, works for more details.
Edit:
I think I see the problem now, I just was paying attention to the wrong part of your stack trace. Pretty sure what you are dealing with here is a name collision. Your file is named scapy.py, so when you import scapy from the context of that file, you are actually importing the file itself as a module. Since your file does not have a submodule named all (it can't, since it's not a package), you get the import error you are seeing. Try switching the name of your file to something that does not conflict with any packages or modules you wish to import inside it, and see if that works out better.
By the way, note in your stack traces that your import is actually essentially recursively calling your one file. That should be a clue that something has gone haywire in the import process.
I had a similar problem on OSX, I installed the scapy package pip install scapy and then I was trying to execute my test file scapy.py The error I got was :
python scapy.py
Traceback (most recent call last):
File "scapy.py", line 1, in <module>
from scapy.all import *
File "/Users/**/Desktop/scapy-test/scapy.py", line 1, in <module>
from scapy.all import *
ModuleNotFoundError: No module named 'scapy.all'; 'scapy' is not a package
In my case, it was the file name itself that caused the problem it can't be called scapy.py. I change it to test.py and all worked, it had nothing to do with the package location just the file name.
I like to add something to #Daniel answer. Your real problem is not scapy package. Your real problem is in your python file name. Don't ever use library name or its contents as your file name.
In your case, your file name is scapy.py. After that you import scappy. In here you accidentally call your python file as object in your code there for your compiler can't understand which type(file or library) to call. There for that error was appeared.
I saw this when I had a scapy.py in the current directory. scapy.all import * seems to look in the current directory first.
The correct import with current versions would be:
from scapy.all import *

Categories

Resources