'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)
Solution 1:[1]
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.
Solution 2:[2]
I faced same problem but now solved with this cmd:
python3 manage.py runserver
Solution 3:[3]
- 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 \
- Then go to the project folder which contains manage.py (pythonpy) C:\Users\ MyDjangoProject \ #Same path as above
- 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.
Solution 4:[4]
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
Solution 5:[5]
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").
Solution 6:[6]
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
Solution 7:[7]
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
Solution 8:[8]
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
Solution 9:[9]
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.
Solution 10:[10]
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
Solution 11:[11]
I have no problem running this way?
sudo ./**(your path)**/bin/python manage.py runserver
Solution 12:[12]
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
Solution 13:[13]
i re installed the v env
virtualenv venv --python=python3.7
i insttalled django
it worked
Solution 14:[14]
I had come out of my virtial environment.
I re-ran pipenv shell
Solution 15:[15]
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!!
Solution 16:[16]
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
venvinsidevenv/bin/activateis now no longer valid.
Resolution:
- deactivate venv if already running
- open venv/bin/activate
- search for
VIRTUAL_ENVvariable and rename as per the new folder changes made. - activate venv
source venv/bin/activate - run
python manage.py runserver
Tada, everything is back to normal and we can happily enjoy our lemonade.
Solution 17:[17]
Just use python3 manage.py runserver instead python manage.py runserver
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
