Detailed installation of anaconda, tensorflow, pytorch


Regardless of whether you have installed python before, this article is very suitable for helping you install anaconda and establish tensorflow and pytorch environments. The demo is performed using CPU, and I have given corresponding reminders and suggestions for the GPU version of the environment.

The reason for writing this blog is that I have installed python before, and I can't find the article I want on the Internet.

No one is more detailed than me (dog head), just kidding


This example version is: Anaconda3-2021.05-Windows-x86_64

1. Download anaconda

Download from the official website: Anaconda | Individual Edition

The official website downloads the latest version, and the official website speed is actually not slow.

If you want to download other versions or dislike the slow speed of the official website, please enter the Tsinghua mirror site to download

Index of /anaconda/archive/ | Tsinghua University Open Source Mirror | Tsinghua Open Source Mirror

Here is an example from the official website

image-20211101164938915

There is no need to log in and register , and the download option will pop up after waiting.

image-20211101165438349

2. Install anaconda

  1. Click next

image-20211101165807309

  1. Click on I Agree. The agreement is something, if you want to install it, you have to agree.

image-20211101165932235

  1. Choose to install for yourself or for all users on your computer.

Here, choose the default installation for yourself. Generally speaking, no matter which one you choose is the same, because there is only one user on the general computer.

image-20211101170107343

  1. Click Browse to select the installation location.

The path should not contain Chinese.

Need space 2.9G, I do not recommend you to use the default installation location (ie C drive).

I created a new folder anaconda3 on the D drive and selected this folder for installation.

After selecting, click next

image-20211101170813776

  1. Choose the appropriate option. After selecting, click install to install.

Details about this option are below. I actually chose the second one .

image-20211101171131338

First option :

Choose whether to add Anaconda to the PATH environment variable. The default is not to add it.

Not adding it means you can't use command line operations in cmd (command prompt) and powershell.

I don't recommend you to add it, because anaconda has its own Anaconda Prompt and Anaconda Powershell Prompt , which are the same as cmd and powershell, and there is no need to add it to the path.

Of course if you have other ideas, you can choose to add them .

Second option:

Choose whether to register Anaconda as your default Python, which is selected by default. (My default is python 3.8)

Basically, leave this option unchecked if you want to install and run multiple versions of Anaconda or multiple versions of Python.
If you have other ideas, say so.

If you have installed python before and use software such as vs and pycharm, check this option, and the python that comes with anaconda will become the default option for these software.

I originally installed python3.9, but I actually checked it here , because it did not affect my use of python3.9.

  1. Wait for the installation to complete.

image-20211101173705395

  1. Here you can choose to install the pycharm software, if you don't want to install it, just click next.

pycharm : A powerful IDE software for python.

Python and jupyter notebooks are a breeze with pycharm, which is designed to work with Anaconda

image-20211101173836279

  1. The installation is complete. You can choose to view the anaconda personal edition tutorial and how to get started with anaconda.

First option: Check out the anaconda personal edition tutorial

Second option: How to start anaconda

Both options will open the corresponding website for you

Once selected, click finish.

image-20211101174428102

  1. Verify that the installation was successful.

Open Anaconda Prompt in the start menu folder , enterconda info

You can also enter this command on cmd and powershell if you added environment variables when installing.

image-20211101191101581

3. Introduction to each part of anaconda

start menu folder

image-20211101175032569

Anaconda Navigator:

The official introduction to Anaconda Navigator: Anaconda Navigator is a desktop graphical user interface (GUI) included in the Anaconda distribution that allows you to launch applications and easily manage conda packages, environments, and channels without using command-line commands.

Anaconda Powershell Prompt and Anaconda Prompt:

Same as above, but in command line mode

Jupyter Notebook: This is used by a lot of people

Jupyter Notebook is a web-based application for interactive computing. It can be applied to the whole process of computation: development, documentation, running the code and presenting the results. In short, Jupyter Notebook is opened in the form of a web page, you can write and run code directly in the web page, and the running result of the code will also be displayed directly under the code block. If you need to write a description document in the programming process, you can write it directly in the same page, which is convenient for timely description and explanation.

Author: Raxxie
Link: https://www.jianshu.com/p/91365f343585

Spyder:

Baidu Encyclopedia: Spyder is a simple integrated development environment developed for it by the author of Python . Compared with other Python development environments, its biggest advantage is that it imitates the function of MATLAB 's "workspace", which can easily observe and modify the value of the array.

4. Common commands of anaconda

  • Current environment information:conda info

  • conda installation version information:conda --version

  • Update conda:conda update -n base -c defaults conda

  • Check the current installation environment:conda info --sendsorconda info -e

  • Check which versions of python are currently available for installation:conda search --full-name python

There are also some commands shown later.

5. Create a new virtual environment

Usually one environment is enough , but I still recommend creating a new one.
In some cases, you can do experiments in the new environment, and then operate in the default environment if there is no problem. If you encounter uncontrollable errors in the new environment, delete and rebuild one.

Or if you want to build a tesorflow and pytorch deep learning environment , then you must create a new environment.

The default environment is base (root), and the version of python is 3.8.8

Check if the installation was successful:conda --version
Check the current installation environment:conda info --sendsorconda info -e

In Anoconda, the installed python or environment is stored in the envs folder.

*The symbol indicates that it is in the current environment.
insert image description here

1. Use the previously installed python

I do not recommend that you use the previous python directly to set up the environment. Because you must have installed a lot of modules, the folder will be much larger.
Of course, if that's all you want for these modules, feel free.

In Anoconda, the installed python or environment is stored in the envs folder.

So, then copy the original python folder to the envs under the folder where anaconda is installed

My original python is installed in the Python3.9.4 folder of the D drive, and my anaconda is installed in the anaconda3 folder of the D drive

After copying into the envs folder, you can change the file name, I will not change it here

image-20211101194902189

Create an environment: (The existing python is used here, so the python version is not specified)

conda create --name Python3.9.4 -y// After adding -y, you don't need to enter y

-name or -n : Specify the environment name. here is the file name

Python3.9.4 : Environment name. here is the filename

If there is an update warning when you create, you can update conda

conda update -n base -c defaults conda

activation environment

Under windows:conda activate Python3.9.4// followed by the environment name

linux & mac:source activate Python3.9.4

The commands of linux and mac are uncertain. If it fails, you can only find the reason by yourself.

After the activation is successful, the first base (root) will become your new environment name.

enterpython --versionView version

enterconda info --sendsCheck the installed environment

The environment has been created in the figure
insert image description here

Returns the default environment base(root)

windows:conda deactivate
//You can add the environment name behind to specify which environment to exit. If you don't add it, you will exit the current environment.

It is not possible to use deactivate directly, because it will be deprecated.

linux & mac:source deactivate

The commands of linux and mac are uncertain. If it fails, you can only find the reason by yourself.

Remove the environment :

conda remove --name Python3.9.4 --all
//You must exit this environment before deleting
insert image description here

2. Using the new python

Create an environment:

conda create --name tensorflow python=3.9You need to enter y in the middle to agree to continue

--name or -n : Specify the environment name

tensorflow : environment name

python=3.9 : Specify the version of python to install, python=3.9 means to install the latest version in 3.9

You can also specify a specific version such as: python=3.9.4

activation environment

Under windows:conda activate tensorflow// followed by the environment name

linux & mac:source activate tensorflow

The commands for linux and mac are uncertain, and if it fails, you may need to find the answer yourself.

After the activation is successful, the first base (root) will become your new environment name.

enterpython --versionView version

enterconda info --sendsCheck the installed environment

The environment has been created in the picture
insert image description here

Returns the default environment base(root)

windows:conda deactivate//You can add the environment name behind to specify which environment to exit. If you don't add it, you will exit the current environment.

It is not possible to use deactivate directly, because it will be deprecated.

linux & mac:source deactivate

The commands of linux and mac are uncertain. If it fails, try adding conda before the source, or after the source.

Remove the environment :

conda remove --name tensorflow --all //You must exit this environment before deleting

I will not demonstrate it here, I have demonstrated it in the previous section.

6. Install tensorflow

Because I am an AMD graphics card, only cpu can be installed on tensorflow on windows, so I only demonstrate the cpu.

Reminders and suggestions for installing the GPU version :

If you are an NVIDIA (NVIDIA) graphics card and want to install a GPU , you must check the official website for the corresponding requirements for the GPU version.
Official website: https://www.tensorflow.org/install/pip#gpu

The problem with most people installing GPU lies in the version of cuda and cudnn.
In the online tutorials about the GPU version, almost no one has mentioned this, which means that you basically have to follow other people's tutorials to be successful .
insert image description here

This is only a partial screenshot

CPU version installation demo :

First activate the previously created environment tensorflow
windows:conda activate tensorflow

Update pip:

pip install --upgrade pip
  • 1

Finally install the cpu version of tensorflow
insert image description here

This is only a partial screenshot.

According to the official website: https://www.tensorflow.org/install/pip#cpu
The cpu version has no requirements . The only requirement is the python version, which is not really a requirement. You only need to have the tensorflow corresponding to the python version. Yes .
When I wrote this blog, there was no corresponding tensorflow for python3.10.

It is strongly recommended to use the method of switching sources . The order is not arranged here, because my classmates always fail to use the method of changing the source, so I used the method without changing the source at the beginning to be safe, but I tried changing the source myself later, no problem, and saving It took a lot of time, half an hour without changing the source, and 2 minutes after changing the source.

1. The method of not changing the source.

It is highly not recommended to use this method at night, and the speed may be slow at night.
The internet speed during the day is still ok, but not stable.

pip install - - ignore - installed - - upgrade tensorflow #This 
 will automatically install the latest tensorflow corresponding to your python version. The premise is relevant. 
# --ignore-installed Ignore installed packages, overwrite them. If you have not installed tensorflow, you can remove this option.
  • 1
  • 2
  • 3

insert image description here
This is how I successfully installed it. These are all tensorflow related dependencies.
insert image description here
Verify success

enterpythonEnter the python command line and
enterimport tensorflow as tfIntroduce tensorflow module (as role: rename)
insert image description here
If you are still not at ease, then verify again

View version and path
tf.__version__as well astf.__path__
insert image description here

2. The method of changing the source .

Still activate the environment, update pip,
I will use the new environment python37 for demonstration

First activate the environment
windows:conda activate python37

Update pip:

pip install --upgrade pip
  • 1

Install tensorflow

The Ali source given by many people on the Internet is http, but in fact it is no longer available, and http must be changed to https .

# Aliyuan 
pip install - i https : // mirrors . aliyun . com / pypi / simple /  - - upgrade tensorflow
 # This will automatically install the latest tensorflow corresponding to your python version. The premise is relevant.
  • 1
  • 2
  • 3
#清华pip 
install -i https://pypi.tuna.tsinghua.edu.cn/simple--upgrade tensorflow # This will automatically install the latest
 tensorflow corresponding to your python version . The premise is relevant. #--ignore-installed Ignore installed packages, overwrite them. #If you have not installed tensorflow, you can choose to remove the -ignore-installed option


  • 1
  • 2
  • 3
  • 4
  • 5

Aliyuan is used here.
insert image description here
After I installed it successfully.
insert image description here
The verification method is the same as before.

7. Install pytorch

The GPU limit of pytorch is not so much. Just pay attention to the supported cuda version. The cuda10.2 in the figure below will not be supported.

Build the environment:conda create -n pytorch python=3.9
Activation environment under windows:conda activate pytorch

Install pytorch
to automatically generate commands according to the official website
Official website: https://pytorch.org/get-started/locally/
insert image description here
The command I generated here is shown below,This is no source. This method is not recommended

conda install pytorch torchvision torchaudio cpuonly -c pytorch
  • 1

-c pytorch : specifies the source of pytorch itself, which is very slow.

So, we can change to domestic source
such as Tsinghua source
https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

Choose the appropriate system, I choose win-64
insert image description here
So, the Tsinghua source I use is

https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/win-64/
  • 1

So, the command after changing the source

conda install pytorch torchvision torchaudio cpuonly -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/win-64/ # will install pytorch corresponding to your environment python version _ _ _ _ _ _ _ _

  • 1
  • 2

If there is such a problem as in the picture below. Enter the prompted directory to delete the target file and execute the command again.

You should not have this problem . This problem should be the result of the forced termination when I installed it using the non-source-changing method.

insert image description here
Verify installation

enterpythonEnter the python command line and
enterimport torchIntroduce torch module
inputprint(torch.__version__)output pytorch version
insert image description here

It's almost here, and the flowers are finished. Destined to add again.

If it's helpful, don't ask for three consecutive clicks, but it's okay to like it, refill

Tags: Detailed installation of anaconda, tensorflow, pytorch

python artificial intelligence python anaconda tensorflow pytorch

Related: Detailed installation of anaconda, tensorflow, pytorch