管理 Python 环境

2024/6/22 22:07:00admin0 阅读0 评论

<h2>pyenv 来管理 Python 版本</h2>
<p>pyenv 是一个强大 Python 包管理工具,可以灵活地切换各种 Python 版本,使用 pyenv 来管理我们的 Python 版本,优雅高效且不会破坏掉系统自带的 Python 环境:</p>
<h3>macOS 安装 pyenv</h3>
<pre><code class=“language-Bash”>➜ brew install pyenv</code></pre>
<p>接着为 pyenv 配置 shell 环境,提高工作效率,可自动联想 Tab 补全我们本地安装的 Python 版本:</p>
<pre><code class=“language-Bash”>echo 'eval "$(pyenv init -)"' >> ~/.zshrc</code></pre>
<h3>pyenv 安装 Python</h3>
<pre><code class=“language-Bash”># 查看已经安装的Python版本
➜ pyenv versions

查看当前的 Python 版本

➜ pyenv version

查看可安装的版本

➜ pyenv install -l

安装与卸载 pypy3.8-7.3.11

➜ pyenv install pypy3.8-7.3.11
➜ pyenv uninstall pypy3.8-7.3.11</code></pre>
<p>版本切换确实很方便,所安装的版本都在 <code>~/.pyenv/versions</code> 目录下:</p>
<pre><code class=“language-Bash”># global 全局设置 一般不建议改变全局设置
➜ pyenv global <python版本>

shell 会话设置 只影响当前的shell会话

➜ pyenv shell <python版本>

取消 shell 会话的设置

➜ pyenv shell --unset

local 本地设置 只影响所在文件夹

➜ pyenv local <python版本></code></pre>
<p>pyenv 的 global、local、shell 的优先级关系是:shell > local > global</p>
<h2>Python 的 pip 管理工具</h2>
<p><code>pipenv</code> 是一个强大的工具,用于简化 Python 项目中的依赖管理和虚拟环境管理。以下是一些常见的 <code>pipenv</code> 用法示例:</p>
<p>更多关于 <code>pipenv</code> 的信息,可以查看它的官方文档:</p>
<ul>
<li><a href=“https://pipenv.pypa.io/en/latest/”>pipenv 官方文档</a></li>
</ul>
<h3>安装 <code>pipenv</code></h3>
<p>首先,你需要确保已经安装了 <code>pipenv</code>。你可以通过以下命令安装它:</p>
<pre><code class=“language-Bash”>pip install pipenv</code></pre>
<h3>创建和管理虚拟环境</h3>
<p><strong>创建一个新项目并初始化 </strong><code>pipenv</code><strong> 环境</strong>:</p>
<pre><code class=“language-Bash”>mkdir my_project
cd my_project
pipenv install</code></pre>
<p><strong>安装一个新的包</strong></p>
<p><code>pipenv</code> 会自动创建一个虚拟环境(如果还没有创建),并将包安装到该虚拟环境中。</p>
<pre><code class=“language-Bash”>pipenv install requests</code></pre>
<p><strong>安装一个开发依赖包</strong>:</p>
<p>开发依赖包只在开发环境中需要,比如测试工具。</p>
<pre><code class=“language-Bash”>pipenv install --dev pytest</code></pre>
<p><strong>卸载一个包</strong>:</p>
<pre><code class=“language-Bash”>pipenv uninstall requests</code></pre>
<p><strong>激活虚拟环境</strong>:</p>
<p>激活虚拟环境后,你可以在其中运行 Python 命令和脚本。</p>
<pre><code class=“language-Bash”>pipenv shell</code></pre>
<p>要退出虚拟环境,使用 <code>exit</code> 命令。</p>
<p><strong>运行脚本而不激活虚拟环境</strong>:</p>
<p>如果你不想手动激活虚拟环境,可以使用以下命令直接在虚拟环境中运行脚本。</p>
<pre><code class=“language-Bash”>pipenv run python your_script.py</code></pre>
<h3>管理依赖文件</h3>
<p><strong>生成 <code>Pipfile.lock</code></strong>:
<code>Pipfile.lock</code> 文件记录了所有包的精确版本,保证项目的一致性。</p>
<pre><code class=“language-Bash”>pipenv lock</code></pre>
<p><strong>安装 <code>Pipfile</code> 中的所有依赖</strong>:
如果你克隆了一个包含 <code>Pipfile</code> 的项目,可以使用以下命令安装所有依赖。</p>
<pre><code class=“language-Bash”>pipenv install</code></pre>
<p><strong>检查包的安全性</strong>:
<code>pipenv</code> 提供了一个方便的命令来检查已安装的包是否有已知的安全漏洞。</p>
<pre><code class=“language-Bash”>pipenv check</code></pre>
<h3>其他有用的命令</h3>
<p><strong>查看已安装包列表</strong>:</p>
<pre><code class=“language-Bash”>pipenv graph</code></pre>
<p><strong>更新包</strong>:</p>
<pre><code class=“language-Bash”>pipenv update requests</code></pre>
<p>或更新所有包:</p>
<pre><code class=“language-Bash”>pipenv update</code></pre>
<p><strong>清理未使用的包</strong>:</p>
<pre><code class=“language-Bash”>pipenv clean</code></pre>
<p><strong>查看虚拟环境路径</strong>:</p>
<pre><code class=“language-Bash”>pipenv --venv</code></pre>
<p><strong>查看 Python 解释器路径</strong>:</p>
<pre><code class=“language-Bash”>pipenv --py</code></pre>
<p>这些命令和用法应该可以帮助你更有效地管理 Python 项目的依赖和虚拟环境。如果你需要更多信息和详细的用法,可以查看 <a href=“https://pipenv.pypa.io/en/latest/”>pipenv 的官方文档</a>。</p>
<p>例如:</p>
<pre><code class=“language-Bash”># 安装 pipenv
pip install pipenv

mkdir my_project

cd my_project

初始化 pipenv 环境

pipenv install

安装模块 openpyxl

pipenv install openpyxl

激活虚拟环境,运行python,使用 exit 命令退出环境

pipenv shell

直接在虚拟环境中运行脚本

pipenv run python your_script.py
</code></pre>