I am experiencing an issue where Git Bash is not recognizing the Python virtual environment set by Poetry, and it defaults to the system's global Python installation instead. However, PowerShell correctly identifies and uses the virtual environment.
Here are the commands and outputs in both shells:
PS H:\Coding\tradido\code> python -c "import sys;print(sys.executable)"
C:\Users\hamid\AppData\Local\pypoetry\Cache\virtualenvs\tradido-cdZ63RI2-py3.11\Scripts\python.exe
hamid@DESKTOP-GJ4J9QV MINGW64 /h/Coding/tradido/code (feature/levels)
$ python -c "import sys;print(sys.executable)"
C:\Program Files\Python311\python.exe
I want Git Bash to use the Python interpreter from the virtual environment as PowerShell does.
The default interpreter is correct but for some reason bash can't find that and uses the global python.
According to this issue, you could find the solution.
This happened because Git Bash uses the system's PATH environment variable to locate executables.
You can do the following steps:
Deactivate the virtual environment using the
exitcommand.Locate the virtual environment's Python executable using the
which pythoncommand.Add the virtual environment's Python executable to the Git Bash PATH variable by modifying the
~/.bashrcfile. You can add the following line to the file:export PATH="$PATH:/path/to/virtual/environment/bin"
Replace
/path/to/virtual/environment/binwith the actual path to your virtual environment's bin directory.source path/to/virtual/environment/bin/activatecommand.