You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Windows用户如何在Git Bash中运行conda base环境的Python脚本

Fixing "/usr/bin/env: ‘python3’: No such file or directory" in Git Bash (Windows + Conda)

Hey there! I’ve run into this exact issue before when using Conda with Git Bash on Windows—let’s get your script running smoothly.

Why This Happens

Git Bash is looking for a python3 executable, but your Conda base environment’s Python is likely named just python.exe (per Windows conventions), and Git Bash might not have Conda’s path properly configured in its environment.


Quick Temporary Fixes (For Immediate Script Run)

  • Option 1: Run the script directly with Conda’s Python
    Skip relying on the shebang line and call Python explicitly in Git Bash:

    python script_1.py
    # Or use the full path if Git Bash can't find `python` yet
    /c/Users/YourUsername/miniconda3/python.exe script_1.py
    

    (Replace YourUsername with your actual Windows username, and adjust the path if you installed Anaconda instead of Miniconda.)

  • Option 2: Update the script’s shebang line
    Open script_1.py and change the first line from:

    #!/usr/bin/env python3
    

    To either:

    #!/usr/bin/env python
    

    Or the full path to your Conda Python executable:

    #!/c/Users/YourUsername/miniconda3/python.exe
    

    Now you can run ./script_1.py as intended.


Permanent Fix (Set Up Git Bash to Recognize Conda Python)

This ensures you never hit this issue again:

  1. Initialize Conda for Git Bash
    In Git Bash, run this command to configure Conda to auto-activate when you open Git Bash:

    conda init bash
    

    Restart Git Bash after running this. You’ll see (base) in your prompt, meaning the base environment is active.

  2. Add a python3 alias (optional but handy)
    If you still want to use python3 commands (or your scripts rely on it), add an alias to your Git Bash profile:

    • Open your .bashrc file in Git Bash:
      nano ~/.bashrc
      
    • Add this line at the bottom:
      alias python3=python
      
    • Save and exit (press Ctrl+O, hit Enter, then Ctrl+X), then restart Git Bash. Now python3 will point directly to your Conda Python.

Verify It Works

Run these commands in Git Bash to confirm everything is set up correctly:

which python  # Should show your Conda base Python path
which python3 # Should either match the above or reference the alias
python --version  # Should display your Conda Python version

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

火山引擎 最新活动