我想在不激活已安装的虚拟环境的情况下执行Python脚本,但遇到了ModuleNotFoundError

编程语言 2026-07-11

我正尝试在C++文件中执行一个Python脚本,而这个Python脚本需要一些模块才能运行。已经通过pip在名为 '.embed' 的虚拟环境中安装了Python包。我的目标是在不激活任何虚拟环境的情况下执行这个Python脚本,同时调用所需的模块:

(.embed) raphy@raphy:/2HardDisk/Qwen3EmbeddingTest/.embed/lib/python3.12/site-packages$ ls -lah | grep sentence_transformers
drwxrwxr-x  12 raphy raphy 4.0K Mar 18 09:34 sentence_transformers
drwxrwxr-x   3 raphy raphy 4.0K Mar 18 09:34 sentence_transformers-5.3.0.dist-info

在 /src/main.cpp文件中:

    char mypythonpath[] = "PYTHONPATH=/2HardDisk/Qwen3EmbeddingTest/src/utilities/";
    putenv( pythonscriptspath_c);
    putenv(mypythonpath);

在 /src/utilities/python_utilities.cpp 文件中:

    void PyScriptCall(std::string modulename, std::string param)
    {

        // Initialize the interpreter.
        Py_Initialize();

        // Adjust Python sys.path
        PyRun_SimpleString(
            "import sys\n"
            "sys.path.append('/usr/include/python3.12/Python.h')\n"
            "sys.path.append('/2HardDisk/Qwen3EmbeddingTest/.embed/bin')\n"
            "sys.path.append('/2HardDisk/Qwen3EmbeddingTest/.embed/lib/python3.12/site-packages/sentence_transformers/')\n"
        );

        // Set the name of the script.
        PyObject *pName = PyUnicode_FromString("embedder");

        // Import the script as a Python module.
        PyObject *pModule = PyImport_Import(pName);
        Py_DECREF(pName);

        if (pModule != NULL)
        {
            const char* modulename_c = modulename.c_str();
            // Get the reference to the Python function.
            PyObject *pFunc = PyObject_GetAttrString(pModule, modulename_c);

            if (pFunc && PyCallable_Check(pFunc))
            {
                // Prepare arguments for the Python function.
                const char* param_c = param.c_str();
                PyObject *pArgs = PyTuple_Pack(1, PyUnicode_FromString(param_c));

                // Call the function.
                PyObject *pValue = PyObject_CallObject(pFunc, pArgs);
                Py_DECREF(pArgs);

                if (pValue != NULL)
                {
                    // Print the return value.
                    printf("[+] Python function return: %s\n", PyUnicode_AsUTF8(pValue));
                    Py_DECREF(pValue);
                }
                else
                {
                    PyErr_Print();
                     fprintf(stderr, "[-] Python function return: %s\n", PyUnicode_AsUTF8(pValue));
                }
                Py_DECREF(pFunc);
            }
            else
            {
                if (PyErr_Occurred())
                {
                    PyErr_Print();
                }
                fprintf(stderr, "[-] Cannot find function \n");
            }
            Py_DECREF(pModule);
        }
        else
        {
            PyErr_Print();
            fprintf(stderr, "[-] Failed to load module \n");
        }
        // Clean up and exit the interpreter.
        Py_Finalize();
    }
Python script to execute:

embedder.py file :
    sys.path.insert(0, '/2HardDisk/Qwen3EmbeddingTest/.embed/lib/python3.12/site-packages/sentence_transformers')

    for i, path in enumerate(sys.path):
        print(f"{i}: {path}")


    from sentence_transformers import SentenceTransformer
    import torch
    from transformers import AutoModel, AutoTokenizer, AutoModelForCausalLM


    print("Total arguments:", len(sys.argv))
    #print("Argument-01:", sys.argv[1])
    #print("Argument-02:", sys.argv[2])


    # Load Qwen3-Embedding-0.6B model for text embeddings
    embedding_model = SentenceTransformer("Qwen/Qwen3-Embedding-0.6B")

    # Load Qwen3-Reranker-0.6B model for reranking
    reranker_tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-Reranker-0.6B", padding_side='left');
    reranker_model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen3-Reranker-0.6B").eval()

    def emb_text(text, is_query=False):

        print(f"Input to emb_text: {text}")

        """
        Generate text embeddings using Qwen3-Embedding-0.6B model.
        Args:
            text: Input text to embed
            is_query: Whether this is a query (True) or document (False)
        Returns:
            List of embedding values
        """
        if is_query:
            # For queries, use the "query" prompt for better retrieval performance
            embeddings = embedding_model.encode([text], prompt_name="query")
        else:
            # For documents, use default encoding
            embeddings = embedding_model.encode([text])

        return embeddings[0].tolist()

    test_embedding = emb_text("This is a test")
    embedding_dim = len(test_embedding)
    print(f"Embedding dimension: {embedding_dim}")
    print(f"First 10 values: {test_embedding[:10]}")




Output : 

    0: /2HardDisk/Qwen3EmbeddingTest/.embed/bin
    1: /2HardDisk/Qwen3EmbeddingTest/src/utilities
    2: /usr/lib/python312.zip
    3: /usr/lib/python3.12
    4: /usr/lib/python3.12/lib-dynload
    5: /usr/local/lib/python3.12/dist-packages
    6: /usr/lib/python3/dist-packages
    7: /usr/include/python3.12/Python.h
    8: /2HardDisk/Qwen3EmbeddingTest/.embed/bin
    9: /2HardDisk/Qwen3EmbeddingTest/.embed/lib/python3.12/site-packages/
    sentence_transformers/
    Traceback (most recent call last):
      File "/2HardDisk/Qwen3EmbeddingTest/src/utilities/embedder.py", line 19, in     
    <module>
        from sentence_transformers import SentenceTransformer
    ModuleNotFoundError: No module named 'sentence_transformers'
    [-] Failed to load module

更新01)

我按照建议尝试在Python脚本中指定模块的完整文件路径:

#from sentence_transformers import SentenceTransformer
#from '/2HardDisk/Qwen3EmbeddingTest/.embed/lib/python3.12/site-    
packages/sentence_transformers' import SentenceTransformer
from '/2HardDisk/Qwen3EmbeddingTest/.embed/lib64/python3.12/site-
packages/sentence_transformers' import SentenceTransformer

但这并不起作用

解决方案


sys.path.append('/2HardDisk/Qwen3EmbeddingTest/.embed/lib/python3.12/site-packages/sentence_transformers/')
替换为
sys.path.append('/2HardDisk/Qwen3EmbeddingTest/.embed/lib/python3.12/site-packages')

site-packages 是Python存放通过 pip 安装的 第三方库 的目录

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章