Name Resolution Order
# Avoid naming your modules the same as built-in Python modules
Python searches for names in this sequence:
- Local scope (memory)
- Built-in modules
- Paths in
sys.path
import sys
print(sys.path)
If a module cannot be found, there are two solutions:
# Solution 1: Append the module path to sys.path
import sys
sys.path.append(r'D:\Projects\myapp\lib')
# Solution 2: Use from...import syntax
from myapp.utils.helpers import validate_input
from myapp.utils.helpers import validate_input as validate
Relative vs Absolute Imports
Module resolution always starts from the path where the executing script resides
Absolute Imports
Absolute imports search from the executing script's sys.path as the base. Examples:
from package_a.module_x import function_a
import package_a.module_x as mod_x
Relative Imports
. refers to the current directory
.. refers to the parent directory
# Relative imports can override the default resolution behavior
from .submodule import helper_func
from ..parent_module import utility
from .services.api_handler import process_request
Important: Any file containing relative imports cannot be executed directly. It must only be imported as a module.
Package Structure
A package is a hierarchical directory structure containing modules and sub-packages. In essence, a package is a directory, but it must contain an __init__.py file (which can be empty) to mark it as a Python package.
project/
├── __init__.py
├── submodule/
│ ├── __init__.py
│ └── utilities.py
└── core/
└── __init__.py
Access module contents through __init__.py:
# In package/__init__.py
from .submodule.utilities import calculate
Project Directory Structure
A well-organized project follows consistent naming conventions:
project_name/
├── bin/
│ └── start.py # Entry point script
├── conf/
│ └── settings.py # Configuration and initialization data
├── core/
│ ├── src.py # Core business logic
│ └── handlers.py # Request/response handlers
├── lib/
│ └── common.py # Shared utility functions
├── log/
│ └── app.log # Application logs
├── db/
│ └── database.db # Data storage
├── api/
│ └── endpoints.py # API interface layer
├── setup.py # Package installation script
├── requirements.txt # Dependencies
└── README.md # Project documentation
Requirements file example:
requests==2.28.0
beautifulsoup4==4.11.0
pymongo==4.3.0
Naming conventions: Use descriptive, English names. Avoid special characters and never use Chinese characters in directory or file names.
Regular Expressions Basics
import re
phone = input('Enter phone number: ')
if re.match(r'^(13|14|15|18)[0-9]{9}$', phone):
print('Valid phone number')
else:
print('Invalid phone number')
Regex is a standalone pattern-matching language independent of any programming language. Python provides regex support through the built-in re module.
Common patterns:
\d- matches any digit\w- matches alphanumeric characters.- matches any character (except newline)*- zero or more occurrences+- one or more occurrences[abc]- matches any character in the set^- anchor at start$- anchor at end