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

Heroku部署Node.js调用Python脚本时找不到Python可执行文件路径问题

Fixing "ENOENT" Error When Running Python Scripts via python-shell on Heroku

Let's work through this issue step by step—this is a common gotcha when mixing Node.js and Python on Heroku, but it’s totally fixable with a few targeted adjustments:

1. Stop Hardcoding Python Paths

Heroku’s dynos use ephemeral filesystems, so the exact path to Python can shift between deployments. Hardcoding paths like /app/.heroku/python/bin/python will break when these paths change. Instead, leverage the environment variables Heroku sets up for you.

The Python buildpack automatically configures PYTHONHOME and adds the Python bin directory to your PATH. You can either:

  • Use python3 directly (modern Heroku Python buildpacks default to Python 3, and python often points to unsupported Python 2)
  • Pull the path dynamically from environment variables

Here’s how to adjust your python-shell config:

const { PythonShell } = require('python-shell');

// Dynamically resolve the Python path with fallbacks
const pythonPath = process.env.PYTHON || `${process.env.PYTHONHOME}/bin/python3` || 'python3';

const options = {
  pythonPath: pythonPath,
  // Pass through the full environment to ensure Python has access to its dependencies
  env: { ...process.env }
};

PythonShell.run('your_script.py', options, (err, results) => {
  if (err) {
    console.error('Python shell error:', err);
    throw err;
  }
  console.log('Script output:', results);
});

2. Confirm Your Python Buildpack is Fully Activated

Even if you enabled the Python buildpack, Heroku requires a requirements.txt file in your repo root to trigger it—even if the file is empty! Without this, Heroku might skip setting up the Python environment entirely, leading to missing executable paths.

  • Create an empty requirements.txt if you have no Python dependencies:
    touch requirements.txt
    
  • Commit the file and redeploy.

Check your build logs (run heroku logs --tail after deploying) to confirm the Python buildpack runs successfully—look for lines like -----> Installing python-3.x.x or -----> Installing requirements with pip.

3. Debug Runtime Paths

To narrow down issues, add quick log statements to verify what environment variables are available to your Node.js app:

console.log('PYTHONHOME:', process.env.PYTHONHOME);
console.log('PATH:', process.env.PATH);
console.log('Which python3:', require('child_process').execSync('which python3').toString().trim());

Deploy this, then check your Heroku logs. This will show you the exact runtime path to Python, helping you validate your dynamic path logic.

4. Lock Your Python Version (If Needed)

If you rely on a specific Python version, specify it in a runtime.txt file (e.g., python-3.11.4). Heroku will install that exact version, and PYTHONHOME will align with the correct path.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:25:52