我已经安装了virtualenv,然后在Windows 10中安装了django。激活virtualenv并运行:python manage.py runserver
之后,我得到了:
File "manage.py", line 10, in main
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
The above exception was the direct cause of the following exception:
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?
在运行django-admin.exe
时也发现:
Note that only Django core commands are listed as settings are not properly
configured (error: Requested setting INSTALLED_APPS, but settings are not
configured. You must either define the environment variable
DJANGO_SETTINGS_MODULE or call settings.configure() before accessing
settings.).
Manage.py:
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'wordcount.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 __name__ == '__main__':
main()
答案 0 :(得分:0)
您可以通过创建python程序并导入django来验证是否已安装django
import django
print (django.VERSION)
以上代码将打印已安装的django版本,确认是否已安装django
答案 1 :(得分:0)
您有2个Python版本:主要版本是默认安装的,另一个版本是virtualenv使用的。
在运行pip install django
时,Python的主版本中安装了Django,这是因为PYTHONPATH环境变量引用的是主版本的路径,而不是virtualenv。
运行python manage.py runserver
时也会发生同样的事情。它不能从virtualenv运行python。
要解决此问题,您需要从virtualenv中访问pip,然后可以将Django与之一起安装
C:\the\path\to\virtualenv\path\to\pip.exe install django
就像pip一样,从virtualenv运行python.exe
C:\the\path\to\virtualenv\path\to\python.exe manage.py runserver
如果您使用PyCharm进行开发,则可以轻松地将venv设置为解释器。
现在可以运行django,就像pip一样,您可以从virtualenv访问python
1-文件>设置>您的项目> Python解释器
2-单击右侧的设置图标,然后单击“添加”
3-单击Virtualenv Environment并选择位置
设置virtualenv后,您可以轻松地使用PyCharm来管理软件包