anaconda 的虛擬環境使用

一、環境配置

1.0 安裝

1、官網:https://www.anaconda.com/products/individual#linux

2、國內鏡像: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,則執行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 設置環境變量

假設anaconda部署在D:\Programs\Anaconda3,在環境變量中配置:

D:\Programs\Anaconda3;D:\Programs\Anaconda3\Scripts;D:\Programs\Anaconda3\Library\bin

輸入 conda -V 檢驗是否安裝以及當前conda的版本
查看anaconda的配置信息

conda config --show-sources

可以查看到.condarc的配置信息內容

1.2 驗證環境變量

1)conda list 查看安裝了哪些包

2)conda env list 或 conda info -e 查看當前存在哪些虛擬環境

3)conda update conda 檢查更新當前conda

1.3 安裝第三方庫

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

根據安裝jieba,執行最后一行的信息即可安裝輸入

3、執行安裝

d:\Programs\Anaconda5.3.1\Scripts>conda install --channel https://conda.anaconda.org/conda-forge jieba
    Solving environment: done

1.3.2 使用源碼安裝

1、打開anaconda promote的窗口

2、切換至源碼目錄,并執行python setup.py install

二、虛擬環境創建

2.1 查看當前的虛擬環境

2.1.1 查看虛擬環境列表

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 設置國內源

命令:
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

運行 conda clean -i 清除索引緩存,保證用的是鏡像站提供的索引

2.2 創建虛擬環境

創建虛擬環境:

conda create -n django_restful python=3.7

創建名字為django_restful的虛擬環境,指定的python版本為3.7

此時虛擬環境安裝在:

anaconda的部署目錄下的envs目錄下,例如, /home/thirsd/anaconda3/envs/django_restful/

2.3 配置虛擬環境

2.3.1切換為虛擬環境

切換虛擬環境為django_restful
conda activate django_restful
此時命令提示符包含虛擬環境,如下所示:
(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 在虛擬環境中,安裝指定包

在django_restful的虛擬環境中,安裝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 查看虛擬環境中的安裝包情況

查看虛擬環境django_restful中的安裝包情況
(django_restful) thirsd@thirsd-16:~$ conda list -n django_restful

2.3.6 更新虛擬環境中的安裝包

更新命令為:`conda update $package_name -n $ $your_env_name`
conda update markdown -n django_restful

2.3.6 刪除虛擬機環境中的安裝包

刪除命令格式為:`conda remove --name $your_env_name  $package_name`

將markdown的包,從虛擬環境django_restful中刪除
conda remove markdown -n django_restful

2.4 退出虛擬環境

從虛擬環境django_restful中,退出,執行`conda deactivate`
(django_restful) thirsd@thirsd-16:~$ conda deactivate
(base) thirsd@thirsd-16:~$ 

2.5 虛擬環境遷移

2.5.1導出虛擬環境

分享代碼的時候,同時也需要將運行環境分享給大家
conda django_restful export > env.yaml

2.5.2 恢復虛擬環境

用對方分享的YAML文件來創建一模一樣的運行環境
conda django_restful create -f env.yaml

2.6 刪除虛擬環境

conda remove -n your_env_name(虛擬環境名稱) --all

三、Djangorestfulframwork項目配置

3.1 手工配置

cd projects/ #進入項目保存的位置
django-admin startproject restful_demo  #創建項目
python manage.py startapp app   #創建web服務
目錄結果如下:

(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應用和rest_framework ,加入到配置~/projects/restful_demo/restful_demo/settings.py中:

INSTALLED_APPS = [
    ...
    'rest_framework',
    'app.apps.appConfig',
]
app.apps.appConfig,實際為`${webname}.apps.${webname}Config`

實際存儲情況為:
(django_restful) thirsd@thirsd-16:~/projects/restful_demo$ cat app/apps.py 
from django.apps import AppConfig


class AppConfig(AppConfig):
    name = 'app'

3.2 通過pycharm來配置

為支持遠程訪問,在我們創建的項目里**修改setting.py文件**
ALLOWED_HOSTS = ['*']  #在這里請求的host添加了*
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,119評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,382評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,038評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,853評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,616評論 6 408
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,112評論 1 323
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,192評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,355評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,869評論 1 334
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,727評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,928評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,467評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,165評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,570評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,813評論 1 282
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,585評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,892評論 2 372

推薦閱讀更多精彩內容