Intel chip mac installs Anaconda, Jupyter, TensorFlow environment

Install Anaconda

Click here to download the installation package on the official website, as shown in the figure below:
Download the conda installation package
Click the installation package to install, pay attention to the installation path. nextModify environment variables, so that python is entered on the command line, and the python environment of anaconda is used by default. This eliminates the need to install packages that python frequently uses.

1. open ~/.bash_profile
2. 如图(忽视上面几行注释)
3. 刷新环境变量:source ~/.bash_profile

Please add a picture description

At this point, executing python in the terminal will use conda's python environment:
condaPython

Note:
Tutorial for adding environment variables on macOS

Opening Anaconda normally means that the installation is successful:
Open Anaconda normally

Add domestic mirror source to anaconda

1. 查看镜像源: conda config --show channels
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/
3. 再次查看镜像源: conda config --show channels

Please add a picture description

The method of PyCharm configuring Anaconda environment in macOS

Our purpose is to use Anaconda's own python and its rich third-party libraries in PyCharm, so this step is the most important.

Open it for the first time after successfully installing PyCharm, first choose to create a new project, and then select the environment in turn:
insert image description here
This step is the most critical and the most error-prone. When we see the content shown in the picture above, the first reaction is to click Conda Environmenr. This is not correct, and it is also a confusion that many people will encounter. The correct choice should be System Interpreter.

  1. Open python interpreter
    insert image description here
    2. After selecting system interpreter, click "...", select add, find python.app/Contents/MacOS/python under the anaconda installation path, and click ok. The configuration is now complete.
    insert image description here
    After creating a new project, as shown in the following figure:
    insert image description here

Configure the default working path of JupyterLab

Purpose: Create a folder by yourself to put the files in JupyterLab

Create a py file, command: jupyter notebook --generate-config
Please add a picture description
open this py file: open /Users/waldo/.jupyter/jupyter_notebook_config.py
find the corresponding location in the figure below, modify the default working directory of the notebook (remove the comment), save:
Please add a picture description
after reopening, it is empty ( in the default wpforJupyter folder ):
Please add a picture description

Note: Mac cannot install cuda, skip

Install tensorflow

Secure tensorflow in conda virtual environment

The virtual environment is equivalent to a sandbox, avoiding the mutual influence of different frameworks, so that multiple different versions of tensorflow can even be installed. It is also convenient to uninstall, just delete the virtual environment directly

1. 打开终端,输入如下命令,创建了一个名为“tf2_8”的虚拟环境: 
   conda create -n tf2_8 python=3.9
2. 执行命令,来激活tf2_8虚拟环境: conda activate tf2_8
3. 执行命令在虚拟环境中进行Tensorflow的安装: pip3 install tensorflow
4. 退出虚拟环境: conda deactivate

Use the command conda info -eto check whether the virtual environment "tf2_8" has been installed successfully:
insert image description here

Configuration of jupyter virtual environment

Open the jupyter notebook at this time, the execution import tensorflow as tfis unsuccessful, the following configuration is required

Open the system terminal and execute the following command:

1. conda activate tf2_8 //注意替换虚拟环境名

2. conda install ipykernel //安装ipykernel

3. python -m ipykernel install --name tf2_8 //在ipykernel中安装当前环境

4. conda deactivate

Open jupyter, switch kernel:
insert image description here
insert image description here
Execute the test code, don’t worry if there is an error, because GPU acceleration is not used:

//测试代码
import tensorflow as tf
import os
os.environ["CUDA_VISIBLE_DEVICES"]="0"
tf.compat.v1.disable_eager_execution()
hello=tf.constant('Hello,TensorFlow')
config=tf.compat.v1.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.9
sess=tf.compat.v1.Session(config=config)
print(sess.run(hello))

test code

conda command supplement

Common operation commands:

1. Environmental operation

1. View all command help for environment management:

conda env -h

2. View all conda virtual environments that have been created:

conda info -e

3. Create a virtual environment:
conda create env_name
//(env_name) is the name of the environment, this command will create a new environment for Biopython, located in /envs/ of the Anaconda installation file

Create a virtual environment with a specified python version:
conda create env_name python=3.9 (3.6 is the python version, change it according to your needs)

Create a virtual environment containing some packages:
conda create env_name numpy scipy

Create a virtual environment containing certain packages under the specified python version:
conda create env_name python=3.9 numpy scipy

Activate (enter) a virtual environment: the
new development environment will be installed by default in the envs file directory under the conda directory, you can specify a different path;
if you do not specify the version of python installed, conda will install the original conda installation The version of python installed.

macOS:
conda activate env_name

Exit a virtual environment:
conda deactivate

Copy a virtual environment:
conda create new_env_name old_env_name

Delete a virtual environment:
conda remove -n env_name --all

2. Package management
View installed packages:
conda list

View packages in the specified virtual environment:
conda list -n xxx

Find packages:
conda search xxx

Update package:
conda update xxx

Installation package:
conda install xxx
pip install xxx

Specified installation virtual environment:
conda install -n env_name xxx

Install all packages in the anaconda distribution:
conda install anaconda

Uninstall package:
conda remove xxx

3. Manage conda
to check the conda version:
conda --version

Upgrade the current version of conda:
conda update conda

View installed packages:
pip list or conda list

Install and update:
pip install requests
pip install requests --upgrade
or
conda install requests
conda update requests

Update all libraries
conda update --all

update conda self
conda update conda

update anaconda own
conda update anaconda

Guess you like

Origin blog.csdn.net/Waldocsdn/article/details/124326642