2. Install Python

This chapter explains how to install the Python environment on the Lubancat board.

重要

If it is not specially mentioned, this tutorial is based on Python 3.8.10 (mirror system is Ubuntu20.04) for experiments and explanations.

2.1. Install Python, PIP through APT

The LubanCat system supports the APT tool, and can be installed with the APT command directly. We use Python3 version directly:

# Execute the following commands on the board, you need to connect the network.
# You need to update the first time using APT.
sudo apt update

# Install Python3 (the default system has been installed to not execute)
sudo apt -y install python3

# Install PIP tools
sudo apt -y install python3-pip

提示

PIP tools are used to install other Python libraries, and you can choose whether to install according to your needs. For the development platform of the test, it is recommended to install it for easy use。

2.2. About python version

Lubancat Mirror is based on Linux distributions such as Debian and Ubuntu. These distribution versions are attached with Python by default. Generally, Python2*older versions and Python3*latest versions. You can use the following command to view the default installation version:

# View python3 version
cat@lubancat:~$ python3 --version

# The following is output
Python 3.8.10

# View python version
cat@lubancat:~$ python2 --version

#Output
Python 2.7.18

# View pip3 version
cat@lubancat:~$ pip3 --version

# The following is output
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)

提示

If you use the Debian10 mirror, the default is Python3.7.3 and Python2.7

2.3. Set the default version of Python and PIP

It can be noted that the Python3 and PIP3 in the above commands have the version number, mainly due to the historical reasons of Python, so that the use can be better distinguished from Python2. Python2 has stopped maintenance, and we don’t recommend that everyone continue to use it. The mirror that we provide is installed with Python2 and Python3 by default, and you can set the default version of the current system.

You can set the Python and PIP commands by default by the following commands: Python3:

#Set up soft links, Python uses Python3 by default
sudo ln -sf /usr/bin/python3 /usr/bin/python

#Set the soft link, PIP defaults to use PIP3
sudo ln -sf /usr/bin/pip3 /usr/bin/pip

After setting, you can directly use the Python or PIP command to check the current system Python version:

#View python
cat@lubancat:~$ which python
/usr/bin/python

# View python version
cat@lubancat:~$ python --version
# The following is output
Python 3.8.10

# View pip version
cat@lubancat:~$ pip --version
# The following is output
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)

In order to be more clear, the Python3 or PIP3 commands are still explained directly behind this book.

2.4. Software library installation method

2.4.1. pip download acceleration

When using Python later, you may need to use the PIP tool to download and install the dependent software package, and the official download station pypi <https://pypi.org/> _ The visit in China is not fast.

You can refer to the following webpage description Settings acceleration: pypi image use help

For example, use PIP installation software package adafruit-circuitpython-ssd1306:

# Use APT to install Python's Adafruit-Circuitpython-SSD1306 package
sudo pip3 install adafruit-circuitpython-ssd1306

# Use pip list to view the installed package
pip3 list | grep adafruit-circuitpython-ssd1306

# Uninstall the package with PIP UninStall
sudo pip3 uninstall adafruit-circuitpython-ssd1306

2.4.2. Use APT instead of PIP installation software package

When installing a software package with a PIP tool, it is usually compiled in this machine. The performance of some Lubancat board cards has caused a long compile time and may fail due to the lack of certain library files.

Therefore, when using a panel card with a low performance, we recommend searching for whether to use the APT tool to install. It will download the pre -compiled software package from the software library, and the installation time basically depends only on the network speed. You need to install the updated version or the package that you cannot find.

For example, the NUMPY data science library commonly used by Python is used as the following APT command installation, and it will be completed soon:

# Use APT to install python Numpy package
sudo apt -y install python3-numpy

As for the names of other specific software wrapped in the APT tools, you can search for content such as “APT installation XXX (such as Numpy)” in the network. If you are using the Debian system, find keywords such as “Numpy” in the software list library of Debian: Find the official software package of Debian

2.4.3. Install the software package with the SetupTools tool

In some cases, some Python libraries that our users need to use. For some reasons, we only got the source code of these library bags. Then we can’t install it with PIP tools or APT tools.

At this time, we can use the Python’s Setuptools tool to install the Python library package through the source code of the library.

SetupTools tool installation method is as follows:

# Enter the following command in the terminal:
sudo apt -y install python3-setuptools

Let’s use the SetupTools tool.

Taking the Jieba (stutter) library under Python as an example, the library can be used for Chinese words, which is a very popular open source Python project in Github.

Its github warehouse is:jieba

We can pull the source code from its warehouse or download its Releases compressed package and install it.

The source code directory is as follows:

broken

Enter the source code directory and use the Setuptools tool to install the library through the source code:

#Pull source code
git clone https://github.com/fxsjy/jieba.git

# Enter jieba/directory and enter the following commands in the terminal:
sudo python3 setup.py install

Wait for the installation of the software package to complete.

Enter the command below. You can see that the Jieba library can be used normally.

#Log in to the system terminal, enter python3 or ipython, enter the Python interactive mode, and then use the command:
import jieba
set_list = jieba.cut("Welcome to the Python Practice Tutorial", cut_all=False)
print("Default Mode:" + "/".join(set_list))
broken