【发布时间】:2020-10-18 16:54:00
【问题描述】:
我是使用 Django 和 Python 的新手,在尝试让我的 manage.py 应用程序在虚拟环境中运行时遇到了一些困难。
我的 models.py 文件顶部出现以下错误:
我最初认为这是 pylint 未指向正确环境的问题(当我将 Python 解释器切换到虚拟环境之外的解释器时,pylint 错误消失了);但是,每当我尝试运行我的服务器时,都会收到以下错误:
(.venv) tjmcerlean@LAPTOP-RKLCBQLO:/mnt/c/Users/tjmce/Desktop/Git/CS50w/Project 2/commerce$ python manage.py runserverTraceback (most recent call last):
File "manage.py", line 10, in main
from django.core.management import execute_from_command_line
File "/mnt/c/Users/tjmce/Desktop/Git/CS50w/Project 2/commerce/.venv/lib/python3.8/site-packages/django/__init__.py", line 1, in <module>
from django.utils.version import get_version
ModuleNotFoundError: No module named 'django.utils'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 12, in main
raise ImportError(
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?
此错误似乎表明虚拟环境未激活。但是,我使用虚拟环境的位置(.venv/bin/activate)激活它,并看到 bash 终端现在显示(.venv):
另外,当使用 (pip freeze) 命令时,我可以看到 Django 也安装在虚拟环境中:
我不知道为什么服务器无法运行,非常感谢任何建议。
谢谢!
【问题讨论】:
-
你有没有把你的项目命名为
django? -
不,很遗憾没有。
-
你的 PYTHONPATH 变量中有什么?
-
您是否尝试重新安装 Django 或重新创建 virtualenv
-
是的,我已经重新创建了虚拟环境并重新安装了 Django,但没有运气。我在新环境中遇到了同样的错误。