Jeremiah Flaga My thoughts and experiences on programming, life, atbp.

ModuleNotFoundError in a Python virtual environment


Experiencing this error when you try to execute commands inside your Python virtual environment?

ModuleNotFoundError: No module named '<name of module>'

I encountered it a while ago.


The reason might be because you transfered your virtual environment folder to another location (or you copied it from another computer, like in my case).

This is how to fix it:

  1. Open the /Scripts/activate file inside your virtual environment folder.

  2. Go to the line that has this (in my case, it is on line #43):
    VIRTUAL_ENV="$(if [ "$OSTYPE" "==" "cygwin" ]; then cygpath -u 'Z:\<your NEW folder>'; else echo '/Z/<your NEW folder>'; fi;)"
  3. Change the path to the new location of your virtual environment. :smile:

    Change it from
    Z:\<your OLD folder> and /Z/<your OLD folder>
    Z:\<your NEW folder> and /Z/<your NEW folder>

  4. If you are using the Command Prompt of Windows, you also have to change the path in the file /Scripts/activate.bat. (line #2 in my case)
    set "VIRTUAL_ENV=Z:\<folder>"