My application was running fine when I was running it on my local machine. But, as soon as I uploaded it on a server , manage.py is giving the following error -
File "manage.py", line 14
) from exc
^
SyntaxError: invalid syntax
This is the code for manage.py -
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "chatbot.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 ***//Here is the error***
execute_from_command_line(sys.argv)
I am using Python 2.7 and Django 1.11
kindly refer this link: How to solve SyntaxError on autogenerated manage.py?
What I have learned from the above link is that if we have two versions of python, then we need to specify the version in which we need to run our application like below:
python3 manage.py runserver
Related
I'm running Django and I can't figure out why it's coming out like that.
It turns out that Python and Django are installed.
input : python manage.py runserver
output : Python
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cmapsite.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 __name__ == '__main__':
main()
Did you activate your Virtual Environment in CMD terminal ?
If No then First Activate your Virtual Environment, It may be a reason of not starting Server.
If you already created the virtual environment then which is the error that you keep getting ? Upload the Error.
I accidentally deleted the manage.py file with vim nerdtree. apparently, there isn't a way to recover it I I don't know what to do.
can I somehow create a new one or restore it?
You can easily copy this file by changing the "start.settings" with your "application.settings". The other codes is always the same :)
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'starter.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 __name__ == '__main__':
main()
You can create a new Django project by using the following command:
django-admin startproject someappname
and copy the file into your project and ensure to change the app name on the manage.py file
I have created a django project called tictoctoe
but when I run "py manage.py runserver" it gives me following error.
File "manage.py", line 11
except ImportError as exc:
^
SyntaxError: invalid syntax
it used to work fine earlier.
here is
manage.py
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tictactoe.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 __name__ == '__main__':
main()
Please make sure your virtual environment is activated where you have Django installed.
I could figure out what was wrong,
I have created project directory inside the virtual environment, hence without activating the virtual environment it gives that error.
after I activating venv, py manage.py runserver works fine.
How to activate virtual environment:
my project location:
c:\Users\xyz\Documents\django-projects
my virtual environment location is
c:\Users\xyz\Documents\django-projects\django-env
go to
c:\Users\310227436\Documents\django-projects\django-env\Scripts
invoke activate:
c:\Users\310227436\Documents\django-projects\django-env\Scripts> Activate
after doing this problem should be resolved.
I am running the following steps in Mac Terminal:
sudo easy_install pip
sudo pip install virtualenv
virtualenv NameOfFolder
cd NameOfFolder
source bin/activate
sudo pip install django
django-admin startproject NameOfFolderSub1
cd NameOfFolderSub1
python manage.py runserver
At this last steps, it communicates this msg occurred:
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 14, in <module>
import django
ModuleNotFoundError: No module named 'django'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 17, in <module>
"Couldn't import Django. Are you sure it's installed and "
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?
Attempted to check the Django version using this command in Terminal:
python -m django --version
it confirmed that django is not there with the following msg:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3: No module named django
What did I do wrong int he step-by-step installation above ?
Appreciate the help.
The code in manage.py is as follows:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xxx.settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
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?"
)
raise
execute_from_command_line(sys.argv)
You've done sudo pip install django. Don't do that. You're in a virtualenv; just install directly without sudo; and use pip3 instead of pip.
While I am setting up a supeuser in my django project using the command
$ python manage.py syncdb
File "manage.py", line 14
) from exc
^
SyntaxError: invalid syntax
manage.py is a system generated file still it shows a syntax error.
Here is the manage.py file! -
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE",
"my_django15_project.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)
Please tell some solution!
The from part of raise is not supported in Python 2.x. Use Python 3.x instead.