[BUG]: AttributeError: module has no attribute
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
What version (or hash if on master) of pybind11 are you using?
2.10.3
Problem description
OK, I am trying to export the following simple Add function (I took the example from the online tutorial on the pybind11 website) without any success:
#include <cstdint>
#include <pybind11/pybind11.h>
int32_t Add(int32_t a, int32_t b) {
return a + b;
}
PYBIND11_MODULE(SGPYCPP_MODULE_NAME, m) {
m.doc() = "A simple Python/C++ example";
m.def("Add", &Add, "A function that adds two numbers");
}
When I import it, it imports my module, but says it has no attribute named Add:
python3
Python 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import SGPyCpp
>>> SGPyCpp.Add(2,1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'SGPyCpp' has no attribute 'Add'
>>>
I even tried to name my module example and use the exact sample from the official online tutorial to leave out any other possibility without any luck:
int add(int i, int j) {
return i + j;
}
PYBIND11_MODULE(example, m) {
m.doc() = "pybind11 example plugin"; // optional module docstring
m.def("add", &add, "A function that adds two numbers");
}
I even tried to comment on most of CMakeLists.txt and reduced it to the sample from here without any luck.
I'll attach the full source code as a zip file in order to replicate the issue. After extraction, just issue:
make clean && make
I'd appreciate it if you'd let me know what's wrong.
Reproducible example code
A full example is provided in the zip file:
#include <cstdint>
#include <pybind11/pybind11.h>
int32_t Add(int32_t a, int32_t b) {
return a + b;
}
PYBIND11_MODULE(SGPYCPP_MODULE_NAME, m) {
m.doc() = "A simple Python/C++ example";
m.def("Add", &Add, "A function that adds two numbers");
}
### Is this a regression? Put the last known working version here if it is.
Not a regression
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Extract the attached SGPyCpp.zip, inspect its CMakeLists.txt and the PYBIND11_MODULE definition, then run make clean && make as requested. Check whether the built module name and imported module match the exported function, and consider the issue done when the cause of the missing Add attribute is reproducible and documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cmake, cpp, python
- Domain
- api, build-system
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100