Python Django manage.py runserver 无效语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47880626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django manage.py runserver invalid syntax
提问by Bharata
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:
我正在使用 django 在 Ubuntu 中开发网络。一切正常。现在,我想更换使用 Windows 的计算机。当我尝试运行服务器时,它给出:
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.
如上所示,我已经安装了 Python 3.6.3。我也使用 pip3 安装了 django 和其他必要的库。
Edit: manage.py file, it is a default manage.py that I get when generating the project.
编辑:manage.py 文件,它是我在生成项目时得到的默认 manage.py。
#!/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)
回答by suria sarath
I faced same problem but now solved with this cmd:
我遇到了同样的问题,但现在用这个 cmd 解决了:
python3 manage.py runserver
回答by arulmr
Edit your manage.py
file as given below:
编辑您的manage.py
文件,如下所示:
#!/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.
请注意,from exc
已从文件中删除。manage.py
文件中不需要它。
回答by HAL 9000
- 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.
- 确保您的 virtualenv 已激活。假设您的 virtualenv 的名称是 pythonpy,然后运行以下命令:
virtualenv pythonpy workon pythonpy #运行这些命令后,你应该看到类似这样的东西,但你的文件路径可能不同:“(pythonpy)C:\Users\MyDjangoProject\
- 然后转到包含 manage.py (pythonpy) C:\Users\ MyDjangoProject \ #Same 路径的项目文件夹
- 然后简单地运行服务器:
python manage.py runserver #这将为您提供本地主机的项目路径。将 URL 复制并粘贴到浏览器中,应该可以正常工作。
回答by Hendra Sirait
i've also got this error but solve it by installing pipenv first,
我也有这个错误,但先安装 pipenv 解决它,
try run this first
尝试先运行这个
pipenv shell django==2.1
then you should be able to run
那么你应该能够运行
python3 manage.py runserver
回答by 7guyo
Ensure you're running the app from virtualenv i.e if you've created a virtualenv for your project, then activate the venv first.
确保您正在从 virtualenv 运行应用程序,即如果您已经为您的项目创建了一个 virtualenv,那么首先激活 venv。
me@debian:~/Desktop/webapp$source venv/bin/activate
(venv) me@debian:~/Desktop/webapp$python manage.py runserver
回答by ADEL NAMANI
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
回答by Micah Walter
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
.
发生的情况是使用了错误版本的 python,它可能没有你的 virtualenv 中的所有依赖项。使用时出现此错误sudo manage.py
:使用sudo
更改正在使用的python版本/usr/bin/python
。
The problem is solved by specifying which version of python to use when using sudo
:
通过指定使用时使用哪个版本的python来解决问题sudo
:
sudo /path/to/my/env/bin/python manage.py makemigrations
回答by Mountain Scott
Try (from command line):
尝试(从命令行):
python3 manage.py runserver
If I used this (no python3) instead:
如果我使用这个(没有 python 3):
python manage.py runserver
the error persisted. This method allows you to not have to alter manage.py (so you can keep "from exc").
错误仍然存在。此方法允许您不必更改 manage.py(因此您可以保持“from exc”)。
回答by user9521248
I have no problem running this way:
我这样运行没有问题:
sudo ./**(your path)**/bin/python manage.py runserver
回答by Serge Kishiko
I've got also the same issue with Python 3.4.4and Django 2.0. I tried the last solution, nothing works (no need to delete that: from exc
on the line 14).
我在Python 3.4.4和Django 2.0 中也遇到了同样的问题。我尝试了最后一个解决方案,没有任何效果(无需删除:from exc
在第 14 行)。
Just run your server with:
只需运行您的服务器:
python manage.py runserver
Instead of:
代替:
./manage.py runserver #or '.\manage.py runserver' for Windows