首先您需要在计算机上安装诗歌。有很多方法可以做到这一点,我使用brew(brew install诗)下载它。您可以通过适当的方式从Poetry自己的网站下载。
但是让我们首先简要了解一下诗歌是什么以及它与 virtualenv 有何不同
Poetry是一个管理Python项目和依赖项的工具,可以更轻松地处理包管理和项目结构。它允许开发人员以简单的方式定义和管理依赖项、创建虚拟环境并处理项目版本控制。
与 venv 的区别:
VirtualEnv:venv 是一个标准的 Python 模块,用于创建隔离的虚拟环境,其中依赖项与全局环境分开安装。然而,venv 本身并不管理项目依赖关系;你通常需要使用 pip 之类的东西来安装依赖项并手动跟踪它们。
Poetry:另一方面,Poetry 不仅创建虚拟环境,还自动管理依赖关系。它使用 pyproject.toml 文件来跟踪依赖项、版本和项目配置。 Poetry 还通过处理版本控制和打包来简化 Python 包的发布。
综上所述,venv 创建隔离环境的同时,Poetry 提供了更全面的项目管理解决方案,包括依赖解析和打包工具。
安装Poetry后,我们来创建Django项目:
thehormat@Pawn Desktop % mdkir DjangoPoetry thehormat@Pawn DjangoPoetry % cd DjangoPoetry thehormat@Pawn DjangoPoetry % poetry init This command will guide you through creating your pyproject.toml config. Package name [djangopoetry]: DjangoPoetry Version [0.1.0]: Description []: We learn Poetry Author [HormatHamidov, n to skip]: TheHormat License []: Compatible Python versions [^3.12]: Would you like to define your main dependencies interactively? (yes/no) [yes] no Would you like to define your development dependencies interactively? (yes/no) [yes] no Generated file [tool.poetry] name = "djangopoetry" version = "0.1.0" description = "We learn Poetry" authors = ["TheHormat"] readme = "README.md" [tool.poetry.dependencies] python = "^3.12" [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" Do you confirm generation? (yes/no) [yes]
然后我们将有一个这样的文件:
太好了,现在让我们继续吧。我们需要将 Django 添加到我们的项目中。但在此之前我们需要先激活诗歌,就像virtualenv逻辑一样:
thehormat@Pawn DjangoPoetry % poetry shell Creating virtualenv djangopoetry-5djS955q-py3.12 in /Users/thehormat/Library/Caches/pypoetry/virtualenvs Spawning shell within /Users/thehormat/Library/Caches/pypoetry/virtualenvs/djangopoetry-5djS955q-py3.12 thehormat@Pawn DjangoPoetry % emulate bash -c '. /Users/thehormat/Library/Caches/pypoetry/virtualenvs/djangopoetry-5djS955q-py3.12/bin/activate' (djangopoetry-py3.12) thehormat@Pawn DjangoPoetry %
(djangopoetry-py3.12) thehormat@Pawn DjangoPoetry %
⬆️ 如果您在终端中看到此结果,则诗歌处于活动状态。
然后让我们将Django添加到项目中:
(djangopoetry-py3.12) hormathamidov@Pawn DjangoPoetry % poetry add django Using version ^5.1.2 for django Updating dependencies Resolving dependencies... (0.3s) Package operations: 3 installs, 0 updates, 0 removals - Installing asgiref (3.8.1) - Installing sqlparse (0.5.1) - Installing django (5.1.2) Writing lock file
我们将在 toml 文件中看到这样的新奇之处。这意味着我们已经成功设置了 django:
[tool.poetry.dependencies] python = "^3.12" django = "^5.1.2"
现在,我们终于可以正常安装并运行 django 了:
(djangopoetry-py3.12) thehormat@Pawn DjangoPoetry % django-admin startproject core . (djangopoetry-py3.12) thehormat@Pawn DjangoPoetry % python manage.py runserver Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them. October 14, 2024 - 18:22:05 Django version 5.1.2, using settings 'core.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
这是令人惊奇的结果:
我想为 Poetry 提供一点帮助,您可以从 Poetry 自己的文档中了解更多信息。
?您不必惊慌,经过详细搜索后,您可以轻松找到并修复它。
?️ 在您离开之前...如果您有任何问题/建议/想法,请在下面给我留言。
如果您喜欢这个,请告诉我们您对漂亮表情符号的感受(?❤️?),并且不要忘记关注未来的更新。
这就是我的。我们很快就会谈!
— TheHormat ♟️
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3