Restarts the script inside the virtual environment if not already inside it.
()
| 124 | |
| 125 | |
| 126 | def run_script_in_venv(): |
| 127 | """Restarts the script inside the virtual environment if not already inside it.""" |
| 128 | python_exec = get_venv_python() |
| 129 | |
| 130 | # Avoid infinite loops by checking if we're already inside the venv |
| 131 | if sys.prefix == os.path.abspath(VENV_DIR): # Already inside the venv |
| 132 | prGreen("Script is already running inside the virtual environment.") |
| 133 | return # Do nothing, exit the function |
| 134 | |
| 135 | print(f"Running script inside virtual environment: {VENV_DIR}") |
| 136 | |
| 137 | # Ensure that the python executable exists |
| 138 | if not os.path.isfile(python_exec): |
| 139 | prRed(f"Error: Python executable not found at {python_exec}") |
| 140 | sys.exit(1) |
| 141 | |
| 142 | # Build the command to restart the script inside the virtual environment |
| 143 | command = [python_exec] + sys.argv |
| 144 | |
| 145 | # Explicitly set the working directory to where the script is |
| 146 | script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 147 | |
| 148 | try: |
| 149 | print(f"Restarting the script with command: {command}") |
| 150 | subprocess.check_call(command, cwd=script_dir, stderr=subprocess.STDOUT) |
| 151 | except subprocess.CalledProcessError as e: |
| 152 | prRed(f"Error during restart: {e}") |
| 153 | sys.exit(1) |
| 154 | |
| 155 | sys.exit() # Ensure the script exits after launching the new one |
| 156 | |
| 157 | |
| 158 | # Setup environment and dependencies |
no test coverage detected