Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-admin pointing outside virtualenv

I've initialised a virtual environment using the virtualenvwrapper command mkvirtualenv -a <path to project> django_project.

I then installed django with pip install django. But then if i try to use django-admin i get:

Traceback (most recent call last):
File "/usr/local/bin/django-admin", line 7, in <module>
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'

Now pip list gives me

Package    Version
---------- -------
Django     2.1.3
pip        18.1
pytz       2018.7
setuptools 40.6.2
wheel      0.32.3

python -m django --version gives

2.1.3

If I run which python it correctly points to my virtualenv, however which django-admin gives:

/usr/local/bin/django-admin

I'd think that it should point to my venv. Why would it point to a global django admin? How do I fix it so that it'll work for my future virtual environments?

I'm on MacOS using zsh and python 3.7.0.

Thank you!

Edit: Mistake in a command

Edit: I realised I don't have a system-wide installation of Django and so the django-admin and django-admin.py files in my /usr/local/bin must've been leftovers from an earlier installation. Hence I deleted them and that solved the problem. Without any further django-admin inside the venv point to the correct django installation (inside the venv).

However, I would still like to know why the command didn't point to the Django installed in the venv in the first place?

like image 473
Morten Avatar asked Sep 03 '25 10:09

Morten


1 Answers

So Django has been installed at system level, while you verified that python command refer to your virtual environment. I bet this is an issue with pip. You may check that it is under <path to project>/bin and is correctly used when you perform

(django_project) $ pip install django

Try to run

which pip

with your venv enabled and disabled to see what pip is used in each case

like image 163
Antwane Avatar answered Sep 05 '25 01:09

Antwane