Table of Contents
Correct answer
Home Backend Development Python Tutorial AttributeError when embedding python in C++ (Ubuntu)

AttributeError when embedding python in C++ (Ubuntu)

Feb 10, 2024 pm 02:09 PM
standard library

在 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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use absolute value in c++ How to use absolute value in c++ May 06, 2024 pm 06:15 PM

How to use absolute value in c++

What does iomanip mean in c language What does iomanip mean in c language May 02, 2024 pm 05:57 PM

What does iomanip mean in c language

How to use std:: in c++ How to use std:: in c++ May 09, 2024 am 03:45 AM

How to use std:: in c++

What does prime mean in c++ What does prime mean in c++ May 07, 2024 pm 11:33 PM

What does prime mean in c++

What does config mean in java? What does config mean in java? May 07, 2024 am 02:39 AM

What does config mean in java?

How to calculate absolute value in c++ How to calculate absolute value in c++ May 06, 2024 pm 06:21 PM

How to calculate absolute value in c++

What does fabs mean in c++ What does fabs mean in c++ May 08, 2024 am 01:15 AM

What does fabs mean in c++

How to pop up a window in python How to pop up a window in python May 05, 2024 pm 08:15 PM

How to pop up a window in python

See all articles