ModuleNotFoundError in a Python virtual environment

June 29 · 1 min read

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.

python-ModuleNotFoundError.png

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>
    to
    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>"
    
Buy Me A Coffee