Mac builds anaconda environment and installs deep learning library

1. Download the anaconda installation package

Depending on your operating system, choose a different installation package Anaconda3-2024.06-1-MacOSX-x86_64.pkg. I still use the old Intel, so download this: https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ . If the Mac uses M1 or M2 chips, you need to download the suffix _arm64.pkg

2. Install anaconda

Double-click to open the installation package, and continue all the way. If the installation is completed and cannot be opened, please allow it in the settings

3. Configure environment variables

Open a terminal:

source ~/.bash_profile. If the path contains user/xxx, you need to change the user to your own directory path. I use the opt directory here, so there is no need to change it. So just source ~/.bash_profile to activate the environment.

Otherwise, you need to activate the environment after changing and saving.

Test whether the environment is installed successfully. If a lot of dependencies are output, it means the installation is successful:

conda list

4. Create a virtual environment for version 3.11.7

1. Enter the command to create a virtual environment for version 3.17:

conda create --name py3117 python=3.11.7

2. Activate the virtual environment:

conda activate py3117

3. Install common libraries:

conda install anaconda

4. Add the new virtual environment to Jupyter:

python -m ipykernel install --name=py3117

5. Install deep learning libraries such as pytorch, transformers, dashscope, modelscope, gradio, etc. Start jupyter at the end of this step.

6. Jupyter notebook installation directory plugin

# (安装了目录插件,我们用jupyter notebook读取python文本时就可以看到python的文本目录了,这样方便查看带有目录的python代码文本。)

# 在 cmd黑色命令窗口或者anaconda prompt 执行如下命令, (windows(windows窗口标志)+R,输入cmd,进入cmd命令窗口)

# 第一步:更新pip
python -m pip install --upgrade pip --user -i https://pypi.tuna.tsinghua.edu.cn/simple

# 第二步:更新Jupyter 
pip install --upgrade jupyter -i https://pypi.tuna.tsinghua.edu.cn/simple


# 第三步:安装 jupyter_contrib_nbextensions  
pip install jupyter_contrib_nbextensions -i https://pypi.tuna.tsinghua.edu.cn/simple


# 第四步:配置 nbextension
jupyter contrib nbextension install --user

# 直接使用以上命令报错:
File "/opt/anaconda3/lib/python3.12/site-packages/jupyter_contrib_core/notebook_compat/nbextensions.py", line 6, in <module>
    from notebook.extensions import BaseExtensionApp
ModuleNotFoundError: No module named 'notebook.extensions' 
# 解决办法:改用以下命令安装
conda install -c conda-forge jupyter_contrib_nbextensions
# 安装后,再次输入成功
jupyter contrib nbextension install --user

###安装问题备注:
出现连接中断之类的,做-i+国内源地址尝试
例如:pip install --upgrade jupyter
加国内源地址:  pip install --upgrade jupyter  -i https://pypi.tuna.tsinghua.edu.cn/simple

常用国内源地址
#清华大学源
https://pypi.tuna.tsinghua.edu.cn/simple

#阿里巴巴源
https://pypi.doubanio.com/simple

#中国科学技术大学源
https://pypi.mirrors.ustc.edu.cn/simple/

#豆瓣源
https://pypi.doubanio.com/simple
    

# Step 5: Start jupyter notebook, select Nbextensions, and check Table of Contents(2) ,

Optional steps
Check Collapsible headings---fold all the content inside the heading
   Code folding---code folding plug-in allows you to fold indented content to save screen space
   Hinterland---code prompt auto-completion function

After opening the "Various Libraries and Model Installation" code package on the Jupyter page, first change Kernel to py3117 (also keep using it when learning the other three code packages) 

After switching the kernel, start installing the deep learning libraries one by one.

 5. Install deep learning libraries

Here you can click on the code boxes one by one, click Run, and wait patiently for the download and installation. During the download and installation, this will become *, and each time a code box is completed, this will become a digital code.

If you don't want to install it here, you can also install it one by one in the command line

# python版本必须>3.10
# 推荐在安装库之前,先更新conda
# conda update conda

# 安装dashscope
!pip install dashscope -i https://pypi.tuna.tsinghua.edu.cn/simple

# 安装modelscope
!pip install modelscope -i https://pypi.tuna.tsinghua.edu.cn/simple

# 报错:ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
s3fs 2024.3.1 requires fsspec==2024.3.1, but you have fsspec 2024.2.0 which is incompatible.


# 安装pytorch
!pip3 install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple

# 安装transformers
!pip install transformers -i https://pypi.tuna.tsinghua.edu.cn/simple

# 安装datasets
!pip install datasets -i https://pypi.tuna.tsinghua.edu.cn/simple

# 安装gradio
!pip install gradio -i https://pypi.tuna.tsinghua.edu.cn/simple

# 下载上课用的预训练模型,warning信息请忽略
from modelscope.models import Model
model = Model.from_pretrained('damo/nlp_bert_fill-mask_chinese-base')
model = Model.from_pretrained('damo/nlp_structbert_zero-shot-classification_chinese-base')

If the above virtual environment is not switched and jupyter is used to install these libraries directly, the base kernel will be installed. Then, if you switch to the virtual environment later, you cannot install them. You need to enter the commands one by one in the command line to install them.

Note: Because the installation given by the teacher did not use the domestic mirror, I added the domestic mirror installation, which will be faster. 

After installing various libraries and models, congratulations! The basic environment is complete. 

7. Register an account

Register an Alibaba Cloud account

We will use Alibaba's model in this class. The URL is https://bailian.console.aliyun.com/#/model-market

Register a Model Scope account

Website: https://modelscope.cn/models

Exit the virtual environment: conda deactivate
Delete the virtual environment: conda env remove --name py3117
List of existing virtual environments: conda env list

Guess you like

Origin blog.csdn.net/yinshuilan/article/details/140095158