Django manage.py runserver invalid syntax - python

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)

I faced same problem but now solved with this cmd:
python3 manage.py runserver

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.

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.

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").

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

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

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

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

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.

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 venv inside venv/bin/activate is
now no longer valid.
Resolution:
deactivate venv if already running
open venv/bin/activate
search for VIRTUAL_ENV variable 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.

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

I have no problem running this way:
sudo ./**(your path)**/bin/python manage.py runserver

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

i re installed the v env
virtualenv venv --python=python3.7
i insttalled django
it worked

I had come out of my virtial environment.
I re-ran pipenv shell

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!!

Just use python3 manage.py runserver instead python manage.py runserver

Related

How do I run an existing Django file?

I downloaded a folder with multiple python files in the form of a Django project for a course and I am unable to run it. Usually, when you create a Django project it is created within the terminal with the line django-admin startproject web_project . and then python manage.py migrate is used and then the line python manage.py runserver to start a server.
How do I start a server to run my project?
When I use django-admin startproject web_project ., it creates a new project which I don't want. When I use python manage.py migrate or python manage.py runserver it says:
Command 'python' not found, did you mean:
command 'python3' from deb python3
command 'python' from deb python-is-python3
But even when I used python3 manage.py migrate or python3 manage.py runserver instead and it returned:
python3: can't open file 'manage.py': [Errno 2] No such file or directory
At first make sure you opened command prompt or terminal in right directory(in projects directory) when you are running the runserver command.
If you are on windows open the project folder(the folder that contains files like manage.py), then right click and press open PowerShell window here.
first of all I recommend creating a virtual environment using the following command
python3 -m venv .env
then activate it
source .env/bin/activate
"django-admin statproject" its for initalite a new project for running an excited Django app you should just install Django - if there is now "requirements.txt" file- , migrate and run.
pip install Django
python manage.py migrate
python manage.py runserver

When I gave command on CMD manage.py runserver it instead took me into VS Code and when i ran it in VS code error was on (from django.core.management)

When I ran the command on CMD manage.py runserver it intern redirected to VS Code. When I tried to run on VS Code it showed the system cannot find the specified path. It was underlining this statement:
try:
from django.core.management
^^^^^^^^^^^^^^^^^^^^^^
first you need to add the python path to your variable environnement
Second
navigate to your django project and use terminal with this command :
python manage.py runserver
use Docs

Django loops infinitly after startapp method call

I'm doing the CS50Web course, and there is a Django project in which I want to create a new app called "wikipedia". The problem is that after I run this command in Windows PowerShell:
python manage.py runserver
this loop happens:
loop print
I don't know what I did wrong for that to happen. After running
python manage.py startapp wikipedia
, I:
went to my Django project and added 'wikipedia' in the installed apps in settings.py
went to urls.py and added a path like this:
path("wiki/", include("wikipedia.urls))
And then I tried running the server. What did I do wrong?
You must have something improperly configured.
Run this:
$ pip uninstall django
$ pip install django
$ django-admin startproject <projectname>
$ cd <projectname>
$ django-admin startapp wikipedia
$ python manage.py makemigrations
$ python manage.py migrate
do what you described in your question
Also, it is always a better idea to run these things in the CMD, rather than in the PowerShell
Are you sure you have url mappings in your wikipedia app urls.py? Not having any patterns there could be the cause of an error.

Virtual environment is not working in Django

I am deploying my Django app on digitalocean but I get an error after the giving the command in my terminal to migrate. Help me to figure out the error so that I can fix.
terminal output
urban#ubuntu-s-1vcpu-1gb-blr1-01:~$ source bin/activate
(urban) urban#ubuntu-s-1vcpu-1gb-blr1-01:~$ cd lok
(urban) urban#ubuntu-s-1vcpu-1gb-blr1-01:~/lok$ python manage.py migrate
File "manage.py", line 14
) from exc
^
SyntaxError: invalid syntax
(urban) urban#ubuntu-s-1vcpu-1gb-blr1-01:~/lok$ ls
business List manage.py news sports
templates
db.sqlite3 lokswar movie README.md static_my_project
Your version of Django requires Python 3, but it looks like python is python2, even after activating the virtual environment. You can use which python to check that python is pointing to the executable in your virtual environment.
If your virtual environment was created with python 2, then you'll have to create a new virtual environment that uses python 3.
deactivate
python3 -m venv /path/to/urban.new
source /path/to/urban.new/bin/activate
# install requirements

Django manage.py question

Why is it that I have to run python manage.py somecommand and others simply run manage.py somecommand? I'm on OSX 10.6. Is this because there is a pre-set way to enable .py files to automatically run as Python scripts, and I've somehow disabled the functionality, or is that something that you explicitly enable?
If you are using a recent version of Django, the manage.py file should be an "executable" file by default.
Please note, you cannot just type manage.py somecommand into the terminal as manage.py is not on the PATH, you will have to type ./ before it to run it from the current directory, i.e. ./manage.py somecommand.
If that does not work please be sure that the manage.py file has:
#!/usr/bin/env python
as its first line. And make sure it is executable: chmod +x manage.py
There are two things you should look at:
First, is the manage.py script set to be executable? If not, try
chmod u+x manage.py
Secondly, does manage.py have a valid hashbang line? If not, you could try adding one that points to the correct python interpreter for your system.
On the mac the manage.py command has to be executable to just run it without the python command. You can do this with:
chmod 755 manage.py
If you are in the same directory as manage.py, to run it you type:
./manage.py somecommand
Otherwise you want to specify the path:
/path/to/my/project/manage.py somecommand

Categories

Resources