Fix Python – How to solve SyntaxError on autogenerated


Asked By – Frank

I’m following the Django tutorial

I’ve created a “mysite” dummy project (my very first one) and try to test it without altering it.

django-admin startproject mysite
cd mysite
python runserver

File "", line 14
) from exc
SyntaxError: invalid syntax

I’m getting a SyntaxError on a file that was generated by the system itself. And I seem unable to find anyone else who has gone through the same issue.

I’ll add some data of my setup in case it may be of use

$ vpython --version
Python 2.7.12
$ pip --version
pip 9.0.1 from /home/frank/.local/lib/python2.7/site-packages (python 2.7)
$ python -m django --version

Adding contents of autogenerated

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

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
        from 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

Now we will see solution for issue: How to solve SyntaxError on autogenerated


Make sure which python version you connect the django with (Make sure to activate the virtual env if you are using any).

When you install django using just

pip install django 

then you have to run

python startapp <yourApp name>

else if you have used:

pip3 install django

then you have to run

python3 startapp <yourapp name>

enter image description here

This question is answered By – lobsang

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0