[Django]-Django manage.py runserver invalid syntax

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.

37๐Ÿ‘

I faced same problem but now solved with this cmd:

python3 manage.py runserver

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.

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โ€).

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

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

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

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

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.

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.

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

0๐Ÿ‘

I have no problem running this way๏ผš

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

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

0๐Ÿ‘

i re installed the v env

virtualenv venv --python=python3.7

i insttalled django

it worked

0๐Ÿ‘

I had come out of my virtial environment.

I re-ran pipenv shell

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

0๐Ÿ‘

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

Leave a comment