How to solve SyntaxError on autogenerated manage.py?

拜拜、爱过 提交于 2019-11-27 04:27:26

Make sure which python version you connect the django with.

When you install django using just pip install django, then you have to run python manage.py startapp else if you used pip3Click here to view terminal example install django, then you have to run python3 manage.py startapp

Fernando

You can try with python3 manage.py runserver. It works for me.

you should activate your virtual environment . In terminal -> source env/bin/activate now there will be ----> (env) in your terminal displayed !

now it will work -> runserver .

No need to delete exc part !

I was experiencing the same but this was solved by running with specific python 3.6 as below:

python3.6 manage.py runserver

Its a simple solution actually one i just ran into. Did you activate your virtual environment?

my terminal screenshot

After testing with precise instructions (using python2 or python3 instead of just "python") I've constated that no matter what the tutorial says, this works ONLY with python3.

It's best to create a virtual environment and run your Django code inside this virtual environment, this helps in not changing your existing environments. Here are the basic steps to start with the virtual environment and Django.

  1. Create a new Directory and cd into it.

    mkdir test , cd test

  2. Install and Create a Virtual environment.

    python3 -m pip install virtualenv virtualenv venv -p python3

  3. Activate Virtual Environment: source venv/bin/activate

  4. Install Django: pip install django

  5. Start a new project: django-admin startproject myproject

  6. cd to your project and Run Project:

    cd myproject , python manage.py runserver

  7. You can see your project here : http://127.0.0.1:8000/

Just delete that from exec part to now have something like

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.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)

You must activate virtual environment where you have installed django. Then run this command - python manage.py runserver

Also, the tutorial recommends that a virtual environment is used (see Django documentation: https://docs.djangoproject.com/en/2.0/topics/install/#installing-official-release"). You can do this with pipenv --three. Once you've installed django with pipenv install django and activated your virtual environment with pipenv shell, python will refer to python3 when executing python manage.py runserver.

Pipenv documentation: https://docs.pipenv.org/

The django-admin maybe the wrong file.I met the same problem which I did not found on a different computer the same set-up flow.

After comparing two project, I found several difference at manage.py and settings.py, then I realized I created 2.0 django project but run it with python2.

runwhich django-adminin iterm

/Library/Frameworks/Python.framework/Versions/3.6/bin/django-admin

It looks like I got a django-admin in python3 which I didn't know why.So I tried to get the correct django-amin.

pip show django

then I got

Name: Django
Version: 1.11a1
Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Home-page: https://www.djangoproject.com/
Author: Django Software Foundation
Author-email: foundation@djangoproject.com
License: BSD
Location: /Library/Python/2.7/site-packages
Requires: pytz

In/Library/Python/2.7/site-packages, I found the django-admin

/Library/Python/2.7/site-packages/django/bin/django-admin.py

So I created project again by

/Library/Python/2.7/site-packages/django/bin/django-admin.py startproject myproject

then run

cd myproject
python manage.py runserver

succeeded🎉

We have to create a virtual environment inside the project, not outside the project.. Then it will solve..

It seems you have more than one version of Python on your computer. Try and remove one and leave the only version you used to develop your application.

If need be, you can upgrade your version, but ensure you have only one version of Python on your computer.

I hope this helps.

What am I wondering is though the django is installed to the container it may not be in the host machine where you are running the command. Then how will the command run. So since no above solutions worked for me.

I found out the running container and get into the running container using docker exec -it <container> bash then ran the command inside docker container. As we have the volumed container the changes done will also reflect locally. What ever command is to be run can be run inside the running container

For future readers, I too had the same issue. Turns out installing Python directly from website as well as having another version from Anaconda caused this issue. I had to uninstall Python2.7 and only keep anaconda as the sole distribution.

tomilia

Have you entered the virtual environment for django? Run python -m venv myvenv if you have not yet installed.

Just do:

pipenv shell

then repeat:

python manage.py runserver

and don't delete from exc as suggested above.

cheers!

I had same problem and could solve it. It is related to the version of Django you've installed, some of them are not supported by python 2.7. If you have installed Django with pip, it means that you are installing the latest version of that which probably is not supported in python 2.7, You can get more information about it here. I would suggest to python 3 or specify the version of Django during installing (which is 1.11 for python 2.7).

SAMARAT ADITYA

just you describe your python version

=> python3.5 manage.py runserver

(my python version=3.5)

I solved this problem to uninstall the multiple version of Python. Check Django Official Documentation for Python compatibility.

"Python compatibility

Django 2.1 supports Python 3.5, 3.6, and 3.7. Django 2.0 is the last version to support Python 3.4."

manage.py file

#!/usr/bin/env python
import os
import sys

if __name__ == '__main__':
   os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'work.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 removing "from exc" from second last line of this code will generate another error due to multiple versions of Python.

activate env by the Following Command

  source  pathetoYourEnv/bin/activate

then run command

python manage.py runserver

I solved same situation.

INSTALLED VERSION

python 3.6, django 2.1

SITUATION

I installed Node.js in Windows 10. After python manage.py runserver caused error.

ERROR

File "manage.py", line 14
) from exc
^
SyntaxError: invalid syntax

REASON

My python path changed to python-2.7 from python-3.6. (3.6 is correct in my PC.)

SOLUTION

Fix python path.

You should start your Virtual Environment,

how to do it?

first with terminal cd into the directory containing manage.py

then type $source <myvenv>/bin/activate replace with you Virtual Environment name, without angular brackets.

Another issue can that your root directory and venv mis-match. The structure should be something like this:

|-website
     ..facebook
     ..manage.py
     ..myvenv
     ..some other files

That is your virtual environment and manage.py should be in the same folder. Solution to that is to restart the project. If you are facing this error you must haven't coded anything yet, so restart.

I landed on the same exact exception because I forgot to activate the virtual environment.

The following could be the possible reasons,

1. The virtual environment is not enabled
2. The virtual environment is enabled but the python version is different

To create virtual environment

$ virtualenv --python=python3 venv

To activate the virtual environment

$ source venv/bin/activate

I had the exact same error, but then I later found out that I forget to activate the conda environment which had django and other required packages installed.

Solution: Create a conda or virtual environment with django installed, and activate it before you use the command: $ python manage.py migrate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!