Namwoo Kim
Namwoo Kim

Reputation: 95

Python + Django + Virtualenv importing django.core failed

I'm using Python 2.7.3 on Windows 7. I've set PATH as a C:\python27 which is a original python binary path.

First, I made a new Virtualenv named "django" without any options,

virtualenv django

Second, activated Virtualenv,

c:\workspace\py-envs\django\Scripts\activate

Third, installed Django by using pip,

pip install django

Fourth, just executed django-admin.py startproject SOME_NAME like below.

Then, I faced an issue while importing django.core package.

FAILED

(django) c:\workspace\python>django-admin.py startproject a
(django) c:\workspace\python>python django-admin.py startproject a
(django) c:\workspace\python>c:\py-envs\django\Scripts\python django-admin.py startproject a

ERROR message

Traceback (most recent call last):
  File "C:\workspace\py-envs\django\Scripts\django-admin.py", line 2, in <module>
    from django.core import management
ImportError: No module named django.core

WORKED

(django) c:\workspace\python>python c:\py-envs\django\Scripts\django-admin.py startproject a

CHECKED

I'd like to start a project by using the first command:

python django-admin.py startproject a

What else I can do?

Upvotes: 4

Views: 1298

Answers (2)

St&#233;phane Bruckert
St&#233;phane Bruckert

Reputation: 22913

Under a virtual environment (virtualenv), the only default way to call django-admin is to call it by django-admin.py.

The following works:

  • django-admin.py startproject PROJECT_NAME,

The following doesn't:

  • django-admin startproject PROJECT_NAME,
  • python django-admin startproject PROJECT_NAME,
  • python django-admin.py startproject PROJECT_NAME.

Upvotes: 2

Mateus Padua
Mateus Padua

Reputation: 193

This happen because the windows python interpreter use the global interpreter always and not the current python virtualenv interpreter.

Example:
C:\python27\python.exe # windows always use it

and not this

C:\envs\my_env\Scripts\python.exe

In oficial documentation, I found this: http://docs.python.org/2/using/cmdline.html?highlight=#-m
"When called with -m module-name, the given module is located on the Python module path and executed as a script."

if you type in console, echo %PATH%, will see the virtualenv path in first. Therefore

(django) c:\workspace\python>python -m django-admin startproject my_new_project  

should work

Upvotes: 1

Related Questions