https://pyloong.github.io/pythonic-project-guidelines/practices/web/#39
开发环境搭建
安装虚拟环境工具:poetry
brew install pipx
pipx install poetry
pipx ensurepath
pipx upgrade poetry(需要时)
项目初始化
初始化项目结构
mkdir pictrack
cd pictrack
mkdir -p src/pictrack/
touch src/pictrack/__init__.py
mkdir tests
touch tests/__init__.py
% tree
.
├── README.md
├── src
│ └── pictrack
│ └── __init__.py
└── tests
└── __init__.py
4 directories, 3 files
初始化项目虚拟环境:
poetry init
% cat pyproject.toml
[tool.poetry]
name = "pictrack"
version = "0.1.0"
description = ""
authors = ["yongjiangshi <[email protected]>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
fastapi = "^0.112.2"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
编写 README.md
文件
安装开发包
poetry install
初始 Git 提交
git init
git add .
git commit -m "feat: First commit"
项目功能开发
创建命令行入口
安装依赖
poetry add click
创建cmdline.py
touch src/pictrack/cmdline.py
编辑 pyproject.toml
,将命令行入口注册到项目描述文件中:
[tool.poetry.scripts]
pictrack = "pictrack.cmdline:main"
提交代码
git add .
git commit -m "feat: Add cmdline."
引入项目配置系统
poetry add dynaconf
建立配置包,和配置文件:
mkdir src/pictrack/config
touch src/pictrack/config/__init__.py
touch src/pictrack/config/settings.yml
提交代码:
git add .
git commit -m "feat: Add config."
引入日志
创建 src/pictrack/log.py
,初始化 log
from logging.config import dictConfig
from pictrack.config import settings
def init_log():
log_config = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'sample': {'format': '%(asctime)s %(levelname)s %(message)s'},
'verbose': {'format': '%(asctime)s %(levelname)s %(name)s %(process)d %(thread)d %(message)s'},
"access": {
"()": "uvicorn.logging.AccessFormatter",
"fmt": '%(asctime)s %(levelprefix)s %(client_addr)s - "%(request_line)s" %(status_code)s',
},
},
'handlers': {
"console": {
"formatter": 'verbose',
'level': 'DEBUG',
"class": "logging.StreamHandler",
},
},
'loggers': {
'': {'level': settings.LOG_LEVEL, 'handlers': ['console']},
},
}
dictConfig(log_config)
```
提交代码:
```bash
git add .
git commit -m "feat: Add log"
数据访问
安装依赖
poetry add sqlalchemy psycopg2-binary redis
激活虚拟环境
poetry shell
启动 Server
将本项目以可编辑方式安装到当前 Python 环境:
pip install -e .
命令行运行:
pictrack server
打包发布
打包
poetry build
发布
poetry publish
要使用Poetry将包发布到内部或私有PyPI服务器,可以按以下步骤操作:
- 配置自定义仓库
使用以下命令添加自定义仓库:
text
poetry config repositories.my_repository https://your-internal-pypi-server.com
poetry config repositories.pypi_dev http://127.0.0.1.8080/simple/
将 my_repository
替换为你想给这个仓库起的名字,URL替换为你的内部PyPI服务器地址。
- 配置认证信息
如果你的内部PyPI服务器需要认证,可以配置用户名和密码:
text
poetry config http-basic.my_repository username password
poetry config http-basic.pypi_dev pictrack pictrack
或者使用API令牌:
text
poetry config pypi-token.my_repository your-api-token
- 发布到自定义仓库
使用 -r
或 --repository
选项指定发布到哪个仓库:
text
poetry publish -r my_repository
poetry publish -r pypi_dev
如果你想在发布前构建,可以加上 --build
选项:
text
poetry publish --build -r my_repository
- 在pyproject.toml中配置
你也可以在 pyproject.toml
文件中配置发布仓库:
text
[tool.poetry.repositories] my_repository = { url = "https://your-internal-pypi-server.com" }
然后发布时就可以直接使用:
text
poetry publish -r my_repository
- 注意事项
- 确保你有权限发布到指定的内部PyPI服务器
- 发布前先测试包的构建和安装
- 考虑使用版本控制和CI/CD流程来自动化发布过程
通过这些步骤,你就可以使用Poetry将包发布到指定的内部PyPI服务器了。这样可以确保你的私有包只在内部网络中可用,增强了安全性和控制性。
pip config set global.index-url http://server_ip:8080/simple
pip config set global.trusted-host server_ip
正确publish
poetry config repositories.pypi_dev http://127.0.0.1:8080
poetry publish -r pypi_dev --verbose -u shi -p shi
% poetry publish --build -r pypi_dev --verbose -u shi -p shi
There are 2 files ready for publishing. Build anyway? (yes/no) [no] y
Using virtualenv: /Users/macuser03/Library/Caches/pypoetry/virtualenvs/pictrack-xwTNEhgi-py3.12
Building pictrack (0.1.0)
- Building sdist
- Built pictrack-0.1.0.tar.gz
- Building wheel
- Built pictrack-0.1.0-py3-none-any.whl
Publishing pictrack (0.1.0) to pypi_dev
- Uploading pictrack-0.1.0-py3-none-any.whl 100%
- Uploading pictrack-0.1.0.tar.gz 100%