Cpython import module error

why can’t i import module in my python code?
What i want is that i want to execute python code in c++ code, and then this python code imports the package of the local virtual environment
here is my c++ code:

#include <Python.h>
#include <iostream>

int main() {
    setenv("PYTHONPATH", "/home/timeplus/ssd/project/proton_sake/contrib/cpython-cmake/cpython/Lib:/home/timeplus/venvs/env2/lib/python3.10/site-packages", 1);
    const char * source = "import sys\n"
                          "from lxml import etree\n"
    // PyRun_SimpleString(source);
    PyObject * pySource = Py_CompileString(source, "", Py_file_input);
    if (pySource == NULL) {
        return 1;
    PyObject *result = PyEval_EvalCode(pySource, PyModule_GetDict(PyImport_AddModule("__main__")), NULL);
    if (result == NULL) {
        return 1;
    return 0;

I am sure this virtual environment has this package.
here is the errro message:

['/home/timeplus/ssd/project/proton_sake/contrib/cpython-cmake/cpython/Lib', '/home/timeplus/venvs/env2/lib/python3.10/site-packages', '/usr/bin/python3/lib/python313.zip', '/usr/bin/python3/lib/python3.13', '/usr/bin/python3/lib/python3.13/lib-dynload', '/home/timeplus/venvs/env2/lib/python3.10/site-packages']
Traceback (most recent call last):
  File "", line 4, in <module>
ImportError: cannot import name 'etree' from 'lxml' (/home/timeplus/venvs/env2/lib/python3.10/site-packages/lxml/__init__.py)

what should i do? If need more message pleage let me know.

You have asked Python (via the PYTHONPATH environment variable) to use libraries that were installed for a specific Python 3.10 virtual environment, but the sys.path shown clearly tells us that a Python 3.13 interpreter is running. This should not be expected to work. After all, lxml is not a pure-Python project; it uses Cython for performance, so it needs to be built - and in doing so it becomes specific to a Python version.

When you use the C API to embed and run Python code, it’s going to use the interpreter that has been embedded (compiled and linked) into the program. It’s not going to care about any specific separate Python executables you may have, whether they’re in virtual environments or not.

I have 2 question about your replay.

  1. About the Python 3.13 interpreter, it is because i cloned the latest CPython code in github, and compile it to static library, then i link it to my program and my local python version is 3.10.Does it have any problem?I think it won’t cause any problem, since the python code is compiled and executed in python3.13.

  2. I just want to know if i can can import the package form some virtual environment to my python code in c/c++? What you say “It’s not going to care about any specific separate Python executables you may have, whether they’re in virtual environments or not.”, does it mean ok? or not every package can do, such as lxml is a special case?

Thank you very much for the answer!

Unless you want to be test the future python 3.13 release I suggest you use python 3.12 latest source release.

If you are using 3.12 you can very likely just install packages you need from PyPI into your venv.