python -m venv venv
linux 下激活
source venv/bin/activate
window 下激活
venv\Scripts\activate
退出虚拟环境
deactivate
除了 python 官方自带的虚拟环境以外,我们还可以使用 conda 管理环境与依赖。
powershell 下无法使用 conda 解决办法,输入后重启命令窗口
conda init powershell
查看所有已存在环境
conda env list
创建环境
conda create -n llm#指定python 版本conda create -n llm python=3.11
激活环境与退出环境
conda activate llmconda deactivate
删除环境
conda remove -n llm --all
jupyter使用conda环境
conda create -n my-conda-envconda activate my-conda-envconda install ipykernelipython kernel install --user --name=my-conda-env-kerneljupyter notebook
pip freeze > requirements.txt
pip install pipreqs
pipreqs ./ --encoding=utf8 --force
简单使用
import logginglogging.basicConfig(format='%(levelname)s - %(asctime)s - %(message)s',level=logging.DEBUG)logging.warning('this is %s log','warning')logging.info('info log')logging.error('error log')
运行代码输出:
WARNING - 2023-04-21 10:27:38,631 - this is warning logINFO - 2023-04-21 10:27:38,631 - info logERROR - 2023-04-21 10:27:38,631 - error log
采用 YAML 格式,用于新的基于字典的方法
version: 1formatters:simple:format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'handlers:console:class: logging.StreamHandlerlevel: DEBUGformatter: simplestream: ext://sys.stdoutloggers:app:level: DEBUGhandlers: [console]propagate: noroot:level: DEBUGhandlers: [console]
使用如下:
import logging.configimport yamlwith open(file='conf/logging.yaml', mode='r', encoding='utf8') as file:logging_yaml = yaml.load(stream=file, Loader=yaml.FullLoader)logging.config.dictConfig(config=logging_yaml)logger = logging.getLogger('app')if __name__ == '__main__':logger.warning('this is %s log', 'warning')logger.info('info log')logger.error('error log')
import ossecret_key = os.environ.get('SECRET_KEY', 'jajajajhh')
class Configure:def __init__(self):self.map_parameters = {}self.cfg_file = os.path.abspath(os.path.join(os.path.dirname(__file__), ".conf/conf.yaml"))self.init_local()def init_local(self):if os.path.exists(self.cfg_file):with open(self.cfg_file, "r+", encoding="utf-8") as reader:configure = yaml.load(reader, Loader=yaml.SafeLoader)if "hbase" not in configure.keys():raise ValueError("not hbase tables configure")self.map_parameters["hbase"] = configure["hbase"]self.map_parameters["hbase.common_param"] = configure["hbase"]["common_param"]self.map_parameters["hbase.tables"] = configure["hbase"]["tables"]
使用如下:
conf = Configure()hbase_param = conf.map_parameters["hbase"]