[Python] Chrome Native Host in Python: Pipenv Not Recognized and Flask App Fails to Run

Discussão em 'Python' iniciado por Stack, Março 20, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to use a flask app cli tool to process some data for a chrome extension. I was able to set up the native host and establish a connection that successfully receives a request and responds to it. But I'm having trouble triggering the cli tool.

    The reason I want a CLI tool as a backend is so that it can function as both a standalone CLI app and process various chrome extension tasks.

    It seems the main issue is that pipenv and python and python3 aren't recognized by the system when it's invoked from the extension. Is it possible to set up a chrome extension native host that 1) is able to use the pipenv shell or 2) can access a flask app and its cli commands? I'm wondering if a server might be necessary somehow, but it seems like starting/using it on the local system (as opposed to hosting it remotely) might be out of the question.

    Project structure:

    \root
    \app
    \commands
    \pdf_group
    __init__.py
    combine.py
    __init__.py
    __init__.py
    app.py
    Pipfile
    native_host.py
    some_script.py


    What I've tried so far:


    • from app.commands import my_cli_command in native_host.py Simply trying to import the code fails with the effect that the native host doesn't respond. The extension error reads Unchecked runtime.lastError: Error when communicating with the native messaging host. Same for the full import path, from app.commands.pdf_group.combine import my_cli_command.


    • os.system("/opt/homebrew/bin/pipenv") in native_host.py My local terminal gave this string result for which pipenv, so I tried that in the native host. Same result.


    • subprocess.call("flask pdf combine") Same result.


    • import click in native_host.py Same result. Click is part of flask, so I think the third-party nature somehow contributes to the failure.


    • import some_script, run script function some_script.do() in native_host.py Same result. This script is in the root level of the project. (do() is obviously not a proper name for a script main function, I am hoping for a solution where some_script is unnecessary.)

    Basically, any third party code from the app or the cli app itself doesn't work in native_host.py and leads the native host to fail to respond.


    • os.system("which python"), or os.system("which pipenv") The output result was error 256, indicating that the system can't find the path for these.


    • I tried all of these methods from the command line/in a python shell. They all worked as expected there.

    EDIT

    I don't think this is the ideal approach, but I was able to get this to work.


    1. On the terminal, go into pipenv shell


    2. Copy the path that is output from which python3


    3. In native_host.py, include the outputted path to run the app.

    import os
    os.system("/Users/bernadette/.local/share/virtualenvs/convert_and_combine_pdfs-mqF7aRtH/bin/python3 ./some_script.py")


    What seemed to make the difference was externally(?) accessing the virtual environment. But it doesn't seem to handle sending the native host's response back properly.

    Remaining questions


    • Is a script within a script the only way to get this working?


    • subprocess.call("...") didn't seem to work, is there a way to use that instead of os.system?

    Continue reading...

Compartilhe esta Página