© 2024 borui. All rights reserved.
This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article:
borui(2024-02-28 01:11:26 +0000). requirements.txt的使用以及虚拟环境. https://borui/blog/2024-02-28-zh-requirment-txt.
@misc{
borui2024,
author = {borui},
title = {requirements.txt的使用以及虚拟环境},
year = {2024},
publisher = {borui's blog},
journal = {borui's blog},
url={https://borui/blog/2024-02-28-zh-requirment-txt}
}
生成requirments.txt
使用pip freeze命令生成requirements.txt文件,如下所示
pip freeze > requirements.txt
- 梦想橡皮擦. (29 Mar, 2022.). python requirements.txt 文件详细说明,一篇就够了. 51CTO. [Blog post]. Retrieved from https://blog.51cto.com/cnca/5155918
requirements文件使用
pip install -r requirements.txt 的作用是从一个文本文件 requirements.txt 中安装所需的 Python 包。一般情况下,项目提供者会在 requirements.txt 文件中列出所有项目所依赖的 Python 包及其版本号,使用这个命令可以方便地安装这些依赖并满足项目运行的需要。
具体地,pip install 是 Python 模块管理器 pip 提供的安装命令,-r requirements.txt 表示从 requirements.txt 文件中读取依赖信息进行安装。这个命令会自动安装依赖的 Python 包,如果依赖的包已经安装了,或者是已经安装了版本符合要求的包,则不会进行重复安装。
在使用 pip install 命令时遇到了速度缓慢的问题,可以使用国内镜像源,例如阿里云镜像,以加快下载速度。可以使用以下命令更改镜像源:
pip install -i https://mirrors.aliyun.com/pypi/simple/ [package_name]
虚拟环境venv
有时候在安装依赖的过程中可能会遇到一些问题,例如与已有环境中的包产生版本冲突或依赖不兼容等。此时可以考虑在虚拟环境中使用 pip install -r requirements.txt 命令,这样可以避免破坏本地环境中的包依赖关系。虚拟环境是一个独立的 Python 环境,安装的包和依赖与本地环境相互独立,不会相互影响。 在虚拟环境中使用 pip install -r requirements.txt 命令与本地环境中的使用方式基本相同,但需要首先激活虚拟环境,以便将要安装的 Python 包和依赖关系与虚拟环境相互独立,避免与本地环境中已有的包产生冲突。
以下是在 Windows 平台下使用虚拟环境的步骤:
创建虚拟环境。在命令行中运行以下命令:
python -m venv myenv
其中 myenv 是虚拟环境的名称,你可以自定义虚拟环境的名称。
激活虚拟环境。在命令行中运行以下命令:
myenv\Scripts\activate.bat
激活虚拟环境之后,命令行提示符会出现环境的名称。
进入项目目录。在命令行中运行以下命令:
cd project_directory
# 其中 project_directory 是你的项目目录路径。
# 安装项目依赖。在命令行中运行以下命令:
pip install -r requirements.txt
# requirements.txt 是包含项目所需的依赖包及其版本信息的文本文件。
运行该命令会自动下载并安装所需的依赖包及其依赖。安装结束后,你的项目即可运行。
退出虚拟环境。在命令行中运行以下命令即可退出虚拟环境:
deactivate
# 这样就可以退出虚拟环境,并返回到本地环境中。
在类 Unix 系统(例如 macOS、Linux 等)中,使用虚拟环境的方法与上述步骤类似,区别在于激活虚拟环境的命令不同:
# 要激活虚拟环境,使用以下命令:
source myenv/bin/activate
# 要退出虚拟环境,直接输入 deactivate 命令即可。
- czhust. (30 May, 2023.). pip 安装 requirements.txt. 知乎专栏. [Blog post]. Retrieved from https://zhuanlan.zhihu.com/p/633335780
vscode使用虚拟环境
全局环境: 默认情况下,安装的任何 Python 解释器都在其自己的全局环境中运行。例如,如果您只是在新终端运行python、python3、 或py(取决于您安装 Python 的方式),那么您正在该解释器的全局环境中运行。您安装或卸载的任何软件包都会影响全局环境以及您在其中运行的所有程序。
💡 Tip: 在 Python 中,最佳实践是创建特定于工作区的环境,例如使用本地环境。
本地环境: 您可以为工作区创建两种类型的环境:virtual和conda。这些环境允许您在不影响其他环境的情况下安装软件包,从而隔离工作区的软件包安装。
英文原文
Global environments: By default, any Python interpreter installed runs in its own global environment. For example, if you just run python, python3, or py at a new terminal (depending on how you installed Python), you're running in that interpreter's global environment. Any packages that you install or uninstall affect the global environment and all programs that you run within it.
💡 Tip: In Python, it is best practice to create a workspace-specific environment, for example, by using a local environment.
Local environments: There are two types of environments that you can create for your workspace: virtual and conda. These environments allow you to install packages without affecting other environments, isolating your workspace's package installations.
Python environment tools
The following table lists the various tools involved with Python environments: | Tool | Definition and Purpose | |---|---| | pip | The Python package manager that installs and updates packages. It's installed with Python 3.9+ by default (unless you are on a Debian-based OS;install python3-pip in that case). | | venv | Allows you to manage separate package installations for different projects and is installed with Python 3 by default (unless you are on a Debian-based OS; install python3-venv in that case) | | conda | Installed with Miniconda. It can be used to manage both packages and virtual environments. Generally used for data science projects. |
Create a virtual environment in the terminal
If you choose to create a virtual environment manually, use the following command (where ".venv" is the name of the environment folder):
# macOS/Linux
# You may need to run `sudo apt-get install python3-venv` first on Debian-based OSs
python3 -m venv .venv
# Windows
# You can also use `py -3 -m venv .venv`
python -m venv .venv
📓 Note: 一般虚拟环境目录创建在当前工作文件夹(workspace)下,vscode的python插件也是最优先匹配当前工作文件夹下的虚拟环境目录
How the extension chooses an environment automatically
If an interpreter hasn't been specified, then the Python extension automatically selects the interpreter with the highest version in the following priority order:
Virtual environments located directly under the workspace folder. Virtual environments related to the workspace but stored globally. For example, Pipenv or Poetry environments that are located outside of the workspace folder.
Globally installed interpreters. For example, the ones found in /usr/local/bin, C:\python38, etc. Note: The interpreter selected may differ from what python refers to in your terminal.
If Visual Studio Code doesn't locate your interpreter automatically, you can manually specify an interpreter.
- Python environments in VS Code. (Dec 6, 2023.). vscode docs. Retrieved Feb 28, 2024, from https://code.visualstudio.com/docs/python/environments#_where-the-extension-looks-for-environments