python虛擬環(huán)境--virtualenv
virtualenv
是一個(gè)創(chuàng)建隔絕的Python
環(huán)境的工具苍碟。virtualenv
創(chuàng)建一個(gè)包含所有必要的可執(zhí)行文件的文件夾,用來使用Python工程所需的包.
window安裝
pip install virtualenv 或 pip3 install virtualenv
Linux和Mac安裝
sudo pip install virtualenv 或 sudo pip3 install virtualenv
測試是否安裝成功
virtualenv --version
15.1.0
創(chuàng)建虛擬環(huán)境
下面以window為例悯恍,創(chuàng)建一個(gè)python3的虛擬環(huán)境。
假設(shè)在E盤已經(jīng)有一個(gè)工程目錄env伙狐,進(jìn)入工程目錄中涮毫,執(zhí)行以下命令:
C:\Users\Administrator>e:
E:>cd env
E:\env>virtualenv kyle
Using base prefix 'c:\users\administrator\appdata\local\programs\python\python36-32
New python executable in E:\env\kyle\Scripts\python.exe
Installing setuptools, pip, wheel...done.
現(xiàn)在瞬欧,virtualenv為我們在工程目錄env中,創(chuàng)建了一個(gè)名為kyle的子目錄罢防,它里面保存了一個(gè)全新的虛擬環(huán)境艘虎,非常簡單。
激活和退出虛擬環(huán)境
在使用虛擬環(huán)境之前咒吐,必須將其激活野建,命令如下:
E:\env>cd kyle
E:\env\kyle>cd Scripts
E:\env\kyle\Scripts>activate
這時(shí),可以看到shell提示符前面加上了(venv)前綴恬叹,說明已經(jīng)工作在虛擬環(huán)境之下了
(kyle) E:\env\kyle\Scripts>
退出虛擬環(huán)境只要執(zhí)行:
(kyle) E:\env\kyle\Scripts>deactivate
在虛擬環(huán)境中安裝Django
E:\env\testenv\Scripts>pip install django==1.11
在E盤目錄的project文件夾創(chuàng)建第一個(gè)Django項(xiàng)目
E:\env\testenv\Scripts>activate
(testenv) E:\env\testenv\Scripts>cd /
(testenv) E:>django-admin startproject hello #(項(xiàng)目名稱)
運(yùn)行manage.py查看方法
(testenv) E:>cd hello
(testenv) E:\hello>python manage.py
Type 'manage.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[auth]
changepassword
createsuperuser
[contenttypes]
remove_stale_contenttypes
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
[sessions]
clearsessions
[staticfiles]
collectstatic
findstatic
runserver
運(yùn)行manage.py runserver
(kyle) E:\hello>python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 13 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.
April 23, 2018 - 12:39:36
Django version 1.11, using settings 'hello.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
如果看到這樣了候生,那么恭喜你,你的第一個(gè)由Django的驅(qū)動(dòng)的頁面正常工作了