Morten
Morten

Reputation: 199

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?

Upvotes: 4

Views: 1591

Answers (2)

subash rimal
subash rimal

Reputation: 11

I had the same problem, but after reboot it solved the issue for me.

Don't forget to check if the executable django-admin is inside the virtualenv's bin folder:

~/Documents/Django_python/django/bin $ ls
activate                deactivate.nu           pip3.10                 wheel
activate.fish           *django-admin*          python                  wheel3
activate.nu             pip                     python3                 wheel3.10
activate.ps1            pip3                    python3.10              wheel-3.10activate.csh
activate_this.py        pip-3.10                sqlformat

(*) Highlight mine.

Upvotes: 0

Antwane
Antwane

Reputation: 22628

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

Upvotes: 2

Related Questions