From 12b6447a87da8077c7dd12b92eefc27dcf7f0818 Mon Sep 17 00:00:00 2001 From: yum Date: Wed, 8 Mar 2023 15:36:46 -0800 Subject: Set PYTHONPATH in synchronous multiprocessing layer A user saw an error like `ModuleNotFoundError: No module named _socket`. StackOverflow blames this on PYTHONPATH, so let's try setting it. * Fix latent bug in Scripts/transcribe.py. PyAudio.open() positional parameters must be specified in correct order, even when telling it which parameter is which. *shrug* --- GUI/GUI/GUI/PythonWrapper.cpp | 34 +++++++++++++++++++++++++++++----- Scripts/transcribe.py | 6 ++++-- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/GUI/GUI/GUI/PythonWrapper.cpp b/GUI/GUI/GUI/PythonWrapper.cpp index 148692a..78947ed 100644 --- a/GUI/GUI/GUI/PythonWrapper.cpp +++ b/GUI/GUI/GUI/PythonWrapper.cpp @@ -268,26 +268,50 @@ bool PythonWrapper::InvokeCommandWithArgs(const std::string& cmd, // Add updated PATH to current process's environment if (!SetEnvironmentVariableA("PATH", env.c_str())) { std::ostringstream err_oss; - err_oss << "Error while executing python command \"" << cmd_oss.str() - << "\": Failed to add git to PATH: " << GetWin32ErrMsg() << std::endl; + err_oss << "Error while executing python command \"" + << cmd_oss.str() + << "\": Failed to add git to PATH: " << GetWin32ErrMsg() + << std::endl; out_cb("", err_oss.str()); return false; } } + + // Add python scripts to PATH std::filesystem::path py_bin = (std::filesystem::current_path() / - "Resources\Python\Scripts").lexically_normal(); + "Resources/Python/Scripts").lexically_normal(); if (env.find(py_bin.string()) == std::string::npos) { env += ";" + py_bin.string(); // Add updated PATH to current process's environment if (!SetEnvironmentVariableA("PATH", env.c_str())) { std::ostringstream err_oss; - err_oss << "Error while executing python command \"" << cmd_oss.str() - << "\": Failed to add git to PATH: " << GetWin32ErrMsg() << std::endl; + err_oss << "Error while executing python command \"" + << cmd_oss.str() + << "\": Failed to add python scripts to PATH: " + << GetWin32ErrMsg() << std::endl; out_cb("", err_oss.str()); return false; } } + + // Set up PYTHONPATH + std::filesystem::path py_lib = (std::filesystem::current_path() / + "Resources/Python/Lib").lexically_normal(); + std::filesystem::path py_site_pkgs = (std::filesystem::current_path() / + "Resources/Python/Lib/site-packages").lexically_normal(); + std::ostringstream pypath_oss; + pypath_oss << py_lib.string(); + pypath_oss << ';' << py_site_pkgs.string(); + // Add updated PATH to current process's environment + if (!SetEnvironmentVariableA("PYTHONPATH", pypath_oss.str().c_str())) { + std::ostringstream err_oss; + err_oss << "Error while executing python command \"" << cmd_oss.str() + << "\": Failed to add site-packages to PYTHONPATH: " + << GetWin32ErrMsg() << std::endl; + out_cb("", err_oss.str()); + return false; + } } std::string cmd_str = cmd_oss.str(); diff --git a/Scripts/transcribe.py b/Scripts/transcribe.py index c4d7682..6793336 100644 --- a/Scripts/transcribe.py +++ b/Scripts/transcribe.py @@ -167,8 +167,10 @@ def getMicStream(which_mic): # Bind audio_state to onAudioFramesAvailable callback = partial(onAudioFramesAvailable, audio_state, input_rate) - audio_state.stream = audio_state.p.open(format=audio_state.FORMAT, - channels=audio_state.CHANNELS, rate=input_rate, + audio_state.stream = audio_state.p.open( + rate=input_rate, + channels=audio_state.CHANNELS, + format=audio_state.FORMAT, input=True, frames_per_buffer=audio_state.CHUNK, input_device_index=device_index, stream_callback=callback) -- cgit v1.2.3