Trying to start Chrome from WSGI/Python (admittedly quick-and-dirty) [Win. XP, x86, Python 2.7, Apache 2.2]
So typically I am too stubborn to use forum sites myself (call me proud), but this has been driving me absolutely mad.
My goal is simply to start Chrome on a server machine, through that server's python WSGI script. I am aware of the bug that prevents Python from using Popen under WSGI, but I've figured out what seemed like a workable (albeit somewhat hack-y) solution: instead of calling Popen with the actual command, I created a .bat and called the bat through Popen with the shell argument set to True
. This actually works for just about everything other than what I needed it to; i.e., calling start iexplore
worked, start firefox
worked, even notepad
and mspaint
, but the only thing that didn't work, was calling start chrome
. I've even tried a few command-line arguments, like start chrome --single-process
, but to no avial.
It's like Windows just hates Chrome. I would greatly appreciate any help, as I've been pulling my hair out over this for the last 48 hours.
tl;dr: Chrome won't start from a batch script called using subprocess.Popen
, but everything else will.
Relevant Technical Stuff
Python script [excerpt]:
import subprocess
command="<path>\\start-browser.bat" # Absolute path to Batch file
subprocess.Popen(command,shell=True)
Batch file:
start /d "C:\Documents and Settings\<Me>\Local Settings\Application Data\Google\Chrome\Application" chrome.exe
System:
- Windows XP sp3, 32-bit
- Python 2.7
- Apache 2.2
Answer
It seems like you're dealing with a peculiar issue where you can successfully start other applications (like iexplore
, firefox
, notepad
, etc.) from a batch file, but Chrome doesn't start as expected. Given the setup you're using (Python WSGI script, Apache, and Windows), there are several things we can explore to try to get Chrome working through the batch file and subprocess.Popen
.
Potential Issues and Solutions:
-
Environment Variables & Permissions:
- Check if Chrome is properly in your PATH: Even though you're specifying the full path to
chrome.exe
in your batch file, there could be some weird behavior around the environment variables when running under the WSGI context. - Ensure that the user under which Apache is running (often
www-data
or a similar user) has the necessary permissions to launch Chrome. This could be the root cause if permissions are restricted.
- Check if Chrome is properly in your PATH: Even though you're specifying the full path to
-
Full Path for Chrome:
- While you're using the
start
command with/d
to specify the directory, it's often a good idea to be more explicit in Windows. Try providing the full path tochrome.exe
directly rather than relying on thestart
command. For example, change your batch file to:
"C:\Program Files\Google\Chrome\Application\chrome.exe"
If you're not sure of the path, you can search for
chrome.exe
and confirm the location. - While you're using the
-
Running Chrome in the Background:
- If Chrome is not showing up or it’s being launched in the background without a visible UI, you might want to make sure that it runs in a way that doesn't depend on a specific GUI session (as WSGI can be running without the necessary graphical context).
- Try adding some flags to ensure that Chrome runs in a detached or headless mode (if you want it to open in the background). For instance:
start chrome --headless --disable-gpu --remote-debugging-port=9222
This will start Chrome without the UI, which might help in a server-side environment.
-
Use
subprocess.Popen
Properly:- The
Popen
function, when called withshell=True
, should work for most cases, but it's possible there’s an issue with how the batch file is invoked through Apache's WSGI environment. - Try directly calling
chrome.exe
from Python without using a batch file. Here's an example of how you could modify your script:
import subprocess command = r'"C:\Program Files\Google\Chrome\Application\chrome.exe"' subprocess.Popen(command, shell=True)
This eliminates the need for the batch file altogether, and if there's any issue with the batch script itself, this would help isolate it.
- The
-
WSGI Environment & User Context:
- Since you're running this through WSGI (likely in Apache), make sure the web server has the necessary permissions to launch GUI applications. Windows services (including Apache) might run under a restricted user account without access to the desktop environment or permission to launch applications like Chrome.
- Try setting the appropriate environment variables in the WSGI script, or try running Apache as a user with higher privileges, such as a local admin account.
-
Try Using a Different Python Version:
- Although it's unlikely, Python 2.7 is outdated and could have compatibility issues with certain subprocess calls or environment handling in WSGI. If possible, test with Python 3.x, which has better handling of subprocesses and environmental issues on Windows.
Debugging:
-
Log the output of the
subprocess.Popen
call:- Capture the output (standard and error) of the subprocess to get more insight into what's failing. Modify the call like this:
import subprocess command = r'"C:\Program Files\Google\Chrome\Application\chrome.exe"' process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() print("STDOUT:", stdout) print("STDERR:", stderr)
This will print out any error messages or additional information that might help identify why Chrome isn't starting.
Summary:
- Permissions: Ensure the Apache user has permission to launch Chrome and that it's not being restricted by environment settings.
- Use Full Path: Explicitly provide the full path to
chrome.exe
. - Headless Mode: Try launching Chrome in headless mode to avoid GUI issues in the server context.
- Log Output: Capture and inspect the stdout/stderr to debug any issues that occur during the subprocess call.
Give these suggestions a try, and see if it helps you troubleshoot why Chrome isn't starting! Let me know if any specific error messages pop up during the debugging process, and I can help you further.