1 前言
在 Python 中,虚拟环境(Virtual Environment)是一个独立的 Python 环境,它能够与系统全局环境隔离,用于管理项目所需的依赖和 Python 版本。
2 创建虚拟环境
在 macOS/Linux 上:
python3.6 -m venv venv
python3.8 -m venv venv
python10 -m venv venv
在 Windows 上:
python3.8 -m venv venv
python10 -m venv venv
3 激活虚拟环境:
在 macOS/Linux 上:
source venv/bin/activate
在 Windows 上:
venv\Scripts\activate
激活虚拟环境后,您将在命令行提示符中看到环境名称(例如,(venv))
4 退出虚拟环境
现在,您已经成功创建并激活了虚拟环境。您可以在该环境中安装和管理特定于该项目的Python包。如果您完成了项目工作或希望退出虚拟环境,只需在命令行中运行以下命令:
deactivate
这将退出虚拟环境并返回到系统默认的Python环境。
5 更新pip
更新pip
# https://www.couragesteak.com/article/541
python3.10 -m pip install --upgrade pip
pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
python3.8 -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
6 安装依赖
pip3.8 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip10 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple
<h2><a id="1__0"></a>1 前言</h2>
<p>在 Python 中,虚拟环境(Virtual Environment)是一个独立的 Python 环境,它能够与系统全局环境隔离,用于管理项目所需的依赖和 Python 版本。</p>
<h2><a id="2__4"></a>2 创建虚拟环境</h2>
<p>在 macOS/Linux 上:</p>
<pre><div class="hljs"><code class="lang-shell">python3.6 -m venv venv
python3.8 -m venv venv
python10 -m venv venv
</code></div></pre>
<p>在 Windows 上:</p>
<pre><div class="hljs"><code class="lang-shell">python3.8 -m venv venv
python10 -m venv venv
</code></div></pre>
<h2><a id="3__21"></a>3 激活虚拟环境:</h2>
<p>在 macOS/Linux 上:</p>
<pre><div class="hljs"><code class="lang-shell">source venv/bin/activate
</code></div></pre>
<p>在 Windows 上:</p>
<pre><div class="hljs"><code class="lang-shell">venv\Scripts\activate
</code></div></pre>
<p>激活虚拟环境后,您将在命令行提示符中看到环境名称(例如,(venv))</p>
<h2><a id="4__37"></a>4 退出虚拟环境</h2>
<p>现在,您已经成功创建并激活了虚拟环境。您可以在该环境中安装和管理特定于该项目的Python包。如果您完成了项目工作或希望退出虚拟环境,只需在命令行中运行以下命令:</p>
<pre><div class="hljs"><code class="lang-shell">deactivate
</code></div></pre>
<p>这将退出虚拟环境并返回到系统默认的Python环境。</p>
<h2><a id="5_pip_47"></a>5 更新pip</h2>
<p>更新pip</p>
<pre><div class="hljs"><code class="lang-shell"><span class="hljs-meta"># </span><span class="language-bash">https://www.couragesteak.com/article/541</span>
python3.10 -m pip install --upgrade pip
pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
python3.8 -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
</code></div></pre>
<h2><a id="6__60"></a>6 安装依赖</h2>
<pre><div class="hljs"><code class="lang-shell">pip3.8 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip10 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple
</code></div></pre>
留言