Home  >  Article  >  Operation and Maintenance  >  Basic configuration guide for system scripting using Python on Linux systems

Basic configuration guide for system scripting using Python on Linux systems

PHPz
PHPzOriginal
2023-07-04 11:01:401972browse

Basic configuration guide for using Python for system script programming on Linux systems

Before proceeding with system script programming, we need to perform some basic configurations on the Linux system to ensure that the Python operating environment and libraries are Support normal. This article will introduce how to configure and use Python for system scripting on Linux systems and provide some basic code examples.

  1. Installing Python

First, we need to confirm whether Python is installed. You can check the Python version by running the following command in the terminal:

python --version

If the Python version information is displayed, then Python has been installed. If it is not installed, please use the following command to install it:

sudo apt-get install python3
  1. Install pip

pip is a Python package manager that can be used to install and manage various Python libraries. On Linux systems, we need to install pip manually. You can use the following command to install pip:

sudo apt-get install python3-pip

After the installation is complete, you can verify whether pip is installed successfully by running the following command:

pip3 --version

If the version information of pip is displayed, the installation is successful.

  1. Configuring Python virtual environment

In order to isolate dependencies between projects and ensure the consistency of projects in different environments, we can use Python virtual environments. A virtual environment can isolate Python projects and related libraries to avoid conflicts between different projects.

First, install the virtual environment tool:

sudo apt-get install python3-venv

After the installation is complete, you can use the following command to create a new virtual environment:

python3 -m venv myenv

where, myenv is the name of the virtual environment , which can be modified according to your own needs. After the creation is completed, activate the virtual environment:

source myenv/bin/activate

After activating the virtual environment, the command prompt of the terminal will have the name of the virtual environment. Python libraries installed in a virtual environment will only take effect on the current virtual environment.

  1. Install commonly used Python libraries

In system script programming, some commonly used Python libraries are often used. The following introduces several commonly used Python libraries and installation methods:

  • requests: A library used to send HTTP requests and process responses.
pip install requests
  • psutil: Library for obtaining system information and process management.
pip install psutil
  • paramiko: Library for SSH remote connections and file transfers.
pip install paramiko

The above are some commonly used Python libraries. Other libraries can be installed in the virtual environment according to specific needs.

  1. System Script Programming Example

The following is a simple system script example written in Python to monitor the CPU and memory usage of the system:

import psutil

# 获取CPU使用率
cpu_percent = psutil.cpu_percent(interval=1)
print("CPU使用率:{}%".format(cpu_percent))

# 获取内存使用情况
memory = psutil.virtual_memory()
total_memory = round(memory.total / (1024 * 1024 * 1024), 2)
used_memory = round(memory.used / (1024 * 1024 * 1024), 2)
memory_percent = memory.percent
print("内存总量:{}GB".format(total_memory))
print("已使用内存:{}GB".format(used_memory))
print("内存使用率:{}%".format(memory_percent))

This script uses the psutil library to obtain the system's CPU usage and memory usage, and prints the results. The script can be modified and extended as needed.

Summary

This article introduces the basic configuration guide for using Python for system script programming on Linux systems, including installing Python and pip, configuring the Python virtual environment, and installing commonly used Python libraries. At the same time, this article also provides a simple system script programming example. I hope this article will be helpful to readers who are learning and using Python for system script programming.

The above is the detailed content of Basic configuration guide for system scripting using Python on Linux systems. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn