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.
Related
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.
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
My project directory structure looks like following:
My wsgi file:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
application = get_wsgi_application()
and my manage.py file:
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.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)
in my settings .py I have:
WSGI_APPLICATION = 'config.wsgi.application'
I added my project path to python with following:
export PYTHONPATH=/home/ec2-user/amm_inovision_backend:$PYTHONPATH
and I am trying to run gunicorn command where my manage.py file is as: gunicorn amm_inovision_backend.config.wsgi:application
But it throws me error no module named amm_inovision_backend.config.wsgi
If I run instead gunicorn config.wsgi:application it throws no module named amm_inovision_backend.config.settings
What am I doing wrong?
Note: in the screenshot it is amm_ino_backend but actually it is amm_inovision_backend in the production
when you want to use gunicorn you must run gunicorn installed on your virtual environment.
so you don't need to export any python path, you only need to find path to gunicorn on your virtual-env where Django is installed, or you can add path to virtual environment not to project files.
also you need first to edit your wsgi.py
you have to replace this
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") with
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
your command must be something like that:
/path/to/.virtual-envs/your-env/bin/gunicorn config.wsgi:application
Use:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
application = get_wsgi_application()
note the "CONFIG.SETTINGS"
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.