Python项目管理
Python项目目录说明
主要包含:
1、pyproject.toml 配置信息文件,其中包含项目基本信息、正式环境依赖、测试环境依赖等信息
2、源代码文件夹:项目的主要代码
3、tests文件夹:测试代码存放目录
4、README.md:项目说明文件
poetry-demo
├── pyproject.toml
├── README.md
├── poetry_demo
│ └── __init__.py
└── tests
└── __init__.py
环境使用说明
poetry的安装与使用
相关网址
官网:https://python-poetry.org/
github: https://github.com/python-poetry/poetry
参考blog:https://blog.kyomind.tw/python-poetry/ \
使用 pipx 来安装 Poetry
- 安装 pipx
使用 pip 安装 pipx:
pip install --user pipx
或者,在某些系统中,你可能需要使用 python -m 来安装:
python -m pip install --user pipx
确保 pipx 被正确安装,可以使用以下命令进行验证:
pipx --version
如果安装成功,应该会看到 pipx 的版本号。
- 使用 pipx 安装 Poetry
pipx install poetry
pipx 会创建一个隔离的环境来安装 Poetry,并将其添加到你的 PATH 中。
如果使用全局的pipx下载,其Poetry命令会在本地 C:\Users\Administrator\.local\bin
目录下,将其手动加入Path环境
安装过程完成后,Poetry 可以直接在终端中使用。
- 验证安装
安装完成后,可以验证是否成功安装 Poetry:
poetry --version
如果安装成功,你会看到类似如下的输出: Poetry version 1.x.x
- 更新 Poetry
pipx upgrade poetry
- 卸载 Poetry
pipx uninstall poetry
默认安装目录:
~/Library/Application Support/pypoetry on MacOS.
/.local/share/pypoetry on Linux/Unix.
%APPDATA%\pypoetry on Windows.
安装目录的绝对路径添加到$PATH中:
~/Library/Application Support/pypoetry/venv/bin/poetry on MacOS.
~/.local/share/pypoetry/venv/bin/poetry on Linux/Unix.
%APPDATA%\pypoetry\venv\Scripts\poetry on Windows.
环境配置参数
基本配置信息
https://python-poetry.org/docs/configuration/
使用Poetry创建虚拟环境时,默认的情况是将虚拟环境创建在c盘下,所以可以修改Poetry的virtualenvs.path的配置。
poetry config --list
poetry config virtualenvs.path 想要修改的路径
以virtualenvs.path举例
想要修改配置就使用
poetry config virtualenvs.path 需要修改的值
想要恢复默认值就使用
poetry config virtualenvs.path --unset
1、初始化项目
使用poetry new 初始化一个项目
poetry new poetry-demo
这将会创建一个名为poetry-demo的目录,目录结构如下
poetry-demo
├── pyproject.toml
├── README.md
├── poetry_demo
│ └── __init__.py
└── tests
└── __init__.py
2、已创建的项目改用poetry
mkdir poetry_demo
cd poetry_demo
执行poe init
poetry init
会出现一系列选项,可以根据自己的需要修改或使用默认:
Package name [test]: # 打包的名字
Version [0.1.0]: # 版本
Description []: # 描述
Author [test test@163.com, n to skip]: # 作者信息
License []: # license
Compatible Python versions [^3.10]: # 兼容的python版本
# 定义依赖,不会安装,只在生成的pyproject.toml中记录,可以选no跳过
Would you like to define your main dependencies interactively? (yes/no) [yes]
Package to add or search for (leave blank to skip): # 要安装的依赖名
Enter package # to add, or the complete package name if it is not listed []: # 从搜寻到的依赖中选择你要安装的
Enter the version constraint to require (or leave blank to use the latest version): # 依赖版本
Add a package (leave blank to skip): # 重复上面搜索步骤
Would you like to define your development dependencies interactively? (yes/no) [yes] # 开发环境的依赖,如pytest等
Do you confirm generation? (yes/no) [yes]
此时目录结构如下:
poetry-demo
└── pyproject.toml
├──tests
│ └── __init__.py
├── poetry_demo
│ └── __init__.py
poetry常用命令汇总
poetry 提供了一系列覆盖整个开发流程的命令,这些命令使用简单,如表所示:
名称 | 功能 |
---|---|
new | 创建一个项目脚手架,包含基本结构、pyproject.toml 文件 |
init | 基于已有的项目代码创建 pyproject.toml 文件,支持交互式填写 |
install | 安装依赖库 |
update | 更新依赖库 |
add | 添加依赖库 |
remove | 移除依赖库 |
show | 查看具体依赖库信息,支持显示树形依赖链 |
build | 构建 tar.gz 或 wheel 包 |
publish | 发布到 PyPI |
run | 运行脚本和代码 |
shell | 激活虚拟环境 |
核心包使用
通过poetry进行包管理
poetry install
Publish To PyPI
配置文件信息
[distutils]
index-servers =
pypi
pypitest
nexus
nexustest
[pypi]
repository:https://pypi.python.org/pypi
username:your_username
password:your_password
[pypitest]
repository:https://testpypi.python.org/pypi
username:your_username
password:your_password
[nexus]
repository=http://localhost:8888/repository/mypypi-hosted/
username=your_username
password=your_password
[nexustest]
repository=http://localhost:8888/repository/mypypi-hosted/
username=your_username
password=your_password
使用poetry进行配置
配置信息:
[[tool.poetry.source]]
name = "nexus"
url = "http://localhost:8888/repository/pypi_host_test/"
# 配置nexus Poetry 使用 PyPI 的 API 令牌进行认证。在命令行中设置认证信息:
poetry config http-basic.nexus <username> <password-or-api-token>
# 构建
poetry build
# 发布
poetry publish --build --repository nexus
私有仓库使用说明
配置信息:
[[tool.poetry.source]]
name = "nexus"
url = "http://localhost:8888/repository/pypi_host_test/"
# 配置nexus Poetry 使用 PyPI 的 API 令牌进行认证。在命令行中设置认证信息:
poetry install
poetry update
调试代码情况下,使用本地源代码信息:
使用此信息更新依赖,不用对使用的依赖项目进行打包,可直接使用源码信息,方便项目调试
[tool.poetry.group.dev.dependencies]
pl_license_valid = {path = "..\\pl_license_valid", develop = true}
nexus私有仓库配置
Nexus Repository Manager OSS 3.x
1、安装
https://www.sonatype.com/nexus-repository-oss 下载,解压,运行即可
执行启动命令
cd /home/soft/nexus-3.10.0-04-unix/nexus-3.10.0-04/bin
./nexus start
然后访问 http://localhost:8081/#browse/welcome
2、PyPi 支持
默认登录用户名/密码: admin/admin123
官方文档地址: https://books.sonatype.com/nexus-book/reference3/pypi.html
3、配置pypi的仓库
建立官方代理仓库 mypypi
填写远程索引地址时用 https://pypi.python.org/
建立 hosted 仓库,用于内部使用 mypypi-hosted
建立 group 仓库把官方代理和 hosted 仓库包含进来 mypypi-group
其中:代理库的代理配置,也可以换成阿里云的地址:http://mirrors.aliyun.com/pypi
先打包本地项目 主要是两步,打包、发布
参照官方文档:
setup规范 https://packaging.python.org/tutorials/distributing-packages/#setup-py
twine使方法 https://pypi.org/project/twine/
posted on 2024-12-31 11:13 Chase_Hanky 阅读(22) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了