classification
Title: Error while using Python C API
Type: compile error Stage: resolved
Components: C API Versions: Python 3.10, Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, piyush115
Priority: normal Keywords:

Created on 2021-02-27 19:08 by piyush115, last changed 2021-03-01 15:16 by piyush115. This issue is now closed.

Messages (4)
msg387783 - (view) Author: Piyush Patel (piyush115) Date: 2021-02-27 19:08
Hi,
I am facing a very strange issue.
I am working on a C++ application which embeds a python.
As per documentation i have the dependencies added from python (/libs and /include directories) to C++ application.
I am using Py_RunString() to run simple python code.

Python code is simple:

import os
def Func():
    return 10.0

The application run smoothly when i have python installation in directory like 
"C:\PythonXY\"

but if python installation is in directory like "C:\MyPython\" or default installation path AppData\local\program\python\pythonxy\ , python code errors out.

the error i am getting is "__import__ not found".

I have used PyErr_Fetch to retrieve error.

It's really strange that the python installation directory affects how Python  code is run.

It seems it have a problem running "import os" statement or any "import" statement.


Could you help me how i can resolve this issue? 

Thanks,
Piyush
msg387789 - (view) Author: Piyush Patel (piyush115) Date: 2021-02-27 22:16
So upon further investigation, it seems all the built-in functions are not working. 

def func():
    max(10,20)

this code errors out saying "name 'max' is not defined"

so when python installation is in custom location or parent folder name is "python", the embedded python in C++ application behavior is bad and inconsistent.
msg387857 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-03-01 12:00
You're going to get more help by posting your question elsewhere. This isn't a forum where we can help you debug your code: it's for reporting bugs in Python.

You might try https://discuss.python.org/c/users/, or maybe the python-list mailing list.

Good luck!
msg387871 - (view) Author: Piyush Patel (piyush115) Date: 2021-03-01 15:16
Hi 

Thanks for your time.

Just wanted to add that earlier I used

PyImport_AddModule(""__main__").

but now I used PyImport_ImportModule(""__main__") 

resolved the issue for me regardless of installation path it worked.

Thanks 
Piyush
History
Date User Action Args
2021-03-01 15:16:28piyush115setstatus: pending -> closed
resolution: works for me
messages: + msg387871

stage: resolved
2021-03-01 12:00:27eric.smithsetstatus: open -> pending
nosy: + eric.smith
messages: + msg387857

2021-02-27 22:16:05piyush115setmessages: + msg387789
2021-02-27 19:08:59piyush115create