cat manage.py
#!/usr/bin/env python3import os
import sys
if __name__ =="__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE","mysite.settings")try:from django.core.management import execute_from_command_line
exceptImportErroras exc:raiseImportError("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)
I’ve created a “mysite” dummy project (my very first one) and try to test it without altering it.
django-admin startproject mysite
cd mysite
python manage.py runserver
File "manage.py", line 14
) from exc
^
SyntaxError: invalid syntax
I’m getting a SyntaxError on a file that was generated by the system itself. And I seem unable to find anyone else who has gone through the same issue.
I’ll add some data of my setup in case it may be of use
Update: adding contents of autogenerated manage.py
cat manage.py
#!/usr/bin/env python3
import os
import sys
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?"
) from exc
execute_from_command_line(sys.argv)
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.
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.
The solution is straightforward. the exception from manage.py
is because when running the command with python, Django is unable
to predict the exact python version,
say you may have 3.6, 3.5, 3.8 and maybe just one of this versions pip module was used to install Django
to resolve this either use:
./manage.py `enter code here`<command>
or using the exact python version(x.x) stands:
pythonx.x manage.py <command>
else the use of virtual environments can come in handy
because its relates any pip django module easily to python version
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
Name:DjangoVersion:1.11a1Summary: A high-level PythonWeb framework that encourages rapid development and clean, pragmatic design.Home-page: https://www.djangoproject.com/Author:DjangoSoftwareFoundationAuthor-email: foundation@djangoproject.com
License: BSD
Location:/Library/Python/2.7/site-packages
Requires: pytz
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.
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.
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.
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.
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).
#!/usr/bin/env pythonimport os
import sys
if __name__ =='__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE','work.settings')try:from django.core.management import execute_from_command_line
exceptImportErroras exc:raiseImportError("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)
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.
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.