In the learning and development of Python, the first step in the work is to prepare the development environment, including the relevant commonly used plug-ins, as well as some auxiliary tools, so that we in the subsequent development work, in order to do more with less. Here are some of the preparation of the Python development environment, as well as some commonly used libraries and modules to install and use the experience summarized for your reference.
1、Development of VScode installation and related preparation
There are several steps that are key in preparing your Python development environment. The following is a detailed guide covering the preparation of the development environment as well as the installation of some common plugins:
1) Install VS Code
VS Code: This is a lightweight but powerful code editor with rich extension support. You can start fromVisual Studio Code Official Website Download. Open the official website / and download the package. Or you can use another one such asPyCharm,can be accessed fromJetBrains Official Website Download.
Python AI programming assistant: Fitten Code:
It is an AI programming assistant driven by non-top 10 models, which can automatically generate code to improve development efficiency, help you debug bugs to save your time, in addition to conversational chat to solve the problems you encountered in programming.
Fitten Code is an AI programming assistant driven by non-top 10 models, which can automatically generate code, improve development efficiency, help you debug bugs, and save your time. It also allows you to chat and solve your programming problems. It's free and supports more than 80 languages: Python, C++, Javascript, Typescript, Java, and so on.
Highly recommendedThe auto-completion code feature saves a lot of time in manual code typing and reduces errors.
2) Install VS Code Python Extension
Install the Python extension in VSCode, search for Python in the Extension Marketplace and install it.
3) Install Python
First, make sure you have Python installed. you can get it from thePython Official Site Download the latest version of the installer and install it.
Installing Python on Window Platforms: /downloads/windows/
Installing Python on the Mac platform: /downloads/mac-osx/
4) Configure Python environment variables
Open the system environment variable and add the Python directory to the PATH variable so that you can use Python directly from the command line.
5) Testing the Python Environment
Type python at the command line. If the Python interpreter version information appears, the Python environment is successfully configured.
6) Install pip
Open the command line and type pip install --upgrade pip to upgrade pip to the latest version.
7) Install virtualenv
Open the command line and type pip install virtualenv to install virtualenv.
2, Python some common library module installation
Python development commonly used library module is very much to see you focus on that aspect, basically when listed in a long list, I focus on conventional back-end Web API development for some of the key recommendations for reference and learning.
1) requests
requests
is an easy-to-use Python library at /psf/requests for sending HTTP requests. It is designed to make interacting with Web services easier and more user-friendly.requests
is based on urllib3 on a wrapper layer , provides a concise API to handle common HTTP request operations , such as GET, POST, PUT, DELETE and so on.
requests
Main characteristics of the
-
Simple API: compared to the native
urllib
,requests
Provides a more intuitive and easier to understand interface. -
Automatic processing of codes:
requests
Automatic processing of response content encoding and automatic decodinggzip
cap (a poem)deflate
Compression. -
Support for holding sessions: By
Session
Object.requests
It is possible to maintain a session between multiple requests and handle cookies. -
Simplified error handling:
requests
The error handling process is simplified by throwing an exception based on the HTTP response status code. - Rich functionality: Support for HTTP authentication, proxies, SSL certificate validation, file uploads, multi-part encoded forms, session objects, cookie persistence, connection pooling, and more.
If you need to consider asynchronous processing, you can use theaiohttp :aiohttp
is an asynchronous HTTP client and server framework that uses Python'sasyncio
library to handle large numbers of concurrent requests.aiohttp
Ideal for applications that require high-performance network communications, such as Web services, WebSocket, and real-time data processing.
2) Uvicorn
Uvicorn
is a high-performance, lightweight Python web server based on ASGI (Asynchronous Server Gateway Interface), designed for running asynchronous web frameworks (such as FastAPI, Starlette). It takes advantage of Python's asynchronous capabilities to handle a large number of concurrent connections, making it suitable for building modern asynchronous web applications.
Uvicorn
Main characteristics of the
-
high performance: Use
uvloop
cap (a poem)httptools
Provides extremely high performance and is suitable for use in high concurrency scenarios. -
asynchronous support: supports an asynchronous programming model that works with Python's
asyncio
cap (a poem)trio
Seamless integration. - ASGI Compatible: Fully compliant with the ASGI standard for modern asynchronous web frameworks such as FastAPI and Starlette.
-
WebSocket Support:: Through ASGI.
Uvicorn
Native support for the WebSocket protocol. -
Flexible deployment: can be used either as a standalone development server or in conjunction with the
Gunicorn
and other WSGI servers to deploy production environments.
Uvicorn
Typically used to run FastAPI or Starlette applications. Below is a simple FastAPI application and uses theUvicorn
Running:
from fastapi import FastAPI app = FastAPI() @("/") async def read_root(): return {"Hello": "World"} if __name__ == "__main__": import uvicorn (app, host="0.0.0.0", port=8000)
3)FastAPI
FastAPI
is a modern, fast (and high-performance) web framework for building APIs that is based on Python 3.7+ type hints and relies on theStarlette
(for web servers and routing) andPydantic
(for data validation and serialization).FastAPI
is designed to provide a similar development experience to Flask and Django, but with greater improvements in performance, type safety, and developer friendliness. GitHub address: /fastapi/fastapi
Key Features of FastAPI
-
Extremely high performance: Asynchronous support, based on ASGI, allows the
FastAPI
It is close to the performance level of Go and is suitable for handling high concurrency. - Automatic generation of API documentation: Automatically generate interactive API documentation (e.g. Swagger UI and ReDoc) using OpenAPI and JSON Schema.
-
Automatic validation based on type hints: Take advantage of Python's type hints and
Pydantic
, automatic data validation and parsing. -
asynchronous support: Native support
async
cap (a poem)await
It can handle asynchronous tasks and is suitable for interacting with databases, third-party APIs, WebSockets, and so on. - Built-in dependency injection system: makes the declaration and management of dependencies simple and powerful, facilitating modular design.
- Developer Friendly: Provides detailed error information and documentation, supports auto-completion, and greatly improves development efficiency.
Here is a simpleFastAPI
Applications:
from fastapi import FastAPI app = FastAPI() @("/") async def read_root(): return {"message": "Hello, World"} @("/items/{item_id}") async def read_item(item_id: int, q: str = None): return {"item_id": item_id, "q": q}
When you run the FastAPI application, it automatically generates interactive documentation:
-
Swagger UI:: Access
http://127.0.0.1:8000/docs
-
ReDoc:: Access
http://127.0.0.1:8000/redoc
These two documentation interfaces allow you to view the structure of the API and even make API calls directly from the interface. As I described in my previous essay, theParameter design for FastAPI projects in Python using Annotated》。
FastAPI is a very modern and efficient framework that is ideal for building high-performance APIs, with features such as automatic document generation, data validation, and dependency injection that enable developers to write code faster, more securely, and provide a great user experience.
Parameter design for FastAPI projects, these you can find in thepath manipulation functionparameter or using theAnnotated
special function used in the dependency function of the request to get data from the request.
We have introduced a configuration file that allows us to unify the management of service startup parameters in FastAPI, as shown in the following code.
if __name__ == "__main__": import uvicorn # log_level: 'critical', 'error', 'warning', 'info', 'debug', 'trace'. Default: 'info'. ( app, host=settings.SERVER_IP, port=settings.SERVER_PORT, log_config="app/uvicorn_config.json", # Log Configuration # log_level="info", # log level )
3) pymysql, pymssql, and SQLAlchemy
Involving back-end processing, certainly not bypass the database processing operations, such as for MySQL, MS SqlServer and other database processing and packaging.
PyMySQL
is a pure Python implementation of the MySQL client library for connecting to MySQL databases and executing SQL queries. It is the PythonMySQLdb
library, especially for projects that are using Python 3 and don't want to rely on C extensions.PyMySQL
Supports all major features of the MySQL database, including transactions, stored procedures, connection pooling, and more.
PyMySQL
Main characteristics of the
- Pure Python implementation: No dependency on C extensions, easy to install and use across platforms.
-
good compatibility: with
MySQLdb
The interfaces are very similar, making it easy to get the most out of theMySQLdb
migrate toPyMySQL
。 - Supports all major MySQL features:: Includes transactions, stored procedures, BLOB data types, and more.
- simple and easy to use: Provides intuitive APIs for database connection, query, insert, update and delete operations.
mountingPyMySQL
You can do this bypip
to installPyMySQL
:
pip install pymysql
utilizationPyMySQL
Connect to the MySQL database:
import pymysql connection = ( host='localhost', user='your_username', password='your_password', database='your_database' ) try: with () as cursor: # Executing SQL Queries ("SELECT VERSION()") # Getting Query Results result = () print(f"MySQL version: {result}") finally: ()
Below is some example code for the operation of my actual table.
sql = "select * from t_customer where name = '{0}' LIMIT 1 ".format(name) print(sql) (sql) myresult = () # fetchone() fetches a record if myresult: print("This name already exists, please change it.") else: print("The name is available.") # Insert Record Statement sql = "INSERT INTO `t_customer` (`ID`, `Name`, `Age`, `Creator`, `CreateTime`) VALUES (%s, %s, %s, %s, %s);" val = (id, name, age, creator, createtime) (sql, val) () # This statement must be used if there is an update to the contents of the data table. print(, " Row record insertion.") sql = "update t_customer Set age = %s where name =%s " val = (26, name) (sql, val) () # This statement must be used if there is an update to the contents of the data table. print(, " A record has been modified.") sql = "select * from t_customer where name = '{0}' LIMIT 1 ".format(name) (sql) myresult = () # fetchone() fetches a record if myresult: print("Modified record.", myresult) sql = "SELECT * FROM t_customer" (sql) print("t_customer Result set.") for x in cursor: print(x) sql = "delete from t_customer where name =%s " try: (sql, (name,)) () # This statement must be used if there is an update to the contents of the data table. print(, " Row records are deleted.") except: () # Rollback on Error print("Failed to delete record!") sql = "SELECT * FROM t_customer" (sql) myresult = () # fetchall() Get all records for x in myresult: print(x) # Close the database connection ()
The output is displayed as shown below.
pymssql
is a Python library for connecting to Microsoft SQL Server databases based on theFreeTDS
A lightweight database interface implemented to simplify the interaction between Python and SQL Server.pymssql
Provides support for T-SQL statements and can perform tasks such as executing stored procedures and handling large data inserts.
pymssql
Main characteristics of the
- Lightweight and easy to use: Provides a simple API interface that is easy to get started with quickly.
- Compatible with SQL Server: Microsoft SQL Server 2005 and above is supported.
- Cross-platform support: Supported on Windows, Linux and macOS systems.
-
Integrated Transaction Management:: Adoption
commit
cap (a poem)rollback
method for transaction management. - Stored Procedure Support:: Ability to execute and process stored procedures for complex database operations.
-
Batch insertion support:: Adoption
executemany
method to efficiently insert large amounts of data.
mountingpymssql
You can do this bypip
mountingpymssql
:
pip install pymssql
utilizationpymssql
to connect to a SQL Server database.pymssql
Transaction support allows you to use transaction control when performing multiple operations to ensure data consistency:
import pymssql # Connect to the database conn = ( server="localhost", user="sa", password="123456", database="Winframework", tds_version="7.0", ) # Create a cursor object cursor = () # Execute a query ("SELECT * FROM T_Customer") # Fetch all the rows rows = () # Print the rows for row in rows: print(row) # Close the cursor and connection () ()
SQLAlchemy
Allows developers to interact with databases through Python code without having to write direct SQL statements, and also supports complex queries directly using native SQL.Key Features of SQLAlchemy
- Object-Relational Mapping (ORM): Allows mapping Python classes to database tables and automatically handles SQL generation and execution.
- SQL Expression Language: Provides an expression language layer that allows the construction and execution of native SQL queries while preserving type safety and cross-database compatibility.
- Database Abstraction Layer: Provides cross-database compatibility, making it relatively easy to switch between databases.
- high performance:: Optimized performance of database access through fine-grained control and caching mechanisms.
- transaction management:: Support for complex transaction processing and context management, making database operations more secure and consistent.
- Support for multiple databases: Support for most major relational databases, such as SQLite, PostgreSQL, MySQL, Oracle, SQL Server, and so on.
Installing SQLAlchemy
You can do this bypip
mountingSQLAlchemy
:
pip install sqlalchemy
If you want to connect to a specific database, you also need to install the appropriate database driver. For example, to connect to a MySQL database, you also need to install thepymysql
maybemysqlclient
:
utilizationSQLAlchemy operates on the database.
It can unify the operation processing of multiple databases, such as SQLITE, SqlServer, MySQL, PostgreSQL and so on.
utilizationSQLAlchemy
Create a connection to the database:
# mysql database engine engine = create_engine( "mysql+pymysql://root:[email protected]:3306/WinFramework", pool_recycle=3600, # echo=True, ) # Sqlite Database Engine # engine = create_engine("sqlite:///testdir//") # PostgreSQL Database Engine # engine = create_engine( # "postgresql+psycopg2://postgres:123456@localhost:5432/winframework", # # echo=True, # ) # engine = create_engine( # "mssql+pymssql://sa:123456@localhost/WinFramework?tds_version=7.0", # # echo=True, # )
Since it corresponds to ORM processing, a class object needs to be defined to associate with the database table, as shown below.
from sqlalchemy import create_engine, Column, Integer, String, DateTime, TIMESTAMP from import declarative_base from import sessionmaker # Create a base class to define the structure of the database tables Base = declarative_base() # Define a model for the Customer database table class Customer(Base): __tablename__ = "t_customer" id = Column(String, primary_key=True, comment="primary key") name = Column(String, comment="name and surname") age = Column(Integer, comment="(a person's) age") creator = Column(String, comment="founder") createtime = Column(DateTime, comment="Creation time")
The example code for CRUD operation is shown below.
# Create a session Session = sessionmaker(bind=engine) session = Session() id = str(guid.uuid4()) # create a new customer customer = Customer( id=id, name="Alice", age=25, creator="admin", createtime=("2021-01-01 12:00:00", "%Y-%m-%d %H:%M:%S"), ) # add the customer to the session (customer) # commit the changes to the database () # query the customer from the session for item in (select(Customer)): print(, , , , ) print("\r\nquery all customers") customers = (Customer).all() for customer in customers: print(, ) print("\r\nquery all customers by condition:age > 20") customers = (Customer).filter( > 20).limit(30).offset(1).all() for customer in customers: print(, ) print("\r\nquery customer by id") customer = (Customer).filter( == id).first() if customer: print(, ) print("\r\n Complex queries") customers = ( (Customer) .filter( or_( and_( > 20, < 30), .in_(["Alice", "Ng Wah Chung (1974-), * actor"]), ) ) .all() ) for customer in customers: print(, ) print("\r\nselect customer by id") stmt = select(Customer).where( == id) result = (stmt) print(result) stmt = select(Customer).where( == "Ng Wah Chung (1974-), * actor") result = (stmt).scalar() if result: print("Customer exists in the database") print(, , ) else: print("Customer does not exist in the database") print("\r\nselect customer In") # query the customer from the session stmt = select(Customer).where(.in_(["Alice", "Ng Wah Chung (1974-), * actor"])) for item in (stmt): print(, , , , ) print('\r\ndelete all customers by name = "Alice"') # delete the customer from the database delete_stmt = delete(Customer).where( == "Alice") result = (delete_stmt) print(str() + " rows deleted") () # close the session ()
Due to space constraints, we have introduced some of the temporary, in fact, even if it is to do the back-end WebAPI processing, we also need to understand a lot of different class libraries, Python class libraries are very rich, and synchronous, asynchronous, and there are different differences in the class libraries, so we can according to the actual need to choose a different class libraries to achieve the purpose of our framework.
For example, for FastAPI data validation, we generally introduce pydantic, which allows for a variety of rich checksums to be performed on the data, similar to strong typing and checksums for various rules.
class Person(BaseModel): name: str age: int @field_validator("age") def age_must_be_positive(cls, v): if v < 0: raise ValueError("Age must be a positive number") return v
For example, for handling configuration information, we can also introduce python-dotenv and pydantic_settings to unify the management of configuration parameters.
from pydantic_settings import BaseSettings, SettingsConfigDict class Settings(BaseSettings): model_config = SettingsConfigDict( env_file=".env", # Loading env files extra="ignore", # Load the env file and don't throw an exception if the properties are not defined in Settings env_file_encoding="utf-8", env_prefix="", case_sensitive=False, ) # Env Server SERVER_IP: str = "127.0.0.1" SERVER_PORT: int = 9000 # Env Database DB_NAME: str = "winframework" DB_USER: str = "root" DB_PASSWORD: str = "123456" DB_HOST: str = "localhost" DB_PORT: int = 3306 DB_URI: str = ( f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}" )
.............
There are some conventional file formats, such as json format, txt format file processing, as well as PDF files, Excel files, image manipulation, sound processing, two-dimensional code processing, etc., there are different libraries to provide auxiliary processing, we can choose from the best choice.
The world of Python is rich and colorful, let's explore it together and apply it in practice.