You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel借助Symfony Process调用Python脚本失败求助

Fixing "Python is not recognized" Error in Laravel with Symfony Process

Hey there, let's work through why your Symfony Process call to Python isn't working in Laravel, even though the script runs fine in CMD! The core issue here is almost always related to environment variables or command formatting differences between your user's CMD session and the web server's runtime environment. Here are the most reliable fixes:

1. Use the Full Path to Your Python Executable

Your Laravel app runs under the web server's user context (like IIS AppPool, Apache, or PHP-FPM), which might not have Python in its system PATH—even if your user account does. The simplest fix is to call Python using its full file path instead of just Python.

For example, if your Python is installed at C:\Users\Bcc\AppData\Local\Programs\Python\Python310\python.exe, update your code like this:

$text = "I luv my <3 iphone & you're awsm apple. DisplayIsAwesome, sooo happppppy 🙂 http://www.apple.com";

// Use full paths and pass command arguments as an array (safer than a single string)
$pythonExecutable = "C:\\Users\\Bcc\\AppData\\Local\\Programs\\Python\\Python310\\python.exe";
$scriptPath = "C:\\Users\\Bcc\\PycharmProjects\\twitter-data\\Car.py";

$process = new Process([$pythonExecutable, $scriptPath, $text]);
$process->run();

// Check results
if ($process->isSuccessful()) {
    dd($process->getOutput());
} else {
    dd($process->getErrorOutput());
}

Using an array for the command arguments avoids messy quote escaping and ensures Symfony Process handles spaces/special characters correctly.

2. Update the Web Server's Environment Variables

If you prefer not to hardcode the Python path, you can add Python's installation directory to the web server's system PATH:

  • On Windows, go to System Properties > Advanced > Environment Variables
  • Under "System Variables", edit the Path entry and add your Python folder (e.g., C:\Users\Bcc\AppData\Local\Programs\Python\Python310)
  • Restart your web server (and your system if needed) to apply the changes

Note: Some web servers (like IIS) might require you to restart the entire server process to pick up new environment variables.

3. Fix Path Escaping and Command Formatting

In your original code, you used a single string for the command with unescaped backslashes—this can cause parsing issues in PHP. Always:

  • Escape backslashes in Windows paths (use \\ instead of \)
  • Avoid wrapping the entire command in a single string; use the array syntax as shown above for better reliability

4. Set the Working Directory (If Needed)

If your Python script relies on files in its own folder (like configs or data files), set the Process working directory to match the script's location:

$process->setWorkingDirectory("C:\\Users\\Bcc\\PycharmProjects\\twitter-data");

These steps should resolve the "Python not recognized" error—start with the full path fix, as it's the most straightforward and least prone to environment-related issues.

内容的提问来源于stack exchange,提问作者Mohammad Mustafa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:33:33