Reputation: 737
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)
Upvotes: 20
Views: 67059
Reputation: 1582
Just use python3 manage.py runserver
instead python manage.py runserver
Upvotes: 0
Reputation: 339
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
insidevenv/bin/activate
is now no longer valid.
Resolution:
VIRTUAL_ENV
variable and rename as per the new folder changes made.source venv/bin/activate
python manage.py runserver
Tada, everything is back to normal and we can happily enjoy our lemonade.
Upvotes: 1
Reputation: 51
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!!
Upvotes: 0
Reputation: 2153
I had come out of my virtial environment.
I re-ran pipenv shell
Upvotes: 0
Reputation: 295
i re installed the v env
virtualenv venv --python=python3.7
i insttalled django
it worked
Upvotes: 0
Reputation: 11
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.
Upvotes: 1
Reputation: 1
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
Upvotes: 0
Reputation: 481
I faced same problem but now solved with this cmd:
python3 manage.py runserver
Upvotes: 37
Reputation: 331
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
Upvotes: 3
Reputation: 3395
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
Upvotes: 2
Reputation: 57
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
Upvotes: 2
Reputation: 179
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 \
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.
Upvotes: 7
Reputation: 63
I have no problem running this way:
sudo ./**(your path)**/bin/python manage.py runserver
Upvotes: 0
Reputation: 283
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").
Upvotes: 3
Reputation: 958
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
Upvotes: 1
Reputation: 486
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
Upvotes: 0
Reputation: 8836
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.
Upvotes: 20