在决定使用 pip 和 conda 安装包时,了解这两个包管理器之间的主要区别非常重要。以下是细分:
1. 区别
1.1. Package Management System
- 包裹管理系统
Pip: :
Primarily used for Python packages.
主要用于 Python 包。
Installs packages from the Python Package Index (PyPI).
从 Python 包索引 (PyPI) 安装包。
Handles Python packages only; you may need to manage dependencies manually.
仅处理 Python 包;您可能需要手动管理依赖项。
Conda:
A general package manager that can manage packages written in any language (Python, R, Ruby, etc.).
一个通用的包管理器,可以管理以任何语言(Python、R、Ruby 等)编写的包。
Installs packages from the Anaconda repository or other configured channels.
从 Anaconda 存储库或其他配置的通道安装包。
Automatically manages dependencies and environments.
自动管理依赖项和环境。
1.2. Environment Management
Pip: 点:
Requires virtual environments (e.g., using venv or virtualenv) for managing project environments.
需要虚拟环境(例如,使用 venv 或 virtualenv)来管理项目环境。
Doesn’t natively support environment management.
本身不支持环境管理。
Conda:
Built-in environment management feature.
内置环境管理功能。
Allows you to create, export, and manage multiple isolated environments easily.
允许您轻松创建、导出和管理多个隔离环境。
1. 3. Dependency Resolution
- 依赖项解析
Pip: 点:
Performs dependency resolution during installation but may not handle complex dependencies well.
在安装过程中执行依赖项解析,但可能无法很好地处理复杂的依赖项。
Can lead to “dependency hell” if conflicts arise.
如果出现冲突,可能会导致 “依赖地狱”。
Conda: 康达:
Uses a more sophisticated dependency resolver.
使用更复杂的依赖关系解析程序。
Ensures that all dependencies are compatible before installation, reducing conflicts.
安装前确保所有依赖项兼容,减少冲突。
1. 4. Installation Speed
- 安装速度
Pip:
Generally installs packages from source, which can take longer.
通常从源安装包,这可能需要更长的时间。
May require compilation for some packages.
可能需要对某些软件包进行编译。
Conda:
Often installs precompiled binaries, leading to faster installation times.
通常会安装预编译的二进制文件,从而加快安装时间。
Ideal for data science packages that may have complex dependencies.
非常适合可能具有复杂依赖项的数据科学包。
1.5. Use Cases 5.
使用案例
Pip: :
Best suited for standard Python projects where you need packages from PyPI.
最适合需要 PyPI 软件包的标准 Python 项目。
Common in web development and general Python programming.
常见于 Web 开发和通用 Python 编程。
Conda:
Preferred in data science, machine learning, and scientific computing where complex dependencies are common.
在数据科学、机器学习和科学计算中,复杂依赖关系很常见,这是首选。
Useful for users working with multiple languages and environments.
对于使用多种语言和环境的用户非常有用。
Conclusion 结论
Both pip and conda have their strengths. If you’re primarily working with Python and need access to a wide range of Python packages, pip is a good choice. However, if you’re dealing with complex dependencies or require a multi-language environment, conda is often the better option.
pip 和 conda 都有其优势。如果您主要使用 Python 并且需要访问各种 Python 包,pip 是一个不错的选择。但是,如果您正在处理复杂的依赖项或需要多语言环境,conda 通常是更好的选择。
2. 具体包查看
To view an installed package using the conda
command, you can follow these steps:
1. Open Your Terminal or Command Prompt
Make sure you have conda
installed and accessible in your terminal or command prompt.
2. Activate the Desired Environment (if necessary)
If you have multiple environments and the package is installed in a specific one, activate that environment first:
conda activate your_environment_name
3. List Installed Packages
To view all installed packages in the current environment, use:
conda list
pip 对应的命令是 pip list;
This command will display a list of all packages along with their versions.
4. View a Specific Package
If you want to check details about a specific package, you can filter the list or use the following command:
conda list package_name
pip 对应的命令是 pip show package_name;
Replace package_name
with the name of the package you want to view.
5. Get More Information About a Package
To get more detailed information about a specific package, including its dependencies, use:
conda info package_name
This will provide additional details about the package, such as its version, build, and dependencies.
Example
conda activate myenv
conda list numpy
conda info numpy
This will show you the installed version of NumPy in the myenv
environment and provide detailed info about it.
3. install and 卸载
使用 conda 安装时, 先更新自己的conda;
如果您使用的是旧版本的 Conda,它可能没有最新的软件包列表。您可以通过以下方式更新 Conda:
conda update condaconda install librosa youtube-dl sox -c conda-forge
conda install -y pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia
-y:此选项在安装过程中自动对所有提示回答 “yes”。如果没有 -y,conda 将在继续安装软件包之前要求确认。-y 标志确保安装继续进行,无需用户干预。
-c: This option specifies the channels from which to pull the packages. In this case, you have two channels specified:-c:此选项指定要从中提取包的通道。在本例中,您指定了两个通道:
-c pytorch: This tells conda to search and install the packages from the pytorch channel.-c pytorch:这告诉 conda 从 pytorch 通道搜索并安装软件包。
-c nvidia: This tells conda to also search the nvidia channel for packages.-c nvidia:这会告诉 conda 也搜索 nvidia 通道中的软件包。
3.1 install
conda install pytorch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1 cudatoolkit=11.2 -c pytorch
# ROCM 5.2 (Linux only)
pip install torch==1.13.1+rocm5.2 torchvision==0.14.1+rocm5.2 torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/rocm5.2
# CUDA 11.6
pip install torch==1.13.1+cu116 torchvision==0.14.1+cu116 torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cu116
# CUDA 11.7
pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cu117
# CPU only
pip install torch==1.13.1+cpu torchvision==0.14.1+cpu torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cpu
3.2 安装出现包不存在时
Solving environment: unsuccessful initial attempt using frozen solve. Retrying with flexible solve.
PackagesNotFoundError: The following packages are not available from current channels:
- cudatoolkit=11.2
Current channels:
- https://conda.anaconda.org/pytorch/linux-64
- https://conda.anaconda.org/pytorch/noarch
- https://repo.anaconda.com/pkgs/main/linux-64
- https://repo.anaconda.com/pkgs/main/noarch
- https://repo.anaconda.com/pkgs/r/linux-64
- https://repo.anaconda.com/pkgs/r/noarch
It seems that cudatoolkit=11.2
might not be available in the channels you’re using. Let’s try a few steps to resolve this issue.似乎 cudatoolkit=11.2
可能在您使用的频道中不可用。让我们尝试几个步骤来解决此问题。
1. Add the nvidia
Channel1. 添加 nvidia
频道
The cudatoolkit
package, especially older versions, is sometimes available in the nvidia
channel. Let’s add this channel explicitly to ensure compatibility:cudatoolkit
包,尤其是旧版本,有时在 nvidia
频道中可用。让我们显式添加此通道以确保兼容性:
conda config --add channels nvidia
2. **Search for Available Versions of cudatoolkit
**2. 搜索 cudatoolkit
的可用版本
You can search for available versions of cudatoolkit
to make sure 11.2 is available:您可以搜索 cudatoolkit
的可用版本以确保 11.2 可用:
conda search cudatoolkit
This will list all the available versions. If version 11.2 isn’t available, choose the closest supported version (e.g., 11.3 or 11.1) and adjust the installation command.这将列出所有可用版本。如果版本 11.2 不可用,请选择最接近的支持版本(例如 11.3 或 11.1)并调整安装命令。
3. Reinstall PyTorch 1.13.1 with the Correct CUDA Toolkit3. 使用正确的 CUDA 工具包重新安装 PyTorch 1.13.1
After checking the available versions, modify the install command accordingly. Here’s the original command updated with nvidia
and pytorch
channels for installation:检查可用版本后,相应地修改 install 命令。以下是使用 nvidia
和 pytorch
通道更新的原始命令以进行安装:
conda install pytorch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1 cudatoolkit=11.2 -c pytorch -c nvidia
If 11.2
is unavailable, adjust the cudatoolkit
version:如果 11.2
版本不可用,请调整 cudatoolkit
版本:
conda install pytorch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1 cudatoolkit=11.3 -c pytorch -c nvidia
4. Update Conda4. 更新 Conda
If the package is still not found, you might want to update Conda to ensure you have the latest environment information:如果仍未找到包,您可能需要更新 Conda 以确保您拥有最新的环境信息:
conda update conda
5. Verify the Installation5. 验证安装
After installation, verify everything by running:安装后,通过运行以下命令来验证所有内容:
import torch
print(torch.__version__) # Should return '1.13.1'
print(torch.cuda.is_available()) # Should return True
print(torch.version.cuda) # Should return '11.x'
Let me know if this works!让我知道这是否有效!
3.2 uninstall
conda remove package name
pip uninstall package name