简介
Poetry 是一个用于管理 Python 项目的工具,它可以帮助我们轻松地创建、管理和发布项目,在 Fedora 系统上使用 Poetry 管理 Python 项目,可以让我们更专注于编写代码,而不需要担心项目依赖和环境配置的问题,本文将详细介绍如何在 Fedora 上安装和使用 Poetry,以及如何将其与虚拟环境结合使用。
安装 Poetry
1、打开终端,输入以下命令安装 Poetry:
sudo dnf install python3-pip python3-devtoolset-devel poetry
2、安装完成后,输入以下命令检查 Poetry 是否安装成功:
poetry --version
创建虚拟环境
1、在项目根目录下,运行以下命令创建一个虚拟环境:
poetry init
这将在项目根目录下创建一个名为 pyproject.toml
的配置文件,以及一个名为 venv
的虚拟环境目录,Poetry 会自动生成一个名为 Pipfile
的文件,用于记录项目的依赖关系。
2、激活虚拟环境:
对于 Linux 用户:
source venv/bin/activate
对于 Windows 用户:
venvScripts\activate
对于 macOS 用户:
source venv/bin/activate
安装依赖包
1、在项目根目录下,运行以下命令添加依赖包:
poetry add <package_name>
要安装 requests 包,可以运行:
poetry add requests
2、如果需要指定依赖包的版本,可以在 <package_name>
后面加上 ==<version>
,
poetry add requests==2.25.1
运行项目
1、在项目根目录下,运行以下命令启动开发服务器:
poetry run python your_script.py
2、要构建生产环境的可执行文件,可以运行以下命令:
poetry build --no-dev --target py37-manylinux2010_x86_64 --output lib/your_project_name/your_project_name.egg-info/your_project_name-1.0.0-py37-none-any.whl
发布项目到 PyPI(Python Package Index)
1、确保已经安装了 twine
,如果没有安装,可以使用以下命令安装:
pip install twine
2、进入到 dist
目录下,运行以下命令打包项目:
poetry build --no-dev --target py37-manylinux2010_x86_64 --format wheel --output dist/your_project_name-1.0.0-py37-none-any.whl
3、使用 twine
将打包好的文件上传到 PyPI:
twine upload --repository pypi dist/your_project_name-1.0.0-py37-none-any.whl --user your_username --password your_password --verbose --skip-existing --sign your_email@example.com;python3-setuptools;binarytype=wheel;platform=linux-x86_64;classifiers=Development Status :: 3 Alpha;Programming Language :: Python :: 3 :: Only;License :: OSI Approved :: MIT License;Programming Language :: Python :: 3.7;Programming Language :: Python :: 3 :: Only;Topic :: Software Development :: Build Tools :: Wheel;Topic :: Software Development :: Libraries :: Python Modules;Environment :: Console;Operating System :: Microsoft :: Windows :: WindowsServer :: WindowsServerCore;Operating System :: POSIX :: Linux;Operating System :: Unix;Operating System :: MacOS::X"'"'--skip-existing'"'"' "'"'--sign your_email@example.com;python3-setuptools;binarytype=wheel;platform=linux-x86_64;classifiers=Development Status :: 3 Alpha;Programming Language :: Python :: 3 :: Only;License :: OSI Approved :: MIT License;Programming Language :: Python :: 3.7;Programming Language :: Python :: 3 :: Only;Topic :: Software Development :: Build Tools :: Wheel;Topic :: Software Development :: Libraries :: Python Modules;Environment :: Console;Operating System :: Microsoft :: Windows :: WindowsServer :: WindowsServerCore;Operating System :: POSIX :: Linux;Operating System :: Unix;Operating System :: MacOS::X"'"'--skip-existing'"'"' "'"'--sign your_email@example.com;python3-setuptools;binarytype=wheel;platform=linux-x86_64;classifiers=Development Status :: 3 Alpha;Programming Language :: Python :: 3 :: Only;License :: OSI Approved :: MIT License;Programming Language :: Python :: 3.7;Programming Language :: Python :: 3 :: Only;Topic :: Software Development :: Build Tools :: Wheel;Topic :: Software Development :: Libraries :: Python Modules;Environment :: Console;Operating System :: Microsoft :: Windows :: WindowsServer :: WindowsServerCore;Operating System :: POSIX :: Linux;Operating System :: Unix;Operating System :: MacOS::X"'"'--skip-existing'"'"' "'"'--sign your_email@example.com;python3-setuptools;binarytype=wheel;platform=linux-x86_64;classifiers=Development Status :: 3 Alpha;Programming Language :: Python :: 3 :: Only;License :: OSI Approved :: MIT License;Programming Language :: Python :: 3.7;Programming Language :: Python :: 3 :: Only;Topic :: Software Development :: Build Tools -> Wheel'"'"' "'"'--sign your_email@example.com;python3-setuptools;binarytype=wheel;platform=linux-x86_64;classifiers=Development Status \\u0026gt;\u0026gt;u0026gt;\u0026gt;\u0026gt;\u0026gt;\u0026gt;\u0026gt;\u0026gt;\u0026gt;u0026gt;\u0026gt;\u0026gt;\u0026gt;\u0026gt;\u0026gt;\u0026gt;\u0026gt;u0026gt;\u0026gt;\u0026gt;\u0026gt;\u0026gt;\u0026gt;\u0026gt;\u0026gt;u0026gt;\u0026gt;\u0026gt;\u0026gt;\u0026gt;\u0026gt;\u0026gt;\u0026gt;u0026gt;\ u(?![aA])(w|\W)*? 问题1:如何在Fedora上安装Poetry?问题解答:参考本文第一部分的“在Fedora上安装Poetry”。
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/130475.html