In python when running a multiprocessing process from a subdirectory in the project folder, the process has problem of importing the needed modules from the root directory.
Specifically I am trying to create a program that opens Edge web browser using pyautogui library. In this program I want to handle specific case like when the connection can be only opened through HTTP which shows a warning page before accessing the page. Since this warning shows only in specific cases I want to run the detection and handling logic in a separate process to not block the main thread. The problem is this open_browser function is a utility function which is utilized by many other functions in my code. This means that the multiprocess is spawned from directory 2 layers deep in the root directory.
main.py
from tests.utils.browser_utils import open_browser
def main():
open_browser("https://192.168.7.239")
if __name__ == "__main__":
main()
./tests/utils/browser_utils.py
import multiprocessing
from utils.multiprocess import process_wrapper
def open_browser(url):
# Logic for opening the browser and desired url
allow_http_traffic_process = multiprocessing.Process(
target=process_wrapper, args=(allow_http_traffic,), name="Allow http traffic"
)
allow_http_traffic_process.start()
# Additional logic to verify if page opened successfully
def allow_http_traffic():
# Try to locate and close the allow HTTP traffic
./tests/utils/process.py
import os
import sys
def process_wrapper(func, *args, **kwargs):
current_dir = os.path.dirname(os.path.abspath(__file__))
parent_dir = os.path.dirname(current_dir)
project_dir = os.path.dirname(parent_dir)
if current_dir not in sys.path:
sys.path.insert(0, current_dir)
if parent_dir not in sys.path:
sys.path.insert(0, parent_dir)
if project_dir not in sys.path:
sys.path.insert(0, project_dir)
os.chdir(project_dir)
return func(*args, **kwargs)
As shown in the code i tried to create a wrapper function which changes working directory and adds the project root directory to the processes path, which actually works and the path is added but the module importing still fails. Exception. The other option is to make the allow_http_traffic function into a separate python file and run it using subprocess module as a standalone process, which may solve this issue but it would not make sense in my project directory and contextual structure.