Git Bash does not recognize Python virtual environment created by Poetry

135 Views Asked by At

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.

1

There are 1 best solutions below

1
MingJie-MSFT On

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:

  1. Deactivate the virtual environment using the exit command.

  2. Locate the virtual environment's Python executable using the which python command.

  3. Add the virtual environment's Python executable to the Git Bash PATH variable by modifying the ~/.bashrc file. You can add the following line to the file:

    export PATH="$PATH:/path/to/virtual/environment/bin"

Replace /path/to/virtual/environment/bin with the actual path to your virtual environment's bin directory.

  1. Activate the virtual environment using the source path/to/virtual/environment/bin/activate command.