Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No module named django.core when creating project in virtualenv

So I have looked around at a lot of questions similar to mine, however I couldn't find a concrete answer. My comp specifications are Windows 7 64-bit.

My problem is as such:

1) I installed virtualenv using pip:

pip install virtualenv

2) After that I created and activated a new environment:

path/virtualenv env
...
path/to/env/Scripts/activate

3) While running the new environment, I installed django:

(env) path/pip install django

4) After installing successfully, I am ready to make a project. However, upon trying it out:

path/django-admin.py startproject test

I get the following error:

File "C:/path/env/Scripts/django-admin.py", line 2, in (module)
    from django.core import management
ImportError: No module named django.core

I have tried out various solutions people have posted, including using the full path:

python C:/path/to/django-admin.py startproject test

I have also checked to make sure the versions of Python it is referencing are correct,as both inside and outside the virtualenv it is associated with Python27. Many other solutions talked about PYTHONPATH or the syspath, however, when I import django or managemnet in the python shell, those work fine.

I have a feeling it may have something to do with the paths, but I'm not sure how a virtualenv interacts with the system paths. Since it is self-contained and the system paths are system wide, is it necessary to have something in the path specifically?

As an aside, my django-admin.py file is in both

path/env/Scripts

and

path/env/Lib/site-packages/django/bin

and the django folder is in

path/env/Lib/site-packages

How to fix this problem?

like image 278
user1549620 Avatar asked Nov 08 '12 03:11

user1549620


People also ask

How to fix No module named Django?

No module named Django macOS To resolve this error, you have to install Django. And, before installing Django must check Python is installed or not. If already installed install pip package, and if not installed install python first and then pip package also. And, after this finally, install Django.

What is Django core management?

management. base BaseCommand Example Code. BaseCommand is a Django object for creating new Django admin commands that can be invoked with the manage.py script. The Django project team as usual provides fantastic documentation for creating your own commands.


8 Answers

I solved this problem by using this command as following instead:

django-admin startproject

just remove the ".py" attached to "django-admin"

like image 149
Haziq Avatar answered Oct 19 '22 15:10

Haziq


In Windows, you set:

set PATH=C:\virtualenv\python2.7\Scripts REM Scripts folder contains python.exe, pip.exe, django-admin.exe,...
set PYTHONPATH=C:\virtualenv\python2.7\Lib\site-packages REM site-packages folder contains packages of python such as django,...

After, create project mysite by

django-admin.py startproject mysite
like image 39
tnductam Avatar answered Oct 19 '22 15:10

tnductam


I could not get any other stack overflow answers to work either. Getting a venved Django stack running on Win64 is a bit of an ordeal.

But, I found an answer that worked for me here: http://samudranb.com/2012/06/02/how-to-setup-a-djangopython-development-env-on-windows/

Try running from an admin command prompt:

ftype Python.File="[your venv path]\Scripts\python.exe" "%1" %*

Just be sure to set it back to the original value when you're done.

like image 36
Matt Conrad Avatar answered Oct 19 '22 13:10

Matt Conrad


This will help you understand why your facing that problem and there is also simple solution for that:

http://blog.jayteebee.org/2009/07/importerror-no-module-named-djangocore.html

like image 33
catherine Avatar answered Oct 19 '22 14:10

catherine


I've literally searched for hours to a solution for this issue... I came across this video randomly: (https://www.youtube.com/watch?v=lPmkl4jtYgA) where he put "python .\Script\django-admin.py startproject" into the command prompt while in a virtual environment, so I tried the same with the following modification to point to the correct path on my machine "python .\env\Script\django-admin.py startproject". Voila!

Hopefully this helps someone as it seems there are multiple reasons for this issue.

like image 43
TW603 Avatar answered Oct 19 '22 14:10

TW603


Windows server 2003 provides the Where command where python.exe

will show the full path of the current python.exe found on the path, use that to check it's using the correct one for your virtualenv.

The association issue comes into play because running file.py so the .py is argv[0] passes it through the windows association, which won't follow your venv.

python file.py will not find file.py unless it's in the current directory.

So the solution is -

python %VIRTUAL_ENV%\scripts\django-admin.py startproject myproject

This runs python from the current active venv and uses the venv env variable so it points to the correct location of django-admin.py (or you could give it an absolute path yourself of course)

like image 22
FLong Avatar answered Oct 19 '22 15:10

FLong


I had the same problem. I solved that using this command: (env)C:\environment directory>python Scripts\django-admin.py

This link was helpful for me: enter link description here

like image 39
MartinP Avatar answered Oct 19 '22 15:10

MartinP


i had the same problem i am running both python 3.4 and 2.7, so i pip installed Django globally on my machine and when i returned to my virtual environment i was able to create a project with no problems.

like image 30
Achim Munene Avatar answered Oct 19 '22 14:10

Achim Munene