Windows用户如何在Git Bash中运行conda base环境的Python脚本
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
YourUsernamewith your actual Windows username, and adjust the path if you installed Anaconda instead of Miniconda.)Option 2: Update the script’s shebang line
Openscript_1.pyand change the first line from:#!/usr/bin/env python3To either:
#!/usr/bin/env pythonOr the full path to your Conda Python executable:
#!/c/Users/YourUsername/miniconda3/python.exeNow you can run
./script_1.pyas intended.
Permanent Fix (Set Up Git Bash to Recognize Conda Python)
This ensures you never hit this issue again:
Initialize Conda for Git Bash
In Git Bash, run this command to configure Conda to auto-activate when you open Git Bash:conda init bashRestart Git Bash after running this. You’ll see
(base)in your prompt, meaning the base environment is active.Add a
python3alias (optional but handy)
If you still want to usepython3commands (or your scripts rely on it), add an alias to your Git Bash profile:- Open your
.bashrcfile 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
python3will point directly to your Conda Python.
- Open your
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




