Can't pybindgen to work with gccxml - python

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"])

Related

TypeError: type 'types.GenericAlias' is not an acceptable base type

Okay so I compiled my python project into an executable and it worked on my computer but I sent it to a couple of friends to test and they all got this error. I have never seen an error like this before. I used Nuitka to compile the code.
Traceback (most recent call last): File "C:\Python39\lib\inspect.py", line 35, in <module>
import ast File "C:\Python39\lib\ast.py", line 29, in <module>
from contextlib import contextmanager, nullcontext File "C:\Python39\lib\contextlib.py", line 4, in <module>
import _collections_abc File "C:\Python39\lib_collections_abc.py", line 416, in <module>
class _CallableGenericAlias(GenericAlias): TypeError: type 'types.GenericAlias' is not an acceptable base type`
I know I'm very late for this but it might help someone in the future,
Caution: This solution involves messing around with source files and I was comfortable with that because I was using it in an isolated conda environment. Make sure you understand what you're doing before implementing it because clearly I DON'T.
I applied the solution pointed out in this post and it somehow solved the issue for me.
Basically I went into the source _collections_abc.py source file and swapped the variable GenericAlias in _CallableGenericAlias(GenericAlias) to _CallableGenericAlias(GenericAlias) which was declared at the beginning as GenericAlias = type(list[int]) to _CallableGenericAlias(GenericAlias) to _CallableGenericAlias(list[int])
Again, use with caution, because I also don't fully understand what's going on.
Original post found here

How to import function from one python script without calling it

I am trying to import a function called page1 that contains other functions inside of it in a library file so that I can call it inside this file. However, this creates the following error:
Traceback (most recent call last):
File "/Users/antonios/numworks-math-libs/numworksMath.py", line 1, in <module>
import numworksLibs
File "/Users/antonios/numworks-math-libs/numworksLibs.py", line 1, in <module>
from numworksMath import page1
File "/Users/antonios/numworks-math-libs/numworksMath.py", line 35, in <module>
page1()
File "/Users/antonios/numworks-math-libs/numworksMath.py", line 29, in page1
numworksLibs.get_ordered_pair(ordered_pair_num, xs, ys)
AttributeError: partially initialized module 'numworksLibs' has no attribute 'get_ordered_pair' (most likely due to a circular import)
I think this is because the main script is importing the library, and the library is importing the main script (circular import). Is there a way that I can get around this in Python? I have tried all solutions from this website and made sure that none of the names conflicted with any Python built-in libraries, as previously mentioned in other posts on SO. get_ordered_pair is also defined in the library file.
library file import:
from numworksMath import page1
a snippet of the main script:
import numworksLibs
def page1():
page1 contains code that will run if the function is called without an input, and this happens when importing the file from the Python CLI.
You can guard your code by checking if the file is being run as a script or if it's being imported as a module. To do this you need to check if the current __name__ is set to '__main__'.
If your file looks like this:
def foo():
...
foo()
Simply change it to only run foo when __name__ is '__main__'
def foo():
...
if __name__ == '__main__':
foo()
Now foo will only automatically run if you call python on the file directly, and not if it is simply imported.
What you would really want to do in this situation is to create a separate file like #Carcigenicate mentioned. with all the code that you are importing more than once and have each script import this. For me, I made a file called main.py with all the code that needed to be imported more than once, and then both the library file and the main script importing this file.

Why does the file name struct.py give errors?

I'm experimenting with image processing in python and for two days i was stuck with a problem.
I realized that naming a file to struct.py gave the error "numpy.core.multiarray failed to import" when trying the simple script below:
import numpy as np
k = np.ones((9,9))
print(k)
the same code worked when i created a file with another name.
Now I'm looking for an answer as to why that happened.
I created a test file "struct.py" to try different structuring elements for morphological operations. In all my other test files numpy worked correctly, but for some reason, in that "struct.py" script I got an error saying that "numpy.core.multiarray failed to import". I was so invested into solving that problem that during the solving process i didn't think to test if numpy worked in my other scripts (where they had worked before)
I reinstalled python several times, switched IDE, tried to code locally instead of using a version controller but all of these things seemed pretty unnecessary for me to do because i didn't see how that could be the reason for numpy not to work.
Today i saw that there is a python module called struct, and that me using that name for my script must be the cause of the problem, but I still don't understand why numpy stopped working because of that. Is numpy dependent on the struct module? How could this happen?
Also this is my first time actually posting an issue on stackoverflow, please let me know if i should change the contents of my post or delete unnecessary information or add more.
You must not name your module struct.py because struct module exists as built-in (this is used to serialize/deserialize data to/from memory/disk, with endianness & size management for numeric types)
>>> import struct
>>> struct.__file__
'C:\\Users\\xxxx\\AppData\\Local\\Programs\\Python\\Python37\\lib\\struct.py'
If you create a file named the same way, if another module tries to import the builtin struct module, it could import yours instead, and hard-to-understand errors may appear.
If I create a struct.py file in my current directory containing just:
print("hellooooo bug")
here what happens when I import numpy:
>>> import numpy
hellooooo bug
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\jotd6\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\__init__.py", line 140, in <module>
from . import _distributor_init
File "C:\Users\jotd6\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\_distributor_init.py", line 9, in <module>
from ctypes import WinDLL
File "C:\Users\jotd6\AppData\Local\Programs\Python\Python37\lib\ctypes\__init__.py", line 14, in <module>
from struct import calcsize as _calcsize
as you see numpy needs struct. Just don't use that name (or any generic name, without checking if it doesn't exist as built-in first). An indirect variant of Python csv import fails
I pretty sure it is because in module numpy there is a file called pickle.py which contains a line called from struct import pack, unpack since your document is called struct but does not contain pack or unpack it gives an error due to a circular import.

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.

python dbus problem

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.

Categories