Loctarogar
Loctarogar

Reputation: 620

Can't use Django in virtualenv

I have two Django projects and I created two different virtualenv for them. When I create another one virtualenv and install Django and create a django project I tried python manage.py runserver and have this error:

Traceback (most recent call last):
  File "manage.py", line 8, in <module>
    from django.core.management import execute_from_command_line
ImportError: No module named 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    import django
ImportError: No module named 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 17, in <module>
    "Couldn't import Django. Are you sure it's installed and "
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

What I need to do? I already tried uninstalling Django, pip, virtualenv and re-install:

sudo apt-get install python3-pip
sudo pip3 install virtualenv
sudo virtualenv ENV
source newenv/bin/activate
sudo -H pip3 install django

Upvotes: 1

Views: 1054

Answers (1)

CasualDemon
CasualDemon

Reputation: 6158

Using sudo with virtualenvs can cause a lot of scope issues, as well as a virture of virtualenvs is that you shouldn't need root permissions for them (in most cases).

Also if you have virtaulenv installed for python 2 as well, it might be defaulting to that one.

sudo apt-get install python3-pip
sudo pip3 install virtualenv

# I prefer using this over `virtualenv --python=/usr/bin/python3 ENV`
python3 -m venv ENV 
source ENV/bin/activate

# Can do a `which pip3` here to make sure it's using the ENV one
pip3 install django

# Could also do full path of `ENV/bin/pip3 install django`

Upvotes: 1

Related Questions