AttributeError when embedding python in C++ (Ubuntu)

WBOY
Release: 2024-02-10 14:09:04
forward
949 people have browsed it

在 C++ 中嵌入 python 时出现 AttributeError (Ubuntu)

Question content

I have a c program embedded in python3.11.2. When calling a python script from embedded python (importing 3rd party modules as well as standard library modules), I get the following error:

excType: 'AttributeError'
excValue: AttributeError("module 'abc' has no attribute '__file__'")
excTraceback: <traceback object at 0x7f1e48c5f200>
Copy after login

When I run the script from normal (non-embedded) python, everything works fine and I am also able to find the file attributes for the abc module.

I compiled python3.11.2 from source using the following configuration: --enable-optimizations --enable-shared --prefix="path/to/my/custom/build/folder/

The weird thing is that every 3rd party module my script imports has a file attribute and loads fine.

Does anyone have any insight into what could be causing this?

My operating system: ubuntu 22.04.3

I tested other standard library modules such as os. If I call a function in the OS from embedded python, it works fine, but the file attribute of the OS is not found. If I do this from non-embedded python, it does find the file attribute.

Update: I just discovered that embedded python has an empty prefix. How can I change this prefix? Setting pythonhome doesn't change anything.


Correct answer


I found the solution by manually adding the file attribute of the module. I still don't understand why the file property is not set by default, but this works for me now.

The above is the detailed content of AttributeError when embedding python in C++ (Ubuntu). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!