python dbus problem - python

I have a problem with dbus and python. Running python from the command line, telling it import dbus and then systembus = dbus.SystemBus() results in no errors, nor does running a program written by a friend which also uses the exact same code. However, when running a program I'm trying to write, I get this error:
Traceback (most recent call last):
File "dbtest.py", line 26, in <module>
a = getDevs()
File "dbtest.py", line 7, in getDevs
bus = dbus.SystemBus()
AttributeError: 'module' object has no attribute 'SystemBus'
Any ideas as to what I'm doing wrong? I don't think I fully understand the error returned. The code I have so far is:
#!/usr/bin/env python
import dbus
def getDevs():
bus = dbus.SystemBus()
if __name__ == "__main__":
a = getDevs()

The obvious problem is that when you are importing dbus, it is not getting all the methods with it.
In both your program and your friend's, do print dbus.__file__. This will show what .pyc it is using. If they are different, you are not importing the correct dbus module.
I'm going to guess that you are actually importing some random file called dbus.py in your local directory. Or, if your script name is "dbus.py", you are just importing itself and luckily python doesn't import recursively. The easiest solution in this case is to rename the offending file to something else.

Related

Run python script from another python script on linux

I'm using python 2.7 and ubuntu 16.04.
I have a simple REST server on python: file server_run.py in module1 which is importing some scripts from module2.
Now I'm writing an integration test, which is sending POST request to my server and verify that necessary action was taken by my server. Obviously, server should be up and running but I don't want to do it manually, I want to start my server (server_run.py which has also main method) from my test: server_run_test.py file in module3.
So, the task sounds very simple: I need to start one python script from another one, but I spent almost the whole day. I found a couple of solutions here, including:
script_path = "[PATH_TO_MODULE1]/server_run.py"
subprocess.Popen(['python', script_path], cwd=os.path.dirname(script_path))
But my server is not coming up, throwing the error:
Traceback (most recent call last):
File "[PATH_TO_MODULE1]/server_run.py", line 1, in <module>
from configuration.constants import *
File "[PATH_TO_MODULE2]/constants.py", line 1, in <module>
from config import *
ModuleNotFoundError: No module named 'config'
So, it looks like when I'm trying to start my server in subprocess it doesn't see imports anymore.
Do you guys have any idea how can I fix it?
Eventually, the solution was found, 2 steps were taken:
1. In each module I had an empty __init__.py file, it was changed to:
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
__version__ = '${version}'
2. Instead of using the following syntax:
from configuration.constants import *
from configuration.config import *
config and constants were imported as:
from configuration import constants,config
and then we are using reference to them when need to get some constant.
Thanks everyone for looking into it.
Rather than running it using os module try using the import function. You will need to save in the same dictionary or a sub folder or the python installation but this seems to be the way to do it. Like this post suggests.

Can't pybindgen to work with gccxml

I have a C++ library I would like to bind to Python. I began using Pybindgen, and it is really easy to use, but manually adding functions and namespaces will take a long time considering the size of my C++ library. I've read through the documentation on PyBindGen, specifically, the gccxml portion that supposedly scans header files for me. This would be ideal, yet I can't get it to function properly. Just as a test, I inputted my main header file and tried to export it, but I get this error:
python bindinggenerator.py
Traceback (most recent call last):
File "bindinggenerator.py", line 16, in <module>
main()
File "bindinggenerator.py", line 9, in main
module = module_parser.parse("include\\PhospheneEngine.h")
File "C:\Users\paolo\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pybindgen\gccxmlparser.py", line 598, in parse
pygen_classifier, gccxml_options)
File "C:\Users\paolo\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pybindgen\gccxmlparser.py", line 658, in parse_init
assert isinstance(header_files, list)
AssertionError
And of course my Python code is basically just the modified example from here.
import sys
import pybindgen
from pybindgen import FileCodeSink
from pybindgen.gccxmlparser import ModuleParser
def main():
module_parser = ModuleParser('PhospheneEngine', '::')
module = module_parser.parse("include\\PhospheneEngine.h")
module.add_include("'include\\PhospheneEngine.h'")
pybindgen.write_preamble(FileCodeSink(sys.stdout))
module.generate(FileCodeSink(sys.stdout))
if (__name__ == '__main__'):
main()
I have both gccxml installed and pygccxml (For Python 3.5) installed. The documentation really doesn't say too much about this process, so possibly a quick rundown of how to use this feature would be appreciated.
Thanks in advance.
The parse function takes a list of headers.
module = module_parser.parse(["include\\PhospheneEngine.h"])

Receiving AttributeError from os.path.isfile() function

Receiving the below error when running my script:
Traceback (most recent call last):
File "HC_Main.py", line 54, in <module>
setup_exists = os.path.isfile(config_file)
AttributeError: 'function' object has no attribute 'isfile'
Sample code is:
import os
setup_exists = os.path.isfile(setup_exists)
if setup_exists is False:
print "Setup file exists"
When I checked the IDLE console with dir(os.path), isfile is listed. Additionaly, I can use the function without issues in IDLE as well.
Could it be my IDE causing issues here? I've also tried running the script apart from the IDE, but it still receives the error.
Somehow, os.path is no longer the builtin module, but it has been replaced with a function. Check your code to make sure you didn't accidentally monkey-patch it somewhere.
For clues, you could start by putting:
print os.path
right before the line where you actually use os.path.isfile. This should give you the function's name which will hopefully give you a good place to start looking.
Try
import os.path
instead
see this thread for more info: How do I check whether a file exists using Python?
Found the issue. I had an if/else statement earlier in the code, which was being used to gather the OS version the script was running on. Turns out I used OS (caps) for the variable name, which I think caused this. I changed it around, and it's fixed.

How do I install Python module given as .py file?

I do realize this is a noobish question, but I've been trying for an hour and I can't get it right.
So, I have a Python script which I'd like to modify a bit and play around with as a Python beginner. However, at the very beginning of the script, there's this:
from priodict import priority_dict
Now, I have a file named priodict.py that came with the script. But how do I make it available to the script so it can be included like that?
The Python manual has pages and pages on installing modules, but they all seem to refer to "packages" which are to be placed in certain directories etc. What do I do when I have just the .py file?
I know there is probably a banale one-sentence response to this, but I'm getting frustrated and I'm short on time so I decided to take the easy way out and ask Stack overflow about it.
It seems that, if I don't have the priodict.py file, I get this error:
Traceback (most recent call last):
File "C:\Python27\scripts\dijksta.py", line 192, in <module>
main()
File "C:\Python27\scripts\dijksta.py", line 185, in main
D, _ = dijkstra(G, 1, v)
File "C:\Python27\scripts\dijksta.py", line 139, in dijkstra
Q = priority_dict() # est.dist. of non-final vert.
NameError: global name 'priority_dict' is not defined
If I place the file in the same directory as my script, I get this error:
Traceback (most recent call last):
File "C:\Python27\scripts\dijksta.py", line 2, in <module>
from priodict import priority_dict
ImportError: cannot import name priority_dict
These are the files in question:
https://github.com/kqdtran/ADA1/tree/master/dijkstra
Place the file in the same directory, and that will get you started. Seems you've figured out that much. If all you have is a .py file, that's what you're usually expected to do.
If you're unable to import a name from a module, it usually means that name doesn't exist in that module. Try:
import priodict
print dir(priodict)
Is priority_dict listed? If not, is there a similarly named attribute that might be what you're looking for? It may just be that the instructions given you were misspelled, like the _ wasn't needed.
If it fails on the import line, it may be that there's an error in the module code itself that must be corrected first. you'll get an error telling you roughly where it is.

Pycap not working in script

i m trying to capture ethernet packet using pycap http://pycap.sourceforge.net/. when i use following command on python prompt with root privileges, it is working
>>>import pycap.capture
>>>p = pycap.capture.capture("wlan0")
>>>p.next()
(Ethernet(type=0x608, 00:1b:b1:46:53:5d -> ff:ff:ff:ff:ff:ff), ARP(op=0x1, protocol=0x800, 00:1b:b1:46:53:5d (192.16.68.10) -> 00:00:00:00:00:00 (192.16.110.39)), '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', 1307898356.222398)
But if i run these commands as a script, i m getting following error
>>>sudo python pycap.py
Traceback (most recent call last):
File "pycap.py", line 2, in <module>
from pycap import *
File "/home/nikhil/Code/Python/pycap.py", line 5, in <module>
p = capture.capture(device)
NameError: name 'capture' is not defined
Any suggestions?
pycap http://pycap.sourceforge.net/ says it is requires python2.3 and im using python2.6. Is that a problem?
Your script using pycap is called pycap itself, so import pycap imports itself (. is usually the first directory on the import path). Because imports are caches, this doesn't lead to infinite recursion but instead gives you a reference to your own module, which of course doesn't define capture or anything else. Rename it.

Categories