pythongitvirtualenv

Git error with Python virtual environment. Can not execute git add


I am relatively new to python virtual environments and I recently set one up while starting a new backend project to learn django, for which I also initialized a git repo. I seemingly set up everything correctly, however, when I reopen the project I noticed that I could no longer commit changes to my git repo. I get the following error when I try git add . :

fatal: unable to access 'C:\Users?peri/.config/git/config': Invalid argument

A few things I have noticed that are odd:

  1. When I open the project the address shown in the terminal does not point to where my project is. The terminal should be pointing to 'C:/Users/vperi/Documents/Python Dev/WebDev/online_marketplace where the project is found but instead the following address is shown in the terminal C:/Users/vperi/AppData/Local/Programs/Microsoft VS Code.
  2. It also appears that a virtual environment is already running when I open the project in vs code (not the one I set up).
  3. When I cd into the correct folder and start the virtual environment for the project the terminal shows that 2 virtual environments are active (see screenshot).enter image description here

I've tried deactivating the virtual environment mentioned in point 2, changing directories to my project directory, and then trying git add . but still get the same 'fatal' error.

I assume this has to do with git bash and my virtual environment but am new to python virtual environments so I'm pretty lost at the moment.


Solution

    1. Delete .gitconfig file in C:\Users\vperi\.gitconfig
    2. Navigate to C:/Users/vperi/Documents/Python Dev/WebDev/online_marketplace and delete any .venv folders

    I suspect that there are 2 .venv folders so check carefully

    Delete the .venv folders

    1. Right click on vscode (I am assuming you pinned it to taskbar and click New Window

    2. Navigate to your project folder

    3. After the window fully loads, open a new terminal, make sure its in the correct path. and run:

    virtualenv venv
    

    MAKE SURE YOU ARE USING POWERSHELL And run

    .\venv\Scripts\activate.ps1
    

    If this shows an error, then run this:

    Set-ExecutionPolicy Bypass -Scope Process -Force; Invoke-Expression ".\venv\Scripts\activate.ps1"
    
    1. Press ctrl+comma to open up the settings and search for python.terminal.activateEnvironment And set it to false

    Using git bash is not recommended unless you have a specific need to use bash-like interfaces.

    Explanation:

    1. Clearly, your git config is messed up, so I suggested you delete it
    2. I highly suspect you have 2 .venv folders, so I suggested you delete them and start from scratch
    3. By setting python.terminal.activateEnvironment to false you are disabling the autoload feature of vscode that loads virtual environments
    4. I suggest you stick to powershell for now because its much easier dealing with powershell if you are developing on windows