一、環(huán)境配置
1.0 安裝
從
1棵介、官網(wǎng):https://www.anaconda.com/products/individual#linux
2灯帮、國(guó)內(nèi)鏡像:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
下載,https://repo.anaconda.com/archive/Anaconda3-2020.02-Linux-x86_64.sh
如果為debain或Ubuntu鉴分,則apt-get install libgl1-mesa-glx libegl1-mesa libxrandr2 libxrandr2 libxss1 libxcursor1 libxcomposite1 libasound2 libxi6 libxtst6
妹田,安裝依賴包贬丛。
如果為redhat或centos撩银,則執(zhí)行yum install libXcomposite libXcursor libXi libXtst libXrandr alsa-lib mesa-libEGL libXdamage mesa-libGL libXScrnSaver
,安裝依賴包豺憔。
安裝:
bash Anaconda3-2020.02-Linux-x86_64.sh
由于使用非root用戶额获,則安裝在家目錄下PREFIX=/home/thirsd/anaconda3
1.1 設(shè)置環(huán)境變量
假設(shè)anaconda部署在D:\Programs\Anaconda3
,在環(huán)境變量中配置:
D:\Programs\Anaconda3;D:\Programs\Anaconda3\Scripts;D:\Programs\Anaconda3\Library\bin
輸入 conda -V
檢驗(yàn)是否安裝以及當(dāng)前conda的版本
查看anaconda的配置信息
conda config --show-sources
可以查看到.condarc的配置信息內(nèi)容
1.2 驗(yàn)證環(huán)境變量
1)conda list 查看安裝了哪些包
2)conda env list 或 conda info -e 查看當(dāng)前存在哪些虛擬環(huán)境
3)conda update conda 檢查更新當(dāng)前conda
1.3 安裝第三方庫(kù)
1.3.1 直接安裝
1恭应、查詢是否存在第三方包
anaconda search -t conda jieba`
d:\Programs\Anaconda5.3.1\Scripts>anaconda search -t conda jieba
Using Anaconda API: https://api.anaconda.org
Packages:
Name | Version | Package Types | Platforms | Builds
------------------------- | ------ | --------------- | --------------- | ----------
auto/jieba | 0.32 | conda | linux-64, linux-32 | py27_0
: http://github.com/fxsjy
conda-forge/jieba | 0.39 | conda | linux-64, win-32, osx-64, noarch, win-64 | py_1, py36_0, p
yh9f0ad1d_1, py35_0, py27_0
: Chinese Words Segementation Utilities
conda-forge/jieba3k | 0.35.1 | conda | linux-64, win-32, osx-64, win-64, noarch | py36_1001, py36
h9f0ad1d_1002, py37_1001, py38h32f6830_1002, py36_1, py37hc8dfbb8_1002, py36hc560c46_1002, py_0, py35_1001, py38_1001, p
y35_1
: Chinese Words Segementation Utilities
creditx/jieba | 0.38 | conda | linux-64 | py35_0, py27_0
hargup/jieba | | conda | linux-64 | py27_0
: Chinese Words Segementation Utilities
iilab/jieba | 0.36.2 | conda | linux-64, osx-64 | py34_0
: Chinese Words Segementation Utilities
2抄邀、獲取安裝命令
anaconda show conda-forge/jieba
d:\Programs\Anaconda5.3.1\Scripts>anaconda show conda-forge/jieba
Using Anaconda API: https://api.anaconda.org
Name: jieba
Summary: Chinese Words Segementation Utilities
Access: public
Package Types: conda
Versions:
+ 0.38
+ 0.39
To install this package with conda run:
conda install --channel https://conda.anaconda.org/conda-forge jieba
根據(jù)安裝jieba,執(zhí)行最后一行的信息即可安裝輸入
3、執(zhí)行安裝
d:\Programs\Anaconda5.3.1\Scripts>conda install --channel https://conda.anaconda.org/conda-forge jieba
Solving environment: done
1.3.2 使用源碼安裝
1昼榛、打開(kāi)anaconda promote的窗口
2境肾、切換至源碼目錄,并執(zhí)行python setup.py install
二胆屿、虛擬環(huán)境創(chuàng)建
2.1 查看當(dāng)前的虛擬環(huán)境
2.1.1 查看虛擬環(huán)境列表
conda env list
奥喻,輸出:
(django_restful) C:\Users\thirsd>conda env list
conda environments:
base D:\Programs\Anaconda3
django_restful * D:\Programs\Anaconda3\envs\django_restful
2.1.2 設(shè)置國(guó)內(nèi)源
命令:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
信息配置在~/.condarc中,如:
thirsd@thirsd-16:~$ cat .condarc
channels:
- defaults
show_channel_urls: true
channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
運(yùn)行 conda clean -i
清除索引緩存非迹,保證用的是鏡像站提供的索引
2.2 創(chuàng)建虛擬環(huán)境
創(chuàng)建虛擬環(huán)境:
conda create -n django_restful python=3.7
創(chuàng)建名字為django_restful的虛擬環(huán)境环鲤,指定的python版本為3.7
此時(shí)虛擬環(huán)境安裝在:
anaconda的部署目錄下的envs目錄下,例如憎兽, /home/thirsd/anaconda3/envs/django_restful/
2.3 配置虛擬環(huán)境
2.3.1切換為虛擬環(huán)境
切換虛擬環(huán)境為django_restful
conda activate django_restful
此時(shí)命令提示符包含虛擬環(huán)境冷离,如下所示:
(django_restful) thirsd@thirsd-16:~$
2.3.2 搜索需要安裝的包
conda search django*
例如:
(django_restful) thirsd@thirsd-16:~$ conda search django*
Loading channels: done
# Name Version Build Channel
django 1.6.5 py26_0 anaconda/pkgs/free
django 1.6.5 py27_0 anaconda/pkgs/free
django 1.6.5 py33_0 anaconda/pkgs/free
django 1.6.5 py34_0 anaconda/pkgs/free
django 1.6.6 py26_0 anaconda/pkgs/free
2.3.4 在虛擬環(huán)境中吵冒,安裝指定包
在django_restful的虛擬環(huán)境中,安裝djangorestframework
conda install djangorestframework -n django_restful
conda install markdown -n django_restful # Markdown support for the browsable API.
conda install django-filter -n django_restful # Filtering support
conda install pygments -n django_restful # We'll be using this for the code highlighting
2.3.5 查看虛擬環(huán)境中的安裝包情況
查看虛擬環(huán)境django_restful中的安裝包情況
(django_restful) thirsd@thirsd-16:~$ conda list -n django_restful
2.3.6 更新虛擬環(huán)境中的安裝包
更新命令為:`conda update $package_name -n $ $your_env_name`
conda update markdown -n django_restful
2.3.6 刪除虛擬機(jī)環(huán)境中的安裝包
刪除命令格式為:`conda remove --name $your_env_name $package_name`
將markdown的包西剥,從虛擬環(huán)境django_restful中刪除
conda remove markdown -n django_restful
2.4 退出虛擬環(huán)境
從虛擬環(huán)境django_restful中痹栖,退出,執(zhí)行`conda deactivate`
(django_restful) thirsd@thirsd-16:~$ conda deactivate
(base) thirsd@thirsd-16:~$
2.5 虛擬環(huán)境遷移
2.5.1導(dǎo)出虛擬環(huán)境
分享代碼的時(shí)候,同時(shí)也需要將運(yùn)行環(huán)境分享給大家
conda django_restful export > env.yaml
2.5.2 恢復(fù)虛擬環(huán)境
用對(duì)方分享的YAML文件來(lái)創(chuàng)建一模一樣的運(yùn)行環(huán)境
conda django_restful create -f env.yaml
2.6 刪除虛擬環(huán)境
conda remove -n your_env_name(虛擬環(huán)境名稱) --all
三瞭空、Djangorestfulframwork項(xiàng)目配置
3.1 手工配置
cd projects/ #進(jìn)入項(xiàng)目保存的位置
django-admin startproject restful_demo #創(chuàng)建項(xiàng)目
python manage.py startapp app #創(chuàng)建web服務(wù)
目錄結(jié)果如下:
(django_restful) thirsd@thirsd-16:~/projects/restful_demo ll
total 20
drwxrwxr-x 3 thirsd thirsd 4096 May 9 02:03 app/
-rwxrwxr-x 1 thirsd thirsd 632 May 9 02:02 manage.py*
drwxrwxr-x 3 thirsd thirsd 4096 May 9 02:02 restful_demo/(django_restful) thirsd@thirsd-16:~/projects/restful_demo$ ll app/
total 32
-rw-rw-r-- 1 thirsd thirsd 63 May 9 02:03 admin.py
-rw-rw-r-- 1 thirsd thirsd 81 May 9 02:03 apps.py
-rw-rw-r-- 1 thirsd thirsd 0 May 9 02:03 init.py
drwxrwxr-x 2 thirsd thirsd 4096 May 9 02:03 migrations/
-rw-rw-r-- 1 thirsd thirsd 57 May 9 02:03 models.py
-rw-rw-r-- 1 thirsd thirsd 60 May 9 02:03 tests.py
-rw-rw-r-- 1 thirsd thirsd 63 May 9 02:03 views.py
將新建的app的web應(yīng)用和rest_framework
结耀,加入到配置~/projects/restful_demo/restful_demo/settings.py中:
INSTALLED_APPS = [
...
'rest_framework',
'app.apps.appConfig',
]
app.apps.appConfig,實(shí)際為`${webname}.apps.${webname}Config`
實(shí)際存儲(chǔ)情況為:
(django_restful) thirsd@thirsd-16:~/projects/restful_demo$ cat app/apps.py
from django.apps import AppConfig
class AppConfig(AppConfig):
name = 'app'
3.2 通過(guò)pycharm來(lái)配置
為支持遠(yuǎn)程訪問(wèn)匙铡,在我們創(chuàng)建的項(xiàng)目里**修改setting.py文件**
ALLOWED_HOSTS = ['*'] #在這里請(qǐng)求的host添加了*