dWitty
dWitty

Reputation: 524

Django syntaxError when starting django project in virtualenv with python 3

A question very similar to this one already exists, but it is regarding python2. In another related question the user did not activate their virtual environment. I did.

After I encountered this problem in a larger project, I tried and succeeded in replicating it on a brand new project.

In empty folder test-django, I ran the following:

virtualenv env
env/scripts/activate
pip install django

to make sure python had successfully installed django, I tried:

python
import django

No import error was raised, and the version of Python running was Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49)

so then I started the project...

django-admin startproject test

project test was indeed successfully created. I then ran:

cd test
./manage.py

and got

File "test-django\test\manage.py", line 14 ) from exc ^ SyntaxError: invalid syntax

The contents of manage.py are:

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "phoenix2.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)

when I remove "from exc", the ImportError is raised successfully-- but I shouldn't be getting an ImportError, running from within a virtual environment that has django installed.

I tried running (from Python within the command line)

 from django.core.management import execute_from_command_line

and did not get an ImportError

the contents of sys.path within the virtual environment:

['', '\test-django\env\Scripts\python36.zip', '\test-django\env\DLLs', '\temp\test-django\env\lib', '\test-django\env\Scripts', 'c:\python36\Lib', 'c:\python36\DLLs', '\test-django\env', '\test-django\env\lib\site-packages']

running django-admin --version: 2.0.5

in case it is helpful, here's the general system python's path contents:

['', 'C:\Python36\python36.zip', 'C:\Python36\DLLs', 'C:\Python36\lib', 'C:\Python36', 'C:\Python36\lib\site-packages']

pip freeze within virtualenv:

Django==2.0.5 pytz==2018.4

pip freeze in general python:

appdirs==1.4.3 lxml==4.0.0 packaging==16.8 pyparsing==2.2.0 six==1.10.0 virtualenv==15.1.0

Upvotes: 0

Views: 2066

Answers (2)

Razia Khan
Razia Khan

Reputation: 478

This is the simple way to start new django project:-
In terminal do following steps

$ mkvirtualenv <env_name>
$ workon <env_name>
$ pip install django
$ django-admin startproject <project_name>
$ cd <project_name>
$ ./manage.py migrate
$ ./manage.py runserver
open this in browser http://127.0.0.1:8000/
open the project in pychram and then do this 
click on file -> settings -> projects interpreter -> add local -> select <env_name> -> click bin -> click python3 or python2 -> apply -> OK

Upvotes: 0

Waket Zheng
Waket Zheng

Reputation: 6371

How about run python3 manage.py instead of ./manage.py?

And I suggest use pipenv instead of virtualenv+pip.

Upvotes: 1

Related Questions