[Django]-ImportError: No module named virtualenv

128πŸ‘

βœ…

Install virtualenv using pip install virtualenv.
If you have it already installed, try reinstalling it by removing it with pip uninstall virtualenv and then reinstalling it.
Good Luck.

πŸ‘€Aurora

22πŸ‘

I had to install virtualenv with the -H flag to set HOME variable to target user’s home dir.

sudo -H pip install virtualenv

πŸ‘€Bailey Smith

13πŸ‘

Use pip3 instead of pip. I had the same issue and pip3 worked for me.

$ pip3 install virtualenv
$ virtualenv venv --python=python3
πŸ‘€Chirag Kalal

13πŸ‘

Try

python3 -m pip uninstall virtualenv

python3 -m pip install virtualenv
πŸ‘€shanwu

12πŸ‘

I think the problem is you need sudo to globally install virtualenv.

> pip install virtualenv
Could not find an activated virtualenv (required).
> sudo pip install virtualenv
Downloading/unpacking virtualenv
...

But this creates files readable only by root (depending on the umask).
In this case, uninstalling/reinstalling may not always help.

You can check with ls -la /usr/local/lib/python2.7/dist-packages/virtualenv.py (replacing 2.7 with whatever version you have or are targeting).

My solution was simply:

sudo chmod -R o+rX /usr/local/lib/python2.7
πŸ‘€jozxyqk

5πŸ‘

I just ran into this same problem. I had to pip uninstall virtualenv as a user with admin rights, then pip install virtualenv as a normal user. I think it’s some kind of permissions issue if you installed virtualenv under admin rights.

πŸ‘€brousch

1πŸ‘

For mac os the issue was with virtualenv. This is because the folder virtualenv did not exist.

This worked well

python3 -m venv env

πŸ‘€Vinz

0πŸ‘

>virtualenv
ImportError: No module named 'virtualenv'
>pip uninstall virtualenv
PermissionError: [Errno 13] Permission denied:

>sudo pip uninstall virtualenv
Successfully uninstalled virtualenv-15.1.0
>pip install virtualenv
Collecting virtualenv

>virtualenv
Options:

Bingo!

πŸ‘€Shuai Li

0πŸ‘

I had the same problem when I created my virtualenv via pycharm and installed requirements with pycharm.
After trail and error , I found that installed requirements are not taken into account by the virtualenv.

The solution is to reinstall all requirements once you have activated your virtualenv:

venv\scripts\activate

python -m pip install -r YourRequirements.txt

Next time I’d better create my virtualenv directly with command line

πŸ‘€petitchamp

0πŸ‘

Got this error when using the ansible pip module automating some pip installs on my localhost.

fatal: [localhost]: FAILED! => {"changed": false, "cmd": ["/opt/bin/virtualenv", "--system-site-packages", "-p/usr/bin/python3", "/opt/venv/myenv"], "msg": "\n:stderr: /usr/bin/python3: No module named  virtualenv\n"}

Uninstalling virtualenv python3 -m pip uninstall virtualenv did show virtualenv was installed here /home/ubuntu/.local/bin/virtualenv.

In the ansible task specify the virtualenv_command:

- name: install requirements file
  pip:
    virtualenv_command: "/home/{{whoami.stdout}}/.local/bin/virtualenv"
    virtualenv: "/home/{{whoami.stdout}}/.venv/{{item.env.virtualenv}}"
    requirements: "/home/{{whoami.stdout}}/git/{{item.env.requirements_txt}}"
    virtualenv_site_packages: yes
  when: req_stat.stat.exists

0πŸ‘

Poetry wants to be in venv by default so I used venv in docker. I got the error randomly after multiple month of using that setup.

If this is the case for you, just don’t use venv in docker. You can turn off venv requirement for poetry by: /usr/bin/poetry config virtualenvs.create false.

It is also possible to export poetry into requirements.txt by poetry export -f requirements.txt --output requirements.txt.

πŸ‘€Kuizame

0πŸ‘

sudo apt install python3-virtualenv

worked for me: (WSL env)

sudo apt install python3-virtualenv

πŸ‘€Omer

Leave a comment