fedora 中 anaconda環境配置
不問anaconda是什麼,只問anaconda裡有什麼。anaconda 裡有python、numpy等科學計算庫,可以方便安裝 pytorch、tensorflow等深度學習庫,可以建立虛擬環境。
1. 安裝anaconda
作業系統為 Fedora workstation 29 x86_64
下載個人版 https://repo.anaconda.com/archive/Anaconda3-2021.05-Linux-x86_64.sh
如何檢視安裝是否成功?可以使用conda list | grep numpy
,如果執行成功且能看到numpy 庫,說明安裝成功。
1.1 啟用conda環境
如果啟用conda環境?安裝指令碼預設往~/.bashrc
裡寫入啟用指令碼,
# >>> conda initialize >>> # !! Contents within this block are managed by 'conda init' !! __conda_setup="$('/home/software/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)" if [ $? -eq 0 ]; then eval "$__conda_setup" else if [ -f "/home/software/anaconda3/etc/profile.d/conda.sh" ]; then . "/home/software/anaconda3/etc/profile.d/conda.sh" else export PATH="/home/software/anaconda3/bin:$PATH" fi fi unset __conda_setup # <<< conda initialize <<<
使用source ~/.bashrc
即可自動啟用
如果取消自動啟用需要使用conda config --set auto_activate_base false
修改~/.condarc
show_channel_urls: true
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- defaults
auto_activate_base: false
手動啟用執行 conda activate
終端bash介面命令列會顯示
(base) [user@localhost anaconda3]$ conda activate
(base) [user@localhost anaconda3]$
這時如果執行 python 則顯示為 anaconda 裡的python 版本
(base) [user@localhost anaconda3]$ python
Python 3.8.8 (default, Apr 13 2021, 19:58:26)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
使用 conda deactivate
手動進行退出。
1.2 修改映象源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
1.3 pycharm 使用 anaconda
PyCharm 2021.2 (Professional Edition)
Build #PY-212.4746.96, built on July 27, 2021
在專案中設定 anaconda 裡的python環境。
1.4 pycharm 中執行示例
點選檢視程式碼
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import numpy as np
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
a = np.arange(15).reshape(3, 5)
print(a)
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print_hi('PyCharm')
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
2. 安裝科學計算庫
先看conda list
預設是否安裝。這裡安裝了 numpy 1.20.1
, scikit-learn 0.24.1
, scipy 1.6.2
.
2.1 安裝 pytorch cpu 版本
https://pytorch.org/ 提供了安裝命令
conda install pytorch torchvision torchaudio cpuonly -c pytorch
成功安裝 pytorch 1.9.1 cpu-only
示例程式:
[user@localhost anaconda3]$ conda activate
(base) [user@localhost anaconda3]$ python
Python 3.8.8 (default, Apr 13 2021, 19:58:26)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import numpy as np
>>> data = [[1, 2],[3, 4]]
>>> x_data = torch.tensor(data)
>>> print(x_data)
tensor([[1, 2],
[3, 4]])
>>>
2.2
[1] https://docs.anaconda.com/anaconda/install/linux/
[2] https://askubuntu.com/questions/1026383/why-does-base-appear-in-front-of-my-terminal-prompt
[3] https://stackoverflow.com/questions/55171696/how-to-remove-base-from-terminal-prompt-after-updating-conda
[4] https://zhuanlan.zhihu.com/p/348120084