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 django
Using base prefix 'c:\users\administrator\appdata\local\programs\python\python36-32
New python executable in E:\env\django\Scripts\python.exe
Installing setuptools, pip, wheel...done.
現(xiàn)在,virtualenv為我們在工程目錄env中,創(chuàng)建了一個(gè)名為django的子目錄,它里面保存了一個(gè)全新的虛擬環(huán)境,非常簡單。
激活和退出虛擬環(huán)境
在使用虛擬環(huán)境之前,必須將其激活,命令如下:
E:\env>cd django
E:\env\django>cd Scripts
E:\env\django\Scripts>activate
這時(shí),可以看到shell提示符前面加上了(venv)前綴,說明已經(jīng)工作在虛擬環(huán)境之下了
(django) E:\env\django\Scripts>
退出虛擬環(huán)境只要執(zhí)行:
(django) E:\env\django\Scripts>deactivate
在虛擬環(huán)境中安裝Django
E:\env\django\Scripts>pip install django==1.11
在E盤目錄的project文件夾創(chuàng)建第一個(gè)Django項(xiàng)目
E:\env\django\Scripts>activate
(django) E:\env\django\Scripts>cd /
(django) E:\env\django\Scripts>cd e:\wordspace\django
(django) E:\wordspace\django>django-admin startproject day01 #(項(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ū)動的頁面正常工作了
在pychorm中的話先打開創(chuàng)建的項(xiàng)目的文件夾,然后給你這個(gè)項(xiàng)目添加你剛才創(chuàng)建的虛擬環(huán)境
E:\env\django\Script\python.exe
(直接添加你剛才創(chuàng)建的虛擬環(huán)境的目錄下的python.exe)
現(xiàn)在你就可以在pychorm中編輯你的項(xiàng)目了。