Python + Django + Virtualenv django.core error

I am using Python 2.7.3 for Windows 7. I installed PATH as C:\python27 , which is the original python binary path.

Firstly, I created a new Virtualenv named "django" without any options,

 virtualenv django 

Secondly, activated Virtualenv,

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

Thirdly, Django is installed using pip,

 pip install django 

Fourth, just do django-admin.py startproject SOME_NAME , as shown below.

Then I ran into a problem while importing the 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 

HAVE WORKED

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

TESTED

  • django-admin.py exists in c:\py-envs\django\Scripts\
  • virtualenv added the line c:\py-envs\django\Scripts\ to PATH (marked with echo% PATH%)
  • pip freeze result shows only Django == 1.5

I would like to start a project using the first command:

 python django-admin.py startproject a 

What else can I do?

+4
source share
2 answers

In a virtual environment (virtualenv), the only way to invoke django-admin by default is to invoke its django-admin.py .

The following works:

  • django-admin.py startproject PROJECT_NAME ,

It is impossible:

  • django-admin startproject PROJECT_NAME ,
  • python django-admin startproject PROJECT_NAME ,
  • python django-admin.py startproject PROJECT_NAME .
+2
source

This is because the python windows interpreter always uses the global interpreter, and not the current python virtual virtual language interpreter.

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

but not

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

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

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

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

must work

+1
source

All Articles