elementzero23
elementzero23

Reputation: 1429

No module named django.core when running django-admin startproject myproject

when running django-admin startproject myproject on macOS I get the error

Traceback (most recent call last):
File "/usr/local/bin/django-admin", line 2, in
from django.core import management
ImportError: No module named django.core

I checked out this question but running import django won't produce any output in a python3 shell.

/usr/local/bin/django-admin is a symlink to /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/bin/django-admin.py.

I already put /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django in my PYTHONPATH as suggested in other questions.

Am I missing something?

Upvotes: 3

Views: 3642

Answers (4)

sptramp
sptramp

Reputation: 946

On MacOS, use sudo before the command:

sudo django-admin startproject myproject

Upvotes: 0

Manan Mehta
Manan Mehta

Reputation: 5929

Even if you make it work, it is not good practice to do what you're doing! Ideally, the only python-related binaries you would want in /usr/local/bin/ would be python, pip and virtualenv (or venv, pyvenv)...

I would suggest you to delete /Library/Frameworks/Python.framework/Versions/3.6 ONLY IF you installed it there. As far as I know, macOS only comes with python2.7 installed and not python3.6!

Then open a new shell and try this:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install python3
pip3 install virtualenv
cd ~/Desktop/
mkdir proj
cd proj
virtualenv -p python3 env
source env/bin/activate
pip install django
django-admin.py startproject testproj

skip the first step if you already have brew installed

Upvotes: 5

McGlothlin
McGlothlin

Reputation: 2099

Check your permissions in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ with an ls -la command. If you see anything owned by root, this probably needs to change. I suspect that since you installed some packages as root, the permissions are weird and it can't find the module. If this is the case, reinstall the package(s) as your own user using sudo.

Another thing you should check: as phd mentioned you need to make sure you're using the version of python you think you are. Check this by running which python to tell you the location of the one you're referencing, and python --version to tell you which version you're using. If it's not Python 3.6, then you installed Django for a different version of Python. In this case, simply install Django for version 3.6 and you'll be on your way.

For future reference, Python offers a module called venv to prevent version mishaps like this. More info can be found here.

Upvotes: 2

drsnark
drsnark

Reputation: 3033

It seems like you are trying to create or work on a django project without using a python virtual environment. I recommend reviewing the python 3 venv documenation (https://docs.python.org/3/library/venv.html). Then creating a virtual environment (venv) specifically for your web application. Once you you that project's venv setup you can install django into that venv.

Upvotes: 1

Related Questions