16 lines
44 KiB
HTML
16 lines
44 KiB
HTML
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width"><meta name="theme-color" content="#222"><meta name="generator" content="Hexo 5.4.2"><link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png"><link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png"><link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png"><link rel="mask-icon" href="/images/logo.svg" color="#222"><link rel="stylesheet" href="/css/main.css"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha256-HtsXJanqjKTc8vVQjO4YMhiqFoXkfBsjBWcX91T1jr8=" crossorigin="anonymous"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css" integrity="sha256-Vzbj7sDDS/woiFS3uNKo8eIuni59rjyNGtXfstRzStA=" crossorigin="anonymous"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/blue/pace-theme-minimal.css"><script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script><script class="next-config" data-name="main" type="application/json">{"hostname":"kiki.kim","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.15.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"flat"},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":false,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果:${query}","hits_time":"找到 ${hits} 个搜索结果(用时 ${time} 毫秒)","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script><meta name="description" content="使用poetry来管理python项目,快速的构建开发环境,快速的发布,以及进行第三方库的依赖管理。"><meta property="og:type" content="article"><meta property="og:title" content="使用poetry来管理python项目"><meta property="og:url" content="http://kiki.kim/2021/01/23/%E4%BD%BF%E7%94%A8poetry%E6%9D%A5%E7%AE%A1%E7%90%86python%E9%A1%B9%E7%9B%AE/index.html"><meta property="og:site_name" content="随言碎语"><meta property="og:description" content="使用poetry来管理python项目,快速的构建开发环境,快速的发布,以及进行第三方库的依赖管理。"><meta property="og:locale" content="zh_CN"><meta property="article:published_time" content="2021-01-23T02:25:34.000Z"><meta property="article:modified_time" content="2023-05-11T04:30:22.305Z"><meta property="article:author" content="小梦同学"><meta property="article:tag" content="python"><meta property="article:tag" content="poetry"><meta name="twitter:card" content="summary"><link rel="canonical" href="http://kiki.kim/2021/01/23/%E4%BD%BF%E7%94%A8poetry%E6%9D%A5%E7%AE%A1%E7%90%86python%E9%A1%B9%E7%9B%AE/"><script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://kiki.kim/2021/01/23/%E4%BD%BF%E7%94%A8poetry%E6%9D%A5%E7%AE%A1%E7%90%86python%E9%A1%B9%E7%9B%AE/","path":"2021/01/23/使用poetry来管理python项目/","title":"使用poetry来管理python项目"}</script><script class="next-config" data-name="calendar" type="application/json">""</script><title>使用poetry来管理python项目 | 随言碎语</title><noscript><link rel="stylesheet" href="/css/noscript.css"></noscript><link rel="alternate" href="/atom.xml" title="随言碎语" type="application/atom+xml"><style>.darkmode--activated{--body-bg-color:#282828;--content-bg-color:#333;--card-bg-color:#555;--text-color:#ccc;--blockquote-color:#bbb;--link-color:#ccc;--link-hover-color:#eee;--brand-color:#ddd;--brand-hover-color:#ddd;--table-row-odd-bg-color:#282828;--table-row-hover-bg-color:#363636;--menu-item-bg-color:#555;--btn-default-bg:#222;--btn-default-color:#ccc;--btn-default-border-color:#555;--btn-default-hover-bg:#666;--btn-default-hover-color:#ccc;--btn-default-hover-border-color:#666;--highlight-background:#282b2e;--highlight-foreground:#a9b7c6;--highlight-gutter-background:#34393d;--highlight-gutter-foreground:#9ca9b6}.darkmode--activated img{opacity:.75}.darkmode--activated img:hover{opacity:.9}.darkmode--activated code{color:#69dbdc;background:0 0}button.darkmode-toggle{z-index:9999}.darkmode-ignore,img{display:flex!important}.beian img{display:inline-block!important}</style></head><body itemscope itemtype="http://schema.org/WebPage"><div class="headband"></div><main class="main"><div class="column"><header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container"><div class="site-nav-toggle"><div class="toggle" aria-label="切换导航栏" role="button"><span class="toggle-line"></span> <span class="toggle-line"></span> <span class="toggle-line"></span></div></div><div class="site-meta"><a href="/" class="brand" rel="start"><i class="logo-line"></i><p class="site-title">随言碎语</p><i class="logo-line"></i></a><p class="site-subtitle" itemprop="description">咕叽咕叽</p></div><div class="site-nav-right"><div class="toggle popup-trigger" aria-label="搜索" role="button"><i class="fa fa-search fa-fw fa-lg"></i></div></div></div><nav class="site-nav"><ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">63</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">56</span></a></li><li class="menu-item menu-item-talk"><a href="/talk/" rel="section"><i class="fa fa-commenting fa-fw"></i>碎语湖</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-search"><a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索</a></li></ul></nav><div class="search-pop-overlay"><div class="popup search-popup"><div class="search-header"><span class="search-icon"><i class="fa fa-search"></i></span><div class="search-input-container"><input autocomplete="off" autocapitalize="off" maxlength="80" placeholder="搜索..." spellcheck="false" type="search" class="search-input"></div><span class="popup-btn-close" role="button"><i class="fa fa-times-circle"></i></span></div><div class="search-result-container no-result"><div class="search-result-icon"><i class="fa fa-spinner fa-pulse fa-5x"></i></div></div></div></div><script type="text/javascript" src="/js/cursor/zjAdd.js"></script><div class="musicwyy2" style="top:200px;position:fixed;left:0;bottom:0;z-index:105;opacity:1;width:45px;height:45px;font-size:45px;font-family:'Long Cang'"><button type="button" style="top:200px;width:45px;height:45px;margin-top:10px;margin-bottom:10px;border-width:0;z-index:200;background:0 0;border-width:0;outline:0" onclick="musicChange()"><img src="/images/wy.svg"></button></div><div class="musicwyy1" style="position:fixed;top:200px;left:0;bottom:0;z-index:100;opacity:1;width:45px;height:45px;font-size:0"><div class="aplayer-miniswitcher" style="top:200px;height:110px;width:20px;display:inline-block;overflow:hidden"></div><div style="width:calc(100% - 30px);top:200px;height:110px;display:none" id="musicZJ"><div class="musicwyy1" style="position:fixed;top:200px;left:15px;bottom:0;z-index:100;opacity:1;width:300px;height:110px;font-size:0"><div class="aplayer-miniswitcher" style="top:200px;height:110px;width:20px;display:inline-block;overflow:hidden"></div><div style="width:calc(100% - 30px);top:200px;height:110px;display:inline-block" id="musicZJ"><iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width="330" height="450" src="//music.163.com/outchain/player?type=0&id=8009036598&auto=0&height=430"></iframe></div></div></div></div></header><aside class="sidebar"><div class="sidebar-inner sidebar-nav-active sidebar-toc-active"><ul class="sidebar-nav"><li class="sidebar-nav-toc">文章目录</li><li class="sidebar-nav-overview">站点概览</li></ul><div class="sidebar-panel-container"><div class="post-toc-wrap sidebar-panel"><div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E8%83%8C%E6%99%AF"><span class="nav-number">1.</span> <span class="nav-text">1.背景</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%AE%89%E8%A3%85"><span class="nav-number">2.</span> <span class="nav-text">2. 安装</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="nav-number">3.</span> <span class="nav-text">3. 基本使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-%E6%B7%BB%E5%8A%A0poetry%E7%AE%A1%E7%90%86"><span class="nav-number">3.1.</span> <span class="nav-text">3.1 添加poetry管理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-%E5%88%9B%E5%BB%BA%E8%99%9A%E6%8B%9F%E7%8E%AF%E5%A2%83"><span class="nav-number">3.2.</span> <span class="nav-text">3.2 创建虚拟环境</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-%E5%AE%89%E8%A3%85%E5%8C%85%E3%80%81%E5%8D%B8%E8%BD%BD%E5%8C%85"><span class="nav-number">3.3.</span> <span class="nav-text">3.3 安装包、卸载包</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4-%E9%94%81%E5%AE%9A%E4%BE%9D%E8%B5%96"><span class="nav-number">3.4.</span> <span class="nav-text">3.4 锁定依赖</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4-%E6%BF%80%E6%B4%BB%E8%99%9A%E6%8B%9F%E7%8E%AF%E5%A2%83"><span class="nav-number">3.5.</span> <span class="nav-text">3.4 激活虚拟环境</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-5-%E5%88%A0%E9%99%A4%E7%8E%AF%E5%A2%83"><span class="nav-number">3.6.</span> <span class="nav-text">3.5 删除环境</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-6-build%EF%BC%9A%E6%9E%84%E5%BB%BA"><span class="nav-number">3.7.</span> <span class="nav-text">3.6 build:构建</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-7-publish%EF%BC%9A%E5%8F%91%E5%B8%83%E5%88%B0%E4%BB%93%E5%BA%93"><span class="nav-number">3.8.</span> <span class="nav-text">3.7 publish:发布到仓库</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-pyproject-toml-%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E7%9A%84%E5%9F%BA%E6%9C%AC%E8%AF%B4%E6%98%8E"><span class="nav-number">4.</span> <span class="nav-text">4. pyproject.toml 配置文件的基本说明</span></a></li></ol></div></div><div class="site-overview-wrap sidebar-panel"><div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person"><img class="site-author-image" itemprop="image" alt="小梦同学" src="https://halliday.oss-cn-nanjing.aliyuncs.com/imagesbjx.png"><p class="site-author-name" itemprop="name">小梦同学</p><div class="site-description" itemprop="description">欲买桂花同载酒,终不似,少年游。</div></div><div class="site-state-wrap animated"><nav class="site-state"><div class="site-state-item site-state-posts"><a href="/archives/"><span class="site-state-item-count">56</span> <span class="site-state-item-name">日志</span></a></div><div class="site-state-item site-state-tags"><a href="/tags/"><span class="site-state-item-count">63</span> <span class="site-state-item-name">标签</span></a></div></nav></div><div class="links-of-author animated"><span class="links-of-author-item"><a href="https://github.com/M-HALLIDAY" title="GitHub → https://github.com/M-HALLIDAY" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i></a> </span><span class="links-of-author-item"><a href="mailto:halliday2023@163.com" title="E-Mail → mailto:halliday2023@163.com" rel="noopener me" target="_blank"><i class="fa fa-envelope fa-fw"></i></a> </span><span class="links-of-author-item"><a href="/atom.xml" title="RSS → /atom.xml" rel="noopener me"><i class="fa fa-rss-square fa-fw"></i></a></span></div><div class="cc-license animated" itemprop="license"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-CN" class="cc-opacity" rel="noopener" target="_blank"><img src="https://cdnjs.cloudflare.com/ajax/libs/creativecommons-vocabulary/2020.11.3/assets/license_badges/small/by_nc_sa.svg" alt="Creative Commons"></a></div></div></div></div></aside></div><div class="main-inner post posts-expand"><div class="post-block"><article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN"><link itemprop="mainEntityOfPage" href="http://kiki.kim/2021/01/23/%E4%BD%BF%E7%94%A8poetry%E6%9D%A5%E7%AE%A1%E7%90%86python%E9%A1%B9%E7%9B%AE/"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="image" content="https://halliday.oss-cn-nanjing.aliyuncs.com/imagesbjx.png"><meta itemprop="name" content="小梦同学"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="随言碎语"><meta itemprop="description" content="欲买桂花同载酒,终不似,少年游。"></span><span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork"><meta itemprop="name" content="使用poetry来管理python项目 | 随言碎语"><meta itemprop="description" content=""></span><header class="post-header"><h1 class="post-title" itemprop="name headline">使用poetry来管理python项目</h1><div class="post-meta-container"><div class="post-meta"><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-calendar"></i> </span><span class="post-meta-item-text">发表于</span> <time title="创建时间:2021-01-23 10:25:34" itemprop="dateCreated datePublished" datetime="2021-01-23T10:25:34+08:00">2021-01-23</time> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-calendar-check"></i> </span><span class="post-meta-item-text">更新于</span> <time title="修改时间:2023-05-11 12:30:22" itemprop="dateModified" datetime="2023-05-11T12:30:22+08:00">2023-05-11</time> </span><span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv"><span class="post-meta-item-icon"><i class="far fa-eye"></i> </span><span class="post-meta-item-text">阅读次数:</span> <span id="busuanzi_value_page_pv"></span> </span><span class="post-meta-break"></span> <span class="post-meta-item" title="本文字数"><span class="post-meta-item-icon"><i class="far fa-file-word"></i> </span><span class="post-meta-item-text">本文字数:</span> <span>4.3k</span> </span><span class="post-meta-item" title="阅读时长"><span class="post-meta-item-icon"><i class="far fa-clock"></i> </span><span class="post-meta-item-text">阅读时长 ≈</span> <span>8 分钟</span></span></div></div></header><div class="post-body" itemprop="articleBody"><p>使用poetry来管理python项目,快速的构建开发环境,快速的发布,以及进行第三方库的依赖管理。</p><span id="more"></span><h2 id="1-背景">1.背景</h2><p>Poetry 是一个Python 中的好用的包管理工具。在 Python 中,打包系统和依赖管理非常复杂:一个项目经常要同时创建多个文件,例如:</p><ul><li><code>setup.py</code></li><li><code>requirements.txt</code></li><li><code>setup.cfg</code></li><li><code>MANIFEST.in</code></li><li><code>Pipfile</code></li></ul><p>基于此, poetry 将所有的配置都放置在一个 toml 文件中,包括:依赖管理、构建、打包、发布等,可谓是简单方便。</p><h2 id="2-安装">2. 安装</h2><ol><li><p>环境:windows10 + Python 3.8.10 + Python 2.7</p><ul><li>注:如何并存多个版本的python,见之前的文章 <a href="/2020/10/08/windows%E4%B8%8A%E5%90%8C%E6%97%B6%E5%AE%89%E8%A3%85%E5%A4%9A%E4%B8%AA%E7%89%88%E6%9C%AC%E7%9A%84python/" title="windows上同时安装多个版本的python">windows上同时安装多个版本的python</a></li></ul></li><li><p>pipx 或者 pip 进行安装:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">pipx install poetry ---推荐</span><br><span class="line">pip install poetry</span><br></pre></td></tr></table></figure><ul><li>注: pipx的安装与介绍见之前的文章 <a href="/2021/04/19/%E4%BD%BF%E7%94%A8pipx%E6%9D%A5%E8%BF%90%E8%A1%8C%E7%8B%AC%E7%AB%8B%E7%9A%84python%E5%BA%94%E7%94%A8/" title="使用pipx来运行独立的python应用">使用pipx来运行独立的python应用</a></li></ul></li><li><p>配置<strong>全局</strong>环境变量(可选,只是为更好的使用)</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">打开cmd窗口</span><br><span class="line"></span><br><span class="line">1.查看当前的poetry环境变量:</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">poetry config --list</span></span><br><span class="line"></span><br><span class="line">cache-dir = "C:\\Users\\zmr\\AppData\\Local\\pypoetry\\Cache"</span><br><span class="line">experimental.new-installer = true</span><br><span class="line">installer.parallel = true</span><br><span class="line">virtualenvs.create = true</span><br><span class="line">virtualenvs.in-project = false</span><br><span class="line">virtualenvs.path = "{cache-dir}\\virtualenvs" # C:\\Users\\zmr\\AppData\\Local\\pypoetry\\Cache\\virtualenvs</span><br></pre></td></tr></table></figure><table><thead><tr><th>配置项</th><th>说明</th></tr></thead><tbody><tr><td>cache-dir</td><td>poetry使用的缓存目录的路径 默认值: macOS: <strong>~/Library/Caches/pypoetry</strong> Windows: <strong>C:\Users< username >\Appdata\Local\pypoetry\Cache</strong> Unix: <strong>~/.cache/pypoetry</strong></td></tr><tr><td>virtualenvs.create</td><td>默认值为true,如果执行 <strong>poetry install</strong>/<strong>poetry add</strong>时没有虚拟环境,就自动创建一个虚拟环境,设置为false的话,当虚拟环境不存在时,会将包安装到系统环境</td></tr><tr><td>virtualenvs.in-project</td><td>默认值为false,设置为true的话,会在当前项目目录下创建虚拟环境</td></tr><tr><td>virtualenvs.path</td><td>虚拟环境的路径,默认路径 <strong>{cache-dir}\virtualenvs</strong></td></tr></tbody></table><p>根据需求做以下的调整</p><figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">1. 更改缓存目录到其他盘,避免C盘占用</span><br><span class="line"><span class="built_in">$</span> poetry config cache-dir E:<span class="keyword">\TEMP</span><span class="keyword">\cache</span><span class="keyword">\poetry</span><span class="keyword">\cache</span></span><br><span class="line"></span><br><span class="line">2.更改为在当前位置创建虚拟环境,更容易管理</span><br><span class="line"><span class="built_in">$</span> poetry config virtualenvs.in-project true</span><br></pre></td></tr></table></figure></li><li><p>确认更改是生效的</p><figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">$</span> poetry config --list</span><br><span class="line"></span><br><span class="line">cache-dir = "E:<span class="keyword">\\</span>TEMP<span class="keyword">\\</span>cache<span class="keyword">\\</span>poetry<span class="keyword">\\</span>cache"</span><br><span class="line">experimental.new-installer = true</span><br><span class="line">installer.parallel = true</span><br><span class="line">virtualenvs.create = true</span><br><span class="line">virtualenvs.in-project = true</span><br><span class="line">virtualenvs.path = "{cache-dir}<span class="keyword">\\</span>virtualenvs" <span class="params">#</span> E:<span class="keyword">\TEMP</span><span class="keyword">\cache</span><span class="keyword">\poetry</span><span class="keyword">\cache</span><span class="keyword">\virtualenvs</span></span><br></pre></td></tr></table></figure></li></ol><h2 id="3-基本使用">3. 基本使用</h2><h3 id="3-1-添加poetry管理">3.1 添加poetry管理</h3><ol><li><p>已有的项目,直接在当前项目的根目录执行<code>poetry init</code>即能根据你回答的问题生成<code>pyproject.toml</code>文件</p><figure class="highlight toml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">[tool.poetry]</span></span><br><span class="line"><span class="attr">name</span> = <span class="string">"demo"</span></span><br><span class="line"><span class="attr">version</span> = <span class="string">"0.1.0"</span></span><br><span class="line"><span class="attr">description</span> = <span class="string">""</span></span><br><span class="line"><span class="attr">authors</span> = [<span class="string">"xxx <xxx@126.com>"</span>]</span><br><span class="line"></span><br><span class="line"><span class="section">[tool.poetry.dependencies]</span></span><br><span class="line"><span class="attr">python</span> = <span class="string">"^3.8"</span></span><br><span class="line"></span><br><span class="line"><span class="section">[tool.poetry.dev-dependencies]</span></span><br><span class="line"></span><br><span class="line"><span class="section">[build-system]</span></span><br><span class="line"><span class="attr">requires</span> = [<span class="string">"poetry-core>=1.0.0"</span>]</span><br><span class="line"><span class="attr">build-backend</span> = <span class="string">"poetry.core.masonry.api"</span></span><br><span class="line"></span><br></pre></td></tr></table></figure></li><li><p>如果你想创建一个新的 Python 项目,使用 <code>poetry new <文件夹名称></code>命令可以创建一个项目模板:</p><figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">$</span> poetry new foo</span><br><span class="line"></span><br><span class="line">这会创建一个这样的项目结构:</span><br><span class="line">foo</span><br><span class="line">├── pyproject.toml</span><br><span class="line">├── README.rst</span><br><span class="line">├── foo</span><br><span class="line">│ └── <span class="built_in">_</span><span class="built_in">_</span>init<span class="built_in">_</span><span class="built_in">_</span>.py</span><br><span class="line">└── tests</span><br><span class="line"> ├── <span class="built_in">_</span><span class="built_in">_</span>init<span class="built_in">_</span><span class="built_in">_</span>.py</span><br><span class="line"> └── test<span class="built_in">_</span>foo.py</span><br></pre></td></tr></table></figure></li></ol><ul><li>注:如果你想使用 src 文件夹,可以添加 <code>--src</code>选项,这会把程序包嵌套在 src 文件夹里。</li></ul><h3 id="3-2-创建虚拟环境">3.2 创建虚拟环境</h3><ol><li><p>使用 <code>poetry install</code>命令创建虚拟环境(确保当前目录有 pyproject.toml 文件):</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">poetry install</span><br></pre></td></tr></table></figure><p>如果系统中既有Python2又有Python3,且项目既支持Python2也支持Python3,那poetry会优先使用Python2。</p></li><li><p>利用 <code>poetry env use</code>创建</p><p>这个命令,可以指定创建虚拟环境时使用的Python解释器版本</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">poetry <span class="built_in">env</span> use python2</span></span><br><span class="line"></span><br><span class="line">查看当前的环境信息</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">poetry <span class="built_in">env</span> info</span></span><br><span class="line"></span><br><span class="line">Virtualenv</span><br><span class="line">Python: 2.7.10</span><br><span class="line">Implementation: CPython</span><br><span class="line">Path: E:\git-code\behave-db\.venv</span><br><span class="line">Valid: True</span><br><span class="line"></span><br><span class="line">System</span><br><span class="line">Platform: win32</span><br><span class="line">OS: nt</span><br><span class="line">Python: d:\program files\python27</span><br></pre></td></tr></table></figure></li></ol><h3 id="3-3-安装包、卸载包">3.3 安装包、卸载包</h3><p>使用 poetry add 命令来安装一个包:</p><figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">安装包</span><br><span class="line"><span class="built_in">$</span> poetry add flask</span><br><span class="line"></span><br><span class="line">执行完成后,会自动加到 pyproject.toml 中,下次直接install的时候就自动包含了flask</span><br><span class="line"></span><br><span class="line">卸载包</span><br><span class="line"><span class="built_in">$</span> poetry remove foo</span><br><span class="line"></span><br><span class="line">会将其从 pyproject.toml 中移除</span><br></pre></td></tr></table></figure><h3 id="3-4-锁定依赖">3.4 锁定依赖</h3><figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">$</span> poetry lock</span><br><span class="line"></span><br><span class="line">会锁定当前使用的所有依赖的版本,生成一个poetry.lock</span><br></pre></td></tr></table></figure><h3 id="3-4-激活虚拟环境">3.4 激活虚拟环境</h3><figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">poetry shell</span><br></pre></td></tr></table></figure><h3 id="3-5-删除环境">3.5 删除环境</h3><figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">poetry env remove python3.7</span><br></pre></td></tr></table></figure><h3 id="3-6-build:构建">3.6 build:构建</h3><figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">$</span> poetry build</span><br><span class="line"></span><br><span class="line">会打包当前的py项目,并放到dist目录中</span><br></pre></td></tr></table></figure><h3 id="3-7-publish:发布到仓库">3.7 publish:发布到仓库</h3><figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">将使用build命令生成的包发布到远程仓库,默认上传至PYPI</span><br><span class="line"></span><br><span class="line"><span class="built_in">$</span> poetry publish</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="params">#</span> 需要输入PYPI的用户名和密码</span><br><span class="line">Username: xxxx</span><br><span class="line">Password: </span><br><span class="line">Publishing py-sanic-samples (0.1.0) to PyPI</span><br><span class="line"> - Uploading py-sanic-samples-0.1.0.tar.gz 100<span class="comment">%</span></span><br><span class="line"> - Uploading py<span class="built_in">_</span>sanic<span class="built_in">_</span>samples-0.1.0-py3-none-any.whl 100<span class="comment">%</span></span><br><span class="line"> </span><br></pre></td></tr></table></figure><h2 id="4-pyproject-toml-配置文件的基本说明">4. pyproject.toml 配置文件的基本说明</h2><figure class="highlight toml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 设置 PyPI 镜像源,默认是走的pip的pip.ini设置的来源</span></span><br><span class="line"><span class="section">[[tool.poetry.source]]</span></span><br><span class="line"><span class="attr">name</span> = <span class="string">"douban"</span></span><br><span class="line"><span class="attr">url</span> = <span class="string">"https://pypi.doubanio.com/simple/"</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="section">[tool.poetry.dependencies]</span></span><br><span class="line"><span class="comment"># 使用git仓库:</span></span><br><span class="line"><span class="attr">requests</span> = { git = <span class="string">"https://github.com/requests/requests.git"</span> }</span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用本地路径:</span></span><br><span class="line"><span class="comment"># directory</span></span><br><span class="line"><span class="attr">my-package</span> = { path = <span class="string">"../my-package/"</span>, develop = <span class="literal">false</span> }</span><br><span class="line"></span><br><span class="line"><span class="comment"># file</span></span><br><span class="line"><span class="attr">my-package</span> = { path = <span class="string">"../my-package/dist/my-package-0.1.0.tar.gz"</span> }</span><br><span class="line"></span><br><span class="line"><span class="comment"># URL</span></span><br><span class="line"><span class="attr">my-package</span> = { url = <span class="string">"https://example.com/my-package-0.1.0.tar.gz"</span> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="section">[tool.poetry.dependencies]</span></span><br><span class="line"><span class="comment"># python限制:</span></span><br><span class="line"><span class="attr">pathlib2</span> = { version = <span class="string">"^2.2"</span>, python = <span class="string">"~2.7"</span> }</span><br><span class="line"></span><br><span class="line"><span class="comment"># 指定版本和运行环境</span></span><br><span class="line"><span class="attr">pathlib2</span> = { version = <span class="string">"^2.2"</span>, markers = <span class="string">"python_version ~= '2.7' or sys_platform == 'win32'"</span> }</span><br><span class="line"></span><br><span class="line"><span class="comment"># 根据py版本来选择版本</span></span><br><span class="line"><span class="attr">foo</span> = [</span><br><span class="line"> {version = <span class="string">"<=1.9"</span>, python = <span class="string">"^2.7"</span>},</span><br><span class="line"> {version = <span class="string">"^2.0"</span>, python = <span class="string">"^3.4"</span>}</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 如果限制很多,也可单独写一个模块</span></span><br><span class="line"><span class="section">[tool.poetry.group.dev.dependencies.black]</span></span><br><span class="line"><span class="attr">version</span> = <span class="string">"19.10b0"</span></span><br><span class="line"><span class="attr">allow-prereleases</span> = <span class="literal">true</span></span><br><span class="line"><span class="attr">python</span> = <span class="string">"^3.6"</span></span><br><span class="line"><span class="attr">markers</span> = <span class="string">"platform_python_implementation == 'CPython'"</span></span><br><span class="line"></span><br></pre></td></tr></table></figure></div><footer class="post-footer"><div><div style="text-align:center;color:#ccc;font-size:24px;padding-top:10px">----- The End -----</div><div><center><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/css/share.min.css"><div class="social-share" data-sites="weibo, qq, qzone,wechat"></div><script src="http://apps.bdimg.com/libs/jquery/1.8.2/jquery.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/js/social-share.min.js"></script></center></div></div><div class="post-copyright"><ul><li class="post-copyright-author"><strong>本文作者: </strong>小梦同学</li><li class="post-copyright-link"><strong>本文链接:</strong> <a href="http://kiki.kim/2021/01/23/%E4%BD%BF%E7%94%A8poetry%E6%9D%A5%E7%AE%A1%E7%90%86python%E9%A1%B9%E7%9B%AE/" title="使用poetry来管理python项目">http://kiki.kim/2021/01/23/使用poetry来管理python项目/</a></li><li class="post-copyright-license"><strong>版权声明: </strong>本博客所有文章除特别声明外,均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-CN" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处!</li></ul></div><div class="post-tags"><a href="/tags/python/" rel="tag"><i class="fa fa-tag"></i> python</a> <a href="/tags/poetry/" rel="tag"><i class="fa fa-tag"></i> poetry</a></div><script type="text/javascript">for(var tagsall=document.getElementsByClassName("post-tags"),i=tagsall.length-1;0<=i;i--)for(var tags=tagsall[i].getElementsByTagName("a"),j=tags.length-1;0<=j;j--){var r,g,b,golden_ratio=.618033988749895,s=.5,v=.999,h=golden_ratio+.8*Math.random()-.5,h_i=parseInt(6*h),f=6*h-h_i,p=v*(1-s),q=v*(1-f*s),t=v*(1-(1-f)*s);switch(h_i){case 0:r=v,g=t,b=p;break;case 1:r=q,g=v,b=p;break;case 2:r=p,g=v,b=t;break;case 3:r=p,g=q,b=v;break;case 4:r=t,g=p,b=v;break;case 5:r=v,g=p,b=q;break;default:b=g=r=1}tags[j].style.background="rgba("+parseInt(255*r)+","+parseInt(255*g)+","+parseInt(255*b)+",0.5)"}</script><div class="post-nav"><div class="post-nav-item"><a href="/2020/10/08/windows%E4%B8%8A%E5%90%8C%E6%97%B6%E5%AE%89%E8%A3%85%E5%A4%9A%E4%B8%AA%E7%89%88%E6%9C%AC%E7%9A%84python/" rel="prev" title="windows上同时安装多个版本的python"><i class="fa fa-chevron-left"></i> windows上同时安装多个版本的python</a></div><div class="post-nav-item"><a href="/2021/02/20/pip%E6%BA%90%E5%88%87%E6%8D%A2%E4%B8%BA%E5%9B%BD%E5%86%85%E6%BA%90/" rel="next" title="pip源切换为国内源">pip源切换为国内源 <i class="fa fa-chevron-right"></i></a></div></div></footer></article></div></div></main><footer class="footer"><div class="footer-inner"><div class="copyright">© <span itemprop="copyrightYear">2023</span> <span class="with-love"><i class="fa fa-heart"></i> </span><span class="author" itemprop="copyrightHolder">小梦同学</span></div><div class="beian"><img src="https://www.beian.gov.cn/img/new/gongan.png" alt=""><a href="https://www.beian.gov.cn//portal/registerSystemInfo?recordcode=%E7%94%B3%E8%AF%B7%E4%B8%AD" rel="noopener" target="_blank">申请中 </a> <a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">苏ICP备2021047152号-1</a></div><div class="busuanzi-count"><span class="post-meta-item" id="busuanzi_container_site_uv"><span class="post-meta-item-icon"><i class="fa fa-user"></i> </span><span class="site-uv" title="总访客量"><span id="busuanzi_value_site_uv"></span> </span></span><span class="post-meta-item" id="busuanzi_container_site_pv"><span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="site-pv" title="总访问量"><span id="busuanzi_value_site_pv"></span></span></span></div><div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动</div><span id="timeDate">载入天数...</span> <span id="times">载入时分秒...</span><script>var now=new Date;function createtime(){var n=new Date("03/01/2023 10:00:00");now.setTime(now.getTime()+250),days=(now-n)/1e3/60/60/24,dnum=Math.floor(days),hours=(now-n)/1e3/60/60-24*dnum,hnum=Math.floor(hours),1==String(hnum).length&&(hnum="0"+hnum),minutes=(now-n)/1e3/60-1440*dnum-60*hnum,mnum=Math.floor(minutes),1==String(mnum).length&&(mnum="0"+mnum),seconds=(now-n)/1e3-86400*dnum-3600*hnum-60*mnum,snum=Math.round(seconds),1==String(snum).length&&(snum="0"+snum),document.getElementById("timeDate").innerHTML="本站已安全运行 "+dnum+" 天 ",document.getElementById("times").innerHTML=hnum+" 小时 "+mnum+" 分 "+snum+" 秒."}setInterval("createtime()",250)</script></div></footer><div class="back-to-top" role="button" aria-label="返回顶部"><i class="fa fa-arrow-up fa-lg"></i> <span>0%</span></div><div class="reading-progress-bar"></div><a href="https://github.com/M-HALLIDAY" class="github-corner" title="在 GitHub 上关注我" aria-label="在 GitHub 上关注我" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin:130px 106px" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><noscript><div class="noscript-warning">Theme NexT works best with JavaScript enabled</div></noscript><script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.4/jquery.min.js" integrity="sha256-oP6HI9z1XaZNBrJURtCoUT5SUnxFr8s3BzRl+cbzUq8=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js" integrity="sha256-yt2kYMy0w8AbtF89WXb2P1rfjcP/HTHLT7097U8Y5b8=" crossorigin="anonymous"></script><script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/next-boot.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script><script src="/js/third-party/search/local-search.js"></script><script src="/js/third-party/fancybox.js"></script><script src="/js/third-party/pace.js"></script><script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><script src="https://unpkg.com/darkmode-js@1.5.7/lib/darkmode-js.min.js"></script><script>var options = {
|
||
bottom: '64px',
|
||
right: 'unset',
|
||
left: '32px',
|
||
time: '0.5s',
|
||
mixColor: 'transparent',
|
||
backgroundColor: 'transparent',
|
||
buttonColorDark: '#100f2c',
|
||
buttonColorLight: '#fff',
|
||
saveInCookies: true,
|
||
label: '🌓',
|
||
autoMatchOsTheme: true
|
||
}
|
||
const darkmode = new Darkmode(options);
|
||
window.darkmode = darkmode;
|
||
darkmode.showWidget();</script><script async src="/js/cursor/love.min.js"></script><script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script></body></html> |