20

I am developing a web in Ubuntu using django. Everything works normal. Now, I want to change my computer which use Windows. When I try to runserver, it gives:

E:\DEGNet>py manage.py runserver
  File "manage.py", line 14
    ) from exc
         ^
SyntaxError: invalid syntax

E:\DEGNet>py
Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)]
 on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

As shown above, I have installed Python 3.6.3. I have installed django and other necessary library using pip3 too.

Edit: manage.py file, it is a default manage.py that I get when generating the project.

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

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DEGNet.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)
6
  • Could you also provide manage.py? Commented Dec 19, 2017 at 5:56
  • I have updated the question with manage.py file content Commented Dec 19, 2017 at 6:01
  • Removing the words from exc from your manage.py file will resolve your issue I think. Try it. Commented Dec 19, 2017 at 6:11
  • why did you change generated manage.py? Commented Dec 19, 2017 at 6:15
  • 1
    I got this error multiple times and the reason behind this error was my environment was not active, this may be a reason for you too. Commented Jun 9, 2019 at 13:33

17 Answers 17

37

I faced same problem but now solved with this cmd:

python3 manage.py runserver
Sign up to request clarification or add additional context in comments.

2 Comments

This is correct solution. If you have both python 2 and python 3 by default python in /usr/bin points to python 2. django would have be installed for python 3 and hence the error. python3 manage.py runserver works with from exc. Thanks for the tip.
that's a valid solution only if you have both python2 and python3 installed, but the error might happen even if you have just python 2 distribution
20

Edit your manage.py file as given below:

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

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DEGNet.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?"
        )
    execute_from_command_line(sys.argv)

Note that from exc is removed from the file. It is not required in the manage.py file.

1 Comment

This might stop the 'invalid syntax' error but it doesn't solve the real issue: you installed Django for Python 3 and now getting an error when you try to run it with Python 2. The real fix is to use the correct Python version.
7
  1. Make sure that your virtualenv is activated. Suppose the name of your virtualenv is pythonpy, then run these commands:

    virtualenv pythonpy workon pythonpy #After running these command, you should see something like this but your file path may be different: "(pythonpy) C:\Users\ MyDjangoProject \

  2. Then go to the project folder which contains manage.py (pythonpy) C:\Users\ MyDjangoProject \ #Same path as above
  3. Then simple run the server:

    python manage.py runserver #This will give you the project path to the localhost. Copy and paste the URL in the browser and should work.

Comments

3

Try (from command line):

python3 manage.py runserver

If I used this (no python3) instead:

python manage.py runserver

the error persisted. This method allows you to not have to alter manage.py (so you can keep "from exc").

Comments

3

You just forgot to activate the virtual environment , do it like that :

source /home/adel/Dev/Python/DjangoProjects/myproject/venv/bin/activate

And than you can run the server :

python manage.py runserver

Comments

2

i've also got this error but solve it by installing pipenv first,

try run this first

pipenv shell django==2.1

then you should be able to run

python3 manage.py runserver

Comments

2

Ensure you're running the app from virtualenv i.e if you've created a virtualenv for your project, then activate the venv first.

me@debian:~/Desktop/webapp$source venv/bin/activate 
(venv) me@debian:~/Desktop/webapp$python manage.py runserver

Comments

1

What's happening is that the wrong version of python is being used, which may not have all the dependencies in your virtualenv. I get this error when using sudo manage.py: using sudo changes the version of python which is being used to /usr/bin/python.

The problem is solved by specifying which version of python to use when using sudo:

sudo /path/to/my/env/bin/python manage.py makemigrations

Comments

1

I have met the same problem, and I find it strange because I have activated the virtualenvironment and set the python3, however, I meet this problem when I use this statement "python manage.py runserver".I use this statement often but I meet this only once, and I restart my virtualenvironment and it runs, hope you wil,too.

Comments

1

I was facing the same issue.

Activated venv & ran python manage.py runserver wasn't working.

I could see the venv was activated but still it wasn't working. Then tried python3 manage.py runserver which got rid of exc issue but now it wasnt detecting my installed libraries.

The change I made that broke it was renaming the base folder ( kind of same situation like OP base folder location was different now).

But wait, how does this impact anything?

A variable that stores a full path to venv inside venv/bin/activate is now no longer valid.

Resolution:

  1. deactivate venv if already running
  2. open venv/bin/activate
  3. search for VIRTUAL_ENV variable and rename as per the new folder changes made.
  4. activate venv source venv/bin/activate
  5. run python manage.py runserver

Tada, everything is back to normal and we can happily enjoy our lemonade.

Comments

0

I've got also the same issue with Python 3.4.4 and Django 2.0. I tried the last solution, nothing works (no need to delete that: from exc on the line 14).

Just run your server with:

python manage.py runserver

Instead of:

./manage.py runserver #or '.\manage.py runserver' for Windows

Comments

0

I have no problem running this way:

sudo ./**(your path)**/bin/python manage.py runserver

Comments

0

i had the same problem. I solved it by simply specifying the python version i.e type python3 manage.py runserver instead of python manage.py runserver

Comments

0

i re installed the v env

virtualenv venv --python=python3.7

i insttalled django

it worked

2 Comments

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
@GeorgeZ. An attempt to answer the question (especially one containing a solution that worked for at least one person) should not be deleted from review
0

I had come out of my virtial environment.

I re-ran pipenv shell

Comments

0

Do this first on your command line where your Env folder is found:

source Env/bin/activate

Then now navigate to your project directory and do:

python manage.py runserver

It works!!

Comments

0

Just use python3 manage.py runserver instead python manage.py runserver

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.