Bharata
Bharata

Reputation: 737

Django manage.py runserver invalid syntax

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)

Upvotes: 20

Views: 67059

Answers (17)

Roytman Piccoli
Roytman Piccoli

Reputation: 1582

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

Upvotes: 0

Aklesh Sakunia
Aklesh Sakunia

Reputation: 339

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.

Upvotes: 1

Benndip
Benndip

Reputation: 51

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!!

Upvotes: 0

Matt Doran
Matt Doran

Reputation: 2153

I had come out of my virtial environment.

I re-ran pipenv shell

Upvotes: 0

Fahd Mannaa
Fahd Mannaa

Reputation: 295

i re installed the v env

virtualenv venv --python=python3.7

i insttalled django

it worked

Upvotes: 0

吴泽森
吴泽森

Reputation: 11

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.

Upvotes: 1

autopilot
autopilot

Reputation: 1

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

Upvotes: 0

suria sarath
suria sarath

Reputation: 481

I faced same problem but now solved with this cmd:

python3 manage.py runserver

Upvotes: 37

ADEL NAMANI
ADEL NAMANI

Reputation: 331

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

Upvotes: 3

Salad.Guyo
Salad.Guyo

Reputation: 3395

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

Upvotes: 2

Hendra Sirait
Hendra Sirait

Reputation: 57

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

Upvotes: 2

HAL 9000
HAL 9000

Reputation: 179

  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.

Upvotes: 7

user9521248
user9521248

Reputation: 63

I have no problem running this way:

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

Upvotes: 0

Mountain Scott
Mountain Scott

Reputation: 283

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").

Upvotes: 3

Micah Walter
Micah Walter

Reputation: 958

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

Upvotes: 1

Serge Kishiko
Serge Kishiko

Reputation: 486

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

Upvotes: 0

arulmr
arulmr

Reputation: 8836

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.

Upvotes: 20

Related Questions