Table of contents
1 Introduction to Anaconda, Python, Jupyter and PyCharm
2 macOS installs Python, Jupyter and PyCharm through Anaconda
3 Use the terminal to create a virtual environment and install PyTorch
4 Install PyCharm and import Anaconda virtual environment
5 Anaconda and PyCharm installation under Windows operating system
7 Jupyter Notebook uses the environment managed by Anaconda
Learning artificial intelligence requires frequent use of Python language, Jupyter, and PyCharm tools
And deep learning frameworks like PyTorch, TensorFlow, and Keras.
This article introduces the installation tutorials under the macOS and windows operating systems.
1 Introduction to Anaconda, Python, Jupyter and PyCharm
2 macOS installs Python, Jupyter and PyCharm through Anaconda
3 Use the terminal to create a virtual environment and install PyTorch
4 Install PyCharm and import Anaconda virtual environment
5 Anaconda and PyCharm installation under Windows operating system
6 Create a TensorFlow virtual environment through Anaconda Navigator
7 Jupyter Notebook uses the environment managed by Anaconda
01
Introduction to Anaconda, Python, Jupyter and PyCharm
Python is an easy-to-learn yet powerful programming language . The elegant syntax and dynamic typing, as well as the interpreted nature of the language, make it an ideal language for scripting and rapid development of applications in many fields and on most platforms. The Python official website https://www.python.org/ can freely download and share third-party Python modules, programs, tools, etc., as well as additional documents.
Because Python is easy to use and open source, it has become the most widely used programming language in the field of artificial intelligence. Most of today's deep learning frameworks support the Python language.
PyCharm is a Python IDE (Integrated Development Environment, integrated development environment), with cross-platform properties. It has a set of tools that can help users improve their efficiency when developing in the Python language, such as debugging, syntax highlighting, project management, code jumping, smart prompts, auto-completion, unit testing, and version control. Other optional IDEs include Spyder and Visual Studio Code.
Jupyter Notebook is a web-based application for interactive computing. It can be applied to the whole process of computing: development, documentation, running code and displaying results. In short, Jupyter Notebook is opened in the form of a web page. You can directly write and run code on the web page, and the running result of the code will also be displayed directly under the code block. If you need to write an instruction document during the programming process, you can write it directly on the same page, which is convenient for timely explanation and explanation.
Because Python, Jupyter, and PyCharm can all be installed through Anaconda, we use Anaconda to install these three software.
Anaconda is a package manager ( conda package manager can help you install and manage these packages on your computer, including installing, uninstalling and updating packages)
and environment manager (for example, use Python 2 in project A, and use Python 3 in project B, and installing two Python versions at the same time may cause a lot of confusion and errors. At this time, conda can create different operating environments for different projects ).
Anaconda is available for multiple platforms (Windows, Mac OS and Linux), and the installer and installation instructions can be found on the official website https://www.anaconda.com/products/distribution#Downloads. According to whether the operating system is 32-bit or 64-bit, and the corresponding Python version, choose to download the corresponding installation file.
If you encounter network download problems, you can also choose the domestic mirror to download
Tsinghua mirror address: https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
Installing Anaconda is equivalent to completing the installation of Python, Jupyter and various Python packages, and also includes its core management tool conda. In use, you can use conda to create a virtual environment, and install different Python packages in the virtual environment, and these virtual environments can be used directly in PyCharm.
Below we introduce the installation of Anaconda under the m acOS and windows operating systems .
02
macOS installs Python, Jupyter and PyCharm through Anaconda
(1) Enter the official website to download Anaconda: https://www.anaconda.com/products/individual#macos, click Download to download.
Official website to download Anaconda
(2) Open the downloaded installation program, and choose to continue. There are multiple continuations during the installation process, just keep clicking.
Anaconda starts to install
Anaconda installation process
(3) Click Install, no need to modify the installation location.
No need to change the installation location
The installation process takes a few minutes.
Wait for about three to five minutes to install
(4) Click Close to complete the installation of Anaconda. At this time, Python and Jupyter Notebook are also installed synchronously.
Anaconda installed successfully
(5) After the installation is complete, open Anaconda Navigator on the launchpad
Open Anaconda Navigator in the launch pad
The interface after Anaconda Navigator is opened is as follows:
Anaconda Navigator Home main interface
(6) When the Anaconda installation is complete, all packages are linked in a virtual environment called "base" by default, which can be viewed in the Anaconda Navigator.
Anaconda Navigator Environment
Of course, you can also view it on the terminal through the command line. The command is:
conda list
The default virtual environment currently active is base, as shown in the figure below:
The default virtual environment is base
04
Use the terminal to create a virtual environment and install PyTorch
PyTorch was open sourced by Facebook Artificial Intelligence Research (FAIR) in January 2017. The predecessor of PyTorch is Torch, and a lot of content has been rewritten in Python. It is not only more flexible, supports dynamic graphs, but also provides a Python interface.
PyTorch is an open source Python machine learning library based on Torch for applications such as natural language processing . It is a Python-based sustainable computing package that provides two advanced features:
Tensor computation (like NumPy) with powerful GPU acceleration;
Deep neural networks with automatic differentiation system.
PyTorch is more conducive to rapid prototyping for researchers, enthusiasts, small-scale projects, etc.
And TensorFlow is more suitable for large-scale deployment, especially when cross-platform and embedded deployment is required.
The installation of PyTorch is the same under macOS and Windows. To create a virtual environment, you can use the terminal or Anaconda Navigator. Here we will first look at how to install the PyTorch framework through the terminal. The next part will demonstrate the installation of the TensorFlow framework through the Anaconda Navigator graphical interface.
(1) Open the terminal (under Mac, click the launcher to search for the terminal to open, under Windows, use the shortcut key win+R, enter cmd and confirm), create a new virtual environment PyTorchEnv (the name can be chosen at will, it is best to remember and distinguish), command yes:
conda create -n PyTorchEnv python=3.9.12
To create a virtual environment in the terminal, you need to specify the environment name and Python version
Ask if, choose Y for both
(2) If you need to delete the virtual environment PyTorchEnv, the command is:
conda remove -n PyTorchEnv --all
To view all virtual environments, the command is:
conda info -e
The asterisk indicates which environment is currently active
To make the virtual environment PyTorchEnv active, the command is:
conda activate PyTorchEnv
To change the currently active virtual environment to an inactive state, the command is:
conda deactivate
View and switch active virtual environments
(3) Install the PyTorch framework in the virtual environment PyTorchEnv , the command is:
conda install pytorch torchvision -c pytorch
Install the PyTorch framework https://pytorch.org/get-started/locally/#mac-anaconda
Check in the terminal to see if the installation is successful, the command is:
python
import torch
If the >>> sign appears, it means the installation is successful. If an error is reported, you can try to close the terminal and re-enter the environment
Verify that PyTorch was successfully installed
So far, the creation of a virtual environment through the terminal has been completed, and the PyTorch framework has been installed in the virtual environment.
At this time, you can already see the newly created virtual environment PyTorchEnv in Anaconda's Navigator.
You can see the newly created environment in Anaconda Navigator
04
Install PyCharm and import Anaconda virtual environment
(1) Next, install PyCharm, open the official website https://www.jetbrains.com/pycharm/, click to download the .dmg file, the Community version is free, and the Professional version can be tried for 30 days.
Download PyCharm from the official website
(2) Open the downloaded .dmg installation file of PyCharm, drag it into the application, wait for a while, and the installation is complete.
Install PyCharm on macOS
(3) There are two ways to open PyCharm, one is to open from the launchpad, and the other is to open from Ananconda Navigator:
Open PyCharm from launchpad
After PyCharm is installed, it will be automatically associated with Anaconda, and the installation order of PyCharm and Anaconda does not matter.
Open PyCharm from Ananconda Navigator
(3) Create a new Project in PyCharm and use the virtual environment:
New Project
Note that in the compiler selection, check the Existing interpreter or Previously configured interpreter, and then click the three points behind the Interpreter column:
What is an interpreter: each project can configure the interpreter separately, select the python environment
The Python interpreter is Python.exe, which is used to explain and run the Python code you wrote. The Python we downloaded (whether it is version 2 or version 3) actually has its own interpreter and compiler, which can be directly typed on the command line Enter code, or write a text, and then call the Python interpreter to execute , and Pycharm is an IDE (mainly to make it easier for us to write programs, or to look simpler, without using text or in the dos window Write code) , but Pycharm does not have a Python interpreter, so you need to install Python before installing Pycharm.
Select Interpreter Interpreter
Select the Conda Environment column, and click in the Conda executable column to select the external environment to import. The environment to be selected is the virtual environment , usually in the path.../ananconda3/envs/PyTorchEnv/bin/python:
Select the conda environment
In this way, the compilation environment of PyCharm is successfully configured as the virtual environment just created, and then a test program can be written to verify whether it is successful. Create a new Python file:
Create a new test file
The test code in the test file is as follows. If torch cannot be found, you can try to restart Anaconda Navigator and PyCharm:
import torch
x = torch.rand(3,3)
print(x)
Test success
05
Anaconda and PyCharm installation under Windows operating system
(1) Enter the official website to download Anaconda: https://www.anaconda.com/products/individual, click Download to download.
(2) Open the installation program, click Next -> I Agree -> Next, it is best not to change the path and then click Next -> Install, first click the web link, and then click Next to complete the installation of Anaconda, and Python and Jupyter Notebook are also installed The installation is complete. The operation diagram is as follows:
(3) You also need to add environment variables under Windows, right-click This Computer -> Properties, select the advanced system settings on the right, click Environment Variables -> path -> Edit -> New. Under the previously installed Anaconda path, find the anaconda3 folder -> anaconda3/scripts file -> anaconda3/Library/bin folder, copy the path, and add it to the new one.
Remarks: Press win+R, cmd, enter Python -V, the following figure will appear, if no error is reported, the addition is successful.
(4) Download and install PyCharm from the official website.
(5) Open the downloaded installer, click Next, then set the installation location, select 64-bit -> next -> install, click Finish, and the installation is complete. The operation diagram is as follows:
06
Create a TensorFlow virtual environment through Anaconda Navigator
TensorFlow is an open source, Python-based deep learning framework developed by Google. It has rich applications in scenarios such as image classification, audio processing, recommendation systems, and natural language processing. It is currently the most popular machine learning framework.
(1) Anaconda creates a TensortFlow environment (the name can still be arbitrary, but it should be easy to remember). After the creation is completed, the environment will automatically create a TensortFlow folder in /Users/hk/opt/anaconda3/envs, which contains this All third-party packages for the environment.
Create a TensortFlow environment
(2) Enter the created virtual environment, click the arrow, and select Open Terminal. The current environment is activated instead of the default base.
Select Enter Environment from Anaconda Navigator
The activation environment is TensorFlowEnv
(3) Install the TensorFlow framework
pip install tensorflow
Install the TensorFlow framework, this process will take a few minutes
(4) Verify that the installation is successful, and enter the Python programming environment from the terminal to see if the framework can be successfully imported.
python
import tensorflow as tf
07
Jupyter Notebook uses the environment managed by Anaconda
(1) There are two ways to open Jupyter Notebook:
The first method: Open Anaconda Navigator and click launch.
Open Jupyter Notebook from Anaconda Navigator
The second method: open the "terminal" and directly enter jupyter notebook to open it.
Open Jupyter Notebook from Terminal
Jump to the Jupyter interface on the web page
(2) Create and run the first Jupyter program
new program
# 安装 TensorFlow
import tensorflow as tf
# 载入并准备好 MNIST 数据集
mnist = tf.keras.datasets.mnist
# 将样本从整数转换为浮点数
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# 将模型的各层堆叠起来,以搭建 tf.keras.Sequential 模型
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
# 训练选择优化器和损失函数
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# 训练并验证模型
model.fit(x_train, y_train, epochs=5)
# 照片分类器的准确度已经达到 98%
model.evaluate(x_test, y_test, verbose=2)
You can switch whether the current cell is code or Markdown format
(3) After modifying the file name and saving the document, and then returning to the main interface, you can see that there is an additional TFdemo.ipynb file in the file tab; there is an additional running document in the running tab.
Files tab
run tab
So far, we have demonstrated how to install Anaconda and PyCharm under macOS and windows operating systems, how to use Anaconda to create and manage virtual environments, and how to use these virtual environments in PyCharm and Jupyter.