Introduction to Python
Python is a concise yet powerful object-oriented programming language that is widely used in system construction, application development, and especially in deep learning and data analysis. Python is also an important scripting language, and many components of operating systems and applications rely on Python for integration.
Component Support Status
Python has two versions, Python 2 and Python 3, but their syntaxes are incompatible with each other. Official maintenance for Python 2 ended on January 1, 2020, so TencentOS Server supports only Python 3.
Check the Python version:
Output example:
Python 3 is preinstalled in standard system images, so you can run the above command directly to check the version. If Python 3 is not preinstalled in the image, the system prompts python3: command not found when you run the command. In this case, run the following command to install Python 3:
After installation, run the following command to verify:
Output example:
Brief Introduction to Usage
This chapter introduces common ways to use Python 3 by demonstrating three methods: directly calling Python 3 modules, using the interactive mode, and running .py scripts.
Direct Module Invocation
You can build a simple web service by calling the built-in http module of Python 3. Go to a directory and run the following command:
python3 -m http.server 80
Open a browser and visit http://127.0.0.1 to see a Web service with file listing enabled. If a standard index.html file exists in the directory, the rendered webpage is displayed after you visit the URL.
If port 80 is prohibited in your network environment, you can use another port number instead. If the port parameter is omitted, port 8000 is used by default.
Python Interactive Mode
The Python 3 interactive mode is a shell-like terminal interface where you can execute Python 3 code.
Run the following command:
The following interface is returned, indicating that you have successfully entered the interactive mode.
Python 3.11.6 on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
You can try to obtain the Python 3 version in the interactive mode:
Output example:
Python 3.11.6 [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys,platform
>>> print("python" + platform.python_version() + ", for " + sys.platform)
python3.11.6, for linux
>>> exit()
The interactive mode executes code line by line, meaning each line is executed immediately after it is entered. It is generally used for verification and learning. To execute multiple lines of code at once, run a .py file. Executing .py Files
A file with the .py extension is the default script format for Python. The following uses hello.py as an example to demonstrate how to run it:
from requests_html import HTMLSession
sess = HTMLSession()
url = "https://www.qq.com"
headers = {
"User-Agent": "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"
}
def get_qq_top_news(url):
web = sess.get(url)
sel = "li.news-top > a"
r = web.html.find(sel, first=True)
return r.text
print(get_qq_top_news(url))
The steps are as follows:
pip3 install requests-html
python3 ./hello.py
Package Management
Python's package management system is called PyPI, commonly known as "pip". It can be used to install, uninstall, upgrade, downgrade, and package Python software packages (online/local). Only two features are demonstrated here: installing software packages online and packaging.
Installing Software Packages Online
Run the following command.
Packaging
The format of a Python software package is .whl, which is essentially a zip archive. The following demonstrates the packaging process for a software package written in the setup.py format:
dnf install python3-setuptools
git clone https://github.com/navdeep-G/setup.py.git
cd setup.py
python3 setup.py bdist_wheel
After the execution is completed, you can see a file in the .whl format in the dist subdirectory under the source code directory.