1 下载并安装python3,在安装过程中勾选同时安装pip,紧接着pip安装virtualenv
安装完以后,可以在DOS下敲入命令查看
C:\Users\wenca\Desktop\Python>python -V
Python 3.7.3
C:\Users\wenca\Desktop\Python>pip -V
pip 19.0.3 from c:\program files\python37\lib\site-packages\pip (python 3.7)
C:\Users\wenca\Desktop\Python>pip install virtualenv
Requirement already satisfied: virtualenv in c:\program files\python37\lib\site-packages (16.7.2)
C:\Users\wenca\Desktop\Python>pip list
Package Version
---------- -------
pip 19.0.3
setuptools 40.8.0
virtualenv 16.7.2
2 新建一个目录,用于专门存放各个项目虚拟环境
C:\Users\wenca\Desktop\Python>mkdir virtual_environment
C:\Users\wenca\Desktop\Python>dir
virtual_environment/
C:\Users\wenca\Desktop\Python>cd virtual_environment/
3 开始搭建虚拟环境
3.1 新建一个特定项目虚拟环境
C:\Users\wenca\Desktop\Python\virtual_environment>virtualenv --no-site-packages --python=3.7 signup_sign
Running virtualenv with interpreter C:\Program Files\Python37\python.exe
Already using interpreter C:\Program Files\Python37\python.exe
Using base prefix 'C:\\Program Files\\Python37'
New python executable in C:\Users\wenca\Desktop\Python\virtual_environment\signup_sign\Scripts\python.exe
Installing setuptools, pip, wheel...
等待几分钟后,可以查看虚拟环境下的目录结构
C:\Users\wenca\Desktop\Python\virtual_environment>dir signup_signin/
Include/ Lib/ LICENSE.txt Scripts/ signup_signin/
3.2 进入到虚拟环境
C:\Users\wenca\Desktop\Python\virtual_environment>signup_signin\Scripts\activate
进入虚拟环境后,提示部分变成,最左边为【(虚拟环境名)】
(signup_sign) c:\Users\wenca\Desktop\Python\virtual_environment>
3.3 安装项目所需要的各种库,例如本文的django项目,需要djano框架库
(signup_sign) c:\Users\wenca\Desktop\Python\virtual_environment>pip install django
安装完以后,可以查看
(signup_sign) c:\Users\wenca\Desktop\Python\virtual_environment>pip list
Package Version
---------- -------
Django 2.2.4
pip 19.0.3
pytz 2019.2
setuptools 40.8.0
sqlparse 0.3.0
virtualenv 16.7.2
在虚拟环境中pip安装的lib,也会被安装到宿主机上,不过另一套虚拟环境可以不用宿主机的lib(加上参数--no-site-packages即可),即实现了隔离。
3.4 开始创建django项目
(signup_sign) c:\Users\wenca\Desktop\Python\virtual_environment>cd signup_signin/
(signup_sign) c:\Users\wenca\Desktop\Python\virtual_environment\signup_signin>django-admin startproject signup_signin
完成以后,会在当前路径下新增一个目录signup_signin/,并且里面还有其他子目录和文件
(signup_sign) c:\Users\wenca\Desktop\Python\virtual_environment\signup_signin>ls
Include/ Lib/ LICENSE.txt Scripts/ signup_signin/
(signup_sign) c:\Users\wenca\Desktop\Python\virtual_environment\signup_signin>ls signup_signin/
db.sqlite3 manage.py* signup_signin/
3.5 运行django框架提供给开发者的web服务器
(signup_sign) c:\Users\wenca\Desktop\Python\virtual_environment\signup_signin\signup_signin>python manage.py runserver 5000
此时会弹出日志信息
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
August 12, 2019 - 21:20:23
Django version 2.2.4, using settings 'signup_signin.settings'
Starting development server at http://127.0.0.1:5000/
Quit the server with CTRL-BREAK.
3.6 测试框架是否正常运行
用浏览器打开【http://127.0.0.1:500】链接,结果如下所示,说明一切正常。