183

I'm following the Django tutorial https://docs.djangoproject.com/es/1.10/intro/tutorial01/

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 manage.py runserver

File "manage.py", 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
1.10.6

Adding contents of autogenerated manage.py

cat manage.py 
#!/usr/bin/env python3
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.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)
5
  • Please post the contents of the whole file (or at least more of it) that has the syntax error.
    – Scovetta
    Commented Mar 5, 2017 at 17:30
  • 17
    That file is intended for use with Python 3 (notice it's first line!), and will not work in 2.7. Commented Mar 5, 2017 at 17:44
  • The tutorial web says that, when some part of the code won't work with 2.7, they'll add the modifications needed in comments. This had no modifications listed, so I assumed it would work...
    – Frank
    Commented Mar 5, 2017 at 17:48
  • (mac os) use: python3 manage.py runserver Commented Oct 29, 2020 at 17:13
  • i run this command and it works for me: .\venv\scripts\activate Commented Apr 16, 2021 at 6:43

40 Answers 40

172

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 manage.py startapp <yourApp name>

else if you have used:

pip3 install django

then you have to run

python3 manage.py startapp <yourapp name>

Refer:
enter image description here

2
  • 20
    Had the same problem with python manage.py migrate and changing it to python3 manage.py migrate fixed the error. Commented Dec 19, 2018 at 13:54
  • 13
    I forgot to activate my virtual env.
    – Bobort
    Commented Jan 1, 2019 at 11:51
78

You can try with python3 manage.py runserver. It works for me.

3
  • i have python3.exe and add its path to my localhost but after restart cmd it has no effect no error no response . show me a new command line. :o Commented Mar 16, 2021 at 9:32
  • *when there is two python version installed "py -3 manage.py runserver" py command comes with Python3.x and allow to choose among multiple Python interpreters. For example if you have both Python 3.4 and 2.7 installed, py -2 will start python2.7 and py -3 will start python3.4 . If you just use py it will start the one that was defined as default. Commented May 6, 2022 at 3:03
  • It literally worked for me. Thanks saviour !
    – ishaj
    Commented May 19, 2023 at 6:19
37

You should activate your virtual environment. In terminal, source env/bin/activate. Depending on your shell, something like (env) should now be a part of the prompt.

And now runserver should work. No need to delete exc part!

1
  • 1
    For Django 3 the activate script in the Scripts folder, so the command should be: source venv/Scripts/activate Commented Apr 24, 2021 at 15:03
27

Just activate your virtual environment.

1
17

For running Python version 3, you need to use python3 instead of python.

The final command will be:

python3 manage.py runserver
16

I was experiencing the same but this was solved by running with specific python 3.6 as below:

python3.6 manage.py runserver
15

Its a simple solution actually one i just ran into. Did you activate your virtual environment?

my terminal screenshot

14

It's best to create a virtual environment and run your Django code inside this virtual environment, this helps in not changing your existing environments. Here are the basic steps to start with the virtual environment and Django.

  1. Create a new Directory and cd into it.

    mkdir test , cd test

  2. Install and Create a Virtual environment.

python3 -m pip install virtualenv virtualenv venv -p python3

  1. Activate Virtual Environment: source venv/bin/activate

  2. Install Django: pip install django

  3. Start a new project: django-admin startproject myproject

  4. cd to your project and Run Project:

cd myproject,

python manage.py runserver
  1. You can see your project here: http://127.0.0.1:8000/
1
  • 1
    well done! by this "tutorial" it was clear how it works. in django3 the activate is located in the .\venv\Script folder Commented Feb 29, 2020 at 8:26
12

After testing with precise instructions (using python2 or python3 instead of just "python") I've constated that no matter what the tutorial says, this works ONLY with python3.

4

The solution is straightforward. the exception from manage.py is because when running the command with python, Django is unable to predict the exact python version, say you may have 3.6, 3.5, 3.8 and maybe just one of this versions pip module was used to install Django to resolve this either use:

./manage.py `enter code here`<command>

or using the exact python version(x.x) stands:

pythonx.x manage.py <command>

else the use of virtual environments can come in handy because its relates any pip django module easily to python version

  • create env with pyenv or virtualenv
  • activate (e.g in virtualenv => virtualenv env)
  • run using python manage.py command
3

I solved same situation.

INSTALLED VERSION

python 3.6, django 2.1

SITUATION

I installed Node.js in Windows 10. After python manage.py runserver caused error.

ERROR

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

REASON

My python path changed to python-2.7 from python-3.6. (3.6 is correct in my PC.)

SOLUTION

Fix python path.

3

The following could be the possible reasons,

1. The virtual environment is not enabled
2. The virtual environment is enabled but the python version is different

To create virtual environment

$ virtualenv --python=python3 venv

To activate the virtual environment

$ source venv/bin/activate
1
  • 2. Different python version is the issue most of the time. +1 for this answer.
    – PSN
    Commented Sep 1, 2020 at 16:14
2

You must activate virtual environment where you have installed django. Then run this command - python manage.py runserver

2

Also, the tutorial recommends that a virtual environment is used (see Django documentation: https://docs.djangoproject.com/en/2.0/topics/install/#installing-official-release"). You can do this with pipenv --three. Once you've installed django with pipenv install django and activated your virtual environment with pipenv shell, python will refer to python3 when executing python manage.py runserver.

Pipenv documentation: https://pipenv.kennethreitz.org/

2

Activate your virtual environment then try collecting static files, that should work.

$ source venv/bin/activate
$ python manage.py collectstatic
2

You should start your Virtual Environment,

How to do it?

First with terminal cd into the directory containing manage.py

Then type $source <myvenv>/bin/activate replace with you Virtual Environment name, without angular brackets.

Another issue can that your root directory and venv mis-match. The structure should be something like this:

|-website
     ..facebook
     ..manage.py
     ..myvenv
     ..some other files

That is your virtual environment and manage.py should be in the same folder. Solution to that is to restart the project. If you are facing this error you must haven't coded anything yet, so restart.

2

I had the exact same error, but then I later found out that I forget to activate the conda environment which had django and other required packages installed.

Solution: Create a conda or virtual environment with django installed, and activate it before you use the command:

$ python manage.py migrate 
1

The django-admin maybe the wrong file.I met the same problem which I did not found on a different computer the same set-up flow.

After comparing two project, I found several difference at manage.py and settings.py, then I realized I created 2.0 django project but run it with python2.

runwhich django-adminin iterm

/Library/Frameworks/Python.framework/Versions/3.6/bin/django-admin

It looks like I got a django-admin in python3 which I didn't know why.So I tried to get the correct django-amin.

pip show django

then I got

Name: Django
Version: 1.11a1
Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Home-page: https://www.djangoproject.com/
Author: Django Software Foundation
Author-email: [email protected]
License: BSD
Location: /Library/Python/2.7/site-packages
Requires: pytz

In/Library/Python/2.7/site-packages, I found the django-admin

/Library/Python/2.7/site-packages/django/bin/django-admin.py

So I created project again by

/Library/Python/2.7/site-packages/django/bin/django-admin.py startproject myproject

then run

cd myproject
python manage.py runserver

succeeded🎉

1

We have to create a virtual environment inside the project, not outside the project.. Then it will solve..

1

I landed on the same exact exception because I forgot to activate the virtual environment.

1

I was also getting the same error. enter image description here

Then I went back to the folder where the environment folder is there and I forgot to activate a Virtual environment so only I was getting this error. enter image description here

Go to that folder and activate the virtual environment.

$ source env/bin/activate
1

I had this issue (Mac) and followed the instructions on the below page to install and activate the virtual environment

https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/

$ cd [ top-level-django-project-dir ]

$ python3 -m pip install --user virtualenv

$ python3 -m venv env

$ source env/bin/activate

Once I had installed and activated the virtual env I checked it

$ which python

Then I installed django into the virtual env

$ pip install django

And then I could run my app

$ python3 manage.py runserver

When I got to the next part of the tutorial

$ python manage.py startapp polls

I encountered another error:

     File "manage.py", line 16

   ) from exc
            ^

   SyntaxError: invalid syntax

I removed

from exc

and it then created the polls directory

1

Same issue occurred to me,But what I did was,

Just Replaced:

python manage.py runserver

with

python3 manage.py runserver

in the terminal(macOsX). Because I am using Python version 3.x

1

I encountered the same error when using pipenv. The issue was caused by not accessing Django correctly from within the virtual environment.

The correct steps using pipenv:

  1. Activate virtual environment: pipenv shell
  2. Install Django: pipenv install django
  3. Create a project: django-admin startproject myproject
  4. Navigate into project folder: cd myproject
  5. Start Django with pipenv: pipenv run python manage.py runserver

Note: Pipenv will use the correct python version and pip within the virtual environment.

1

It seems you have more than one version of Python on your computer. Try and remove one and leave the only version you used to develop your application.

If need be, you can upgrade your version, but ensure you have only one version of Python on your computer.

0

What am I wondering is though the django is installed to the container it may not be in the host machine where you are running the command. Then how will the command run. So since no above solutions worked for me.

I found out the running container and get into the running container using docker exec -it <container> bash then ran the command inside docker container. As we have the volumed container the changes done will also reflect locally. What ever command is to be run can be run inside the running container

0

For future readers, I too had the same issue. Turns out installing Python directly from website as well as having another version from Anaconda caused this issue. I had to uninstall Python2.7 and only keep anaconda as the sole distribution.

0

Have you entered the virtual environment for django? Run python -m venv myvenv if you have not yet installed.

0

I had same problem and could solve it. It is related to the version of Django you've installed, some of them are not supported by python 2.7. If you have installed Django with pip, it means that you are installing the latest version of that which probably is not supported in python 2.7, You can get more information about it here. I would suggest to python 3 or specify the version of Django during installing (which is 1.11 for python 2.7).

0

I solved this problem to uninstall the multiple version of Python. Check Django Official Documentation for Python compatibility.

"Python compatibility

Django 2.1 supports Python 3.5, 3.6, and 3.7. Django 2.0 is the last version to support Python 3.4."

manage.py file

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

if __name__ == '__main__':
   os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'work.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)

If removing "from exc" from second last line of this code will generate another error due to multiple versions of Python.

Not the answer you're looking for? Browse other questions tagged or ask your own question.