Package Configuration Tips
Creating setup.py
from setuptools import setup, find_packages
setup(
name='mypackage',
version='0.1.0',
packages=find_packages(),
install_requires=[
'numpy>=1.18.0',
'pandas>=1.0.0'
],
author='Your Name',
description='A sample Python package',
python_requires='>=3.8'
)
Dependency Management Strategies
graph TD
A[Dependency Management] --> B[requirements.txt]
A --> C[setup.py]
A --> D[pyproject.toml]
Configuration File Techniques
1. Using Configuration Parsers
## config.py
import configparser
def load_config(config_path):
config = configparser.ConfigParser()
config.read(config_path)
return config
2. Environment-Based Configuration
## config.py
import os
class Config:
def __init__(self):
self.env = os.getenv('APP_ENV', 'development')
self.config = self._load_config()
def _load_config(self):
configs = {
'development': self._dev_config(),
'production': self._prod_config()
}
return configs.get(self.env, self._dev_config())
Package Configuration Best Practices
Practice |
Description |
Recommendation |
Centralized Config |
Single source of truth |
Use dedicated config module |
Environment Separation |
Different configs per environment |
Implement environment-based loading |
Secure Credentials |
Avoid hardcoding sensitive data |
Use environment variables |
Advanced Configuration Techniques
Logging Configuration
## logging_config.py
import logging
import sys
def setup_logging(log_level='INFO'):
logging.basicConfig(
level=getattr(logging, log_level),
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(sys.stdout),
logging.FileHandler('app.log')
]
)
Dynamic Plugin Loading
## plugin_manager.py
import importlib
import pkgutil
import inspect
def discover_plugins(package):
plugins = []
for loader, name, is_pkg in pkgutil.iter_modules(package.__path__):
full_name = f"{package.__name__}.{name}"
module = importlib.import_module(full_name)
for item_name, item in inspect.getmembers(module):
if inspect.isclass(item) and hasattr(item, 'is_plugin'):
plugins.append(item)
return plugins
Package Distribution Considerations
Creating pyproject.toml
[build-system]
requires = ["setuptools>=45", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "mypackage"
version = "0.1.0"
description = "A sample Python package"
requires-python = ">=3.8"
[project.optional-dependencies]
dev = ["pytest", "flake8"]
LabEx Packaging Recommendations
By implementing these configuration techniques, developers can create more flexible, maintainable, and scalable Python packages that meet professional software development standards.