设置python 虚拟环境并激活,然后安装Django开发版本
这里可以本人参考博客
新建项目
之前安装Django以及虚拟环境都是在C盘下处理的,在这里用D盘存储项目
出现小插曲:
C:\Users\用户名>d:
D:\>cd .DjangoProj
D:\.DjangoProj>django-admin startproject mysite
'django-admin' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
D:\.DjangoProj>python
Python 3.8.5 (default, Sep 3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated. Libraries may fail to load. To activate this environment
please see https://conda.io/activation
Type "help", "copyright", "credits" or "license" for more information.
>>> django-admin startproject mysite
File "" , line 1
django-admin startproject mysite
^
SyntaxError: invalid syntax
>>> import Django
Traceback (most recent call last):
File "" , line 1, in <module>
ModuleNotFoundError: No module named 'Django'
>>> import django
Traceback (most recent call last):
File "" , line 1, in <module>
ModuleNotFoundError: No module named 'django'
>>>
D:\.DjangoProj>python -m django --version
D:\Anaconda\python.exe: No module named django
D:\.DjangoProj>python -m django --version
D:\Anaconda\python.exe: No module named django
但是没关系,我们可以在这里同样使用命令
python -m pip install Django -i https://pypi.tuna.tsinghua.edu.cn/simple/
然后:
D:\.DjangoProj>python -m pip install Django -i https://pypi.tuna.tsinghua.edu.cn/simple/
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting Django
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/a8/ca/e88eb097959c48cd313dfc4bc394699a48fe5c158ed3a64c13e4fa46c1fd/Django-3.2.9-py3-none-any.whl (7.9 MB)
Collecting asgiref<4,>=3.3.2
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/fe/66/577f32b54c50dcd8dec38447258e82ed327ecb86820d67ae7b3dea784f13/asgiref-3.4.1-py3-none-any.whl (25 kB)
Collecting sqlparse>=0.2.2
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/05/40/d836d55fb3f467243ee839ab7b814822fda522cd395fa41e282684e71ee5/sqlparse-0.4.2-py3-none-any.whl (42 kB)
Requirement already satisfied: pytz in d:\anaconda\lib\site-packages (from Django) (2020.1)
Installing collected packages: asgiref, sqlparse, Django
Successfully installed Django-3.2.9 asgiref-3.4.1 sqlparse-0.4.2
D:\.DjangoProj>django-admin startproject mysite
D:\.DjangoProj>
没有错误发生
发现创建了文件夹mysite
并且项目结构为:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
这些目录和文件的用处是:
然后出现了问题:
D:\.DjangoProj\mysite>python manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "D:\Anaconda\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "D:\Anaconda\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "D:\Anaconda\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "D:\Anaconda\lib\site-packages\django\core\management\commands\runserver.py", line 110, in inner_run
autoreload.raise_last_exception()
File "D:\Anaconda\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
raise _exception[1]
File "D:\Anaconda\lib\site-packages\django\core\management\__init__.py", line 375, in execute
autoreload.check_errors(django.setup)()
File "D:\Anaconda\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "D:\Anaconda\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "D:\Anaconda\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "D:\Anaconda\lib\site-packages\django\apps\config.py", line 301, in import_models
self.models_module = import_module(models_module_name)
File "D:\Anaconda\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "" , line 1014, in _gcd_import
File "" , line 991, in _find_and_load
File "" , line 975, in _find_and_load_unlocked
File "" , line 671, in _load_unlocked
File "" , line 783, in exec_module
File "" , line 219, in _call_with_frames_removed
File "D:\Anaconda\lib\site-packages\django\contrib\auth\models.py", line 3, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "D:\Anaconda\lib\site-packages\django\contrib\auth\base_user.py", line 48, in <module>
class AbstractBaseUser(models.Model):
File "D:\Anaconda\lib\site-packages\django\db\models\base.py", line 122, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "D:\Anaconda\lib\site-packages\django\db\models\base.py", line 326, in add_to_class
value.contribute_to_class(cls, name)
File "D:\Anaconda\lib\site-packages\django\db\models\options.py", line 207, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "D:\Anaconda\lib\site-packages\django\utils\connection.py", line 15, in __getattr__
return getattr(self._connections[self._alias], item)
File "D:\Anaconda\lib\site-packages\django\utils\connection.py", line 62, in __getitem__
conn = self.create_connection(alias)
File "D:\Anaconda\lib\site-packages\django\db\utils.py", line 204, in create_connection
backend = load_backend(db['ENGINE'])
File "D:\Anaconda\lib\site-packages\django\db\utils.py", line 111, in load_backend
return import_module('%s.base' % backend_name)
File "D:\Anaconda\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "D:\Anaconda\lib\site-packages\django\db\backends\sqlite3\base.py", line 15, in <module>
from sqlite3 import dbapi2 as Database
File "D:\Anaconda\lib\sqlite3\__init__.py", line 23, in <module>
from sqlite3.dbapi2 import *
File "D:\Anaconda\lib\sqlite3\dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: DLL load failed while importing _sqlite3: 找不到指定的模块。
是因为缺少mysqlclient
吗?
然后:
pip install mysqlclient -i https://pypi.tuna.tsinghua.edu.cn/simple/
依旧不行
换到C盘下并且在虚拟环境中执行该命令:
发现玄学问题:
根据教程找到:
从网站下载对应文件并解压
网站传送门
解压后移动到Andconda下对应目录,如图:
执行命令之后:
然后成功!!!!!!!