pip软件包管理工具介绍及基本使用:https://blog.csdn.net/weixin_44621343/article/details/114945338
国内常用镜像源
清华大学 :https://pypi.tuna.tsinghua.edu.cn/simple/
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科学技术大学 :http://pypi.mirrors.ustc.edu.cn/simple/
华中科技大学:http://pypi.hustunique.com/
豆瓣源:http://pypi.douban.com/simple/
腾讯源:http://mirrors.cloud.tencent.com/pypi/simple
华为镜像源:https://repo.huaweicloud.com/repository/pypi/simple/
Windows平台永久修改配置
[global]
index-url=https://pypi.tuna.tsinghua.edu.cn/simple
[install]
use-mirrors=true
mirrors=https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host=pypi.tuna.tsinghua.edu.cn
1.安装:
pip3 install virtualenv
pip3 install virtualenvwrapper-win # windows
2.配置环境变量
3.Python3解释器安装目录下的Scripts文件夹下的virtualenvwrapper.bat文件双击一下
然后就可以创建虚拟环境了:
创建虚拟环境到配置的WORKON_HOME路径下。
选取默认Python环境创建虚拟环境:
-- mkvirtualenv 虚拟环境名称
基于某Python环境创建虚拟环境:
-- mkvirtualenv -p python2.7 虚拟环境名称
-- mkvirtualenv -p python3.6 虚拟环境名称
虚拟环境指令:
# 1、查看已有的虚拟环境
-- workon
# 2、使用某个虚拟环境
-- workon 虚拟环境名称
# 3、进入|退出 该虚拟环境的Python环境
-- python | exit()
# 4、为虚拟环境安装模块
-- pip或pip3 install 模块名
# 5、退出当前虚拟环境
-- deactivate
# 6、删除虚拟环境(删除当前虚拟环境要先退出)
-- rmvirtualenv 虚拟环境名称
安装django
进入虚拟环境,切换要保存项目的文件夹创建django项目
切换到django项目下启动项目
重构项目目录
├── luffy
├── logs/ # 项目运行时/开发时日志目录 - 包
├── luffy/ # 项目主应用,开发时的代码保存 - 包
├── apps/ # 开发者的代码保存目录,以模块[子应用]为目录保存 - 包
├── libs/ # 第三方类库的保存目录[第三方组件、模块] - 包
├── settings/ # 配置目录 - 包
├── dev.py # 项目开发时的本地配置
└── prod.py # 项目上线时的运行配置
├── urls.py # 总路由
└── utils/ # 多个模块[子应用]的公共函数类库[自己开发的组件]
├── scripts/ # 保存项目运营时的脚本文件 - 文件夹
└── manage.py # 脚本文件
在IDE中打开项目并重构目录
要运行需要修改manage.py中的代码
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'luffy.settings.dev')
上线需要修改wsgi.py中的代码
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'luffy.settings.prod')
创建app
在项目里的apps文件夹下创建django app
在dev.py中添加解释器环境变量
print(BASE_DIR) # D:\PythonLearn\luffy\luffy
# 将BASE_DIR和apps的路径添加到解释器环境变量中,在安装app时直接写app名字即可
import os
import sys
sys.path.extend([str(BASE_DIR), os.path.join(BASE_DIR, 'apps')])
print(sys.path)
# ['D:\\PythonLearn\\luffy', 'c:\\Users\\xuxiaoxu\\.vscode\\extensions\\ms-python.python-2022.18.0\\pythonFiles\\lib\\python\\debugpy\\_vendored\\pydevd', 'D:\\Python38\\python38.zip', 'D:\\Python38\\DLLs', 'D:\\Python38\\lib', 'D:\\Python38', 'D:\\Virtualenvs\\luffy', 'D:\\Virtualenvs\\luffy\\lib\\site-packages', 'D:/PythonLearn/luffy/luffy', 'D:\\PythonLearn\\luffy\\luffy\\apps']
安装app
INSTALLED_APPS = [
...
'course',
'home',
'user',
]