pip 与 conda 命令

本文详细对比了pip与conda这两种Python包管理工具的区别。conda在安装包时会默认安装依赖包,而pip则只安装必需的包。此外,还介绍了如何使用pip和conda的各种命令进行包的管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pip与conda

优快云 - anaconda,conda,pip的关系
新浪博客 - Anaconda、Miniconda、Conda、pip的相互关系

使用上的理解:

  1. conda 安装包的时候会将默认依赖的很多包一起安装,如果之前没有这些包就下载,有但版本不合适就更新,如果已安装的包不兼容会将这些包卸载。当安装前会提示是否确认,所以要仔细看下小心卸掉已有的包或者将已有的包降低版本。
    pip 安装的时候依赖项相对于 conda 来说要少很多,只会安装必需的包,可能因此会产生版本不兼容问题,但不会对原有环境做太多变更。
  2. pip 是 python 官方库,包含的 python 包的范围要比 conda 大。

pip : python installer for python

pip -V / --version : 查看pip版本
pip list : 列出所有包
pip show package_name : 查找已安装包的信息
pip search package_name : 搜索 PyPI(Python Package Index) 的包
pip install package_name[==version] : 安装指定的包
pip / pip help / pip -h / pip --help :

Usage:
  pip <command> [options]

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  check                       Verify installed packages have compatible dependencies.
  config                      Manage local and global configuration.
  search                      Search PyPI for packages.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion.
  help                        Show help for commands.

General Options:
  -h, --help                  Show help.
  --isolated                  Run pip in an isolated mode, ignoring environment variables and user configuration.
  -v, --verbose               Give more output. Option is additive, and can be used up to 3 times.
  -V, --version               Show version and exit.
  -q, --quiet                 Give less output. Option is additive, and can be used up to 3 times (corresponding to WARNING, ERROR, and CRITICAL logging levels).
  --log <path>                Path to a verbose appending log.
  --proxy <proxy>             Specify a proxy in the form [user:passwd@]proxy.server:port.
  --retries <retries>         Maximum number of retries each connection should attempt (default 5 times).
  --timeout <sec>             Set the socket timeout (default 15 seconds).
  --exists-action <action>    Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort).
  --trusted-host <hostname>   Mark this host as trusted, even though it does not have valid or any HTTPS.
  --cert <path>               Path to alternate CA bundle.
  --client-cert <path>        Path to SSL client certificate, a single file containing the private key and the certificate in PEM format.
  --cache-dir <dir>           Store the cache data in <dir>.
  --no-cache-dir              Disable the cache.
  --disable-pip-version-check Don't periodically check PyPI to determine whether a new version of pip is available for download. Implied with --no-index.
  --no-color                  Suppress colored output

conda

conda -V / --version : 查看conda版本

conda list : 列出所有包
conda list package_name : 查找已安装包的信息
conda search package_name : 搜索PyPI(Python Package Index)的包
conda install package_name[==version] : 安装指定的包
conda update package_name[==version] : 更新包

conda info -e / --envs || conda env list : 列出所有conda环境
conda create -n env_name package_name[=version] : 创建新环境
conda create -n env_name_new --clone env_name_old : 克隆环境
conda activate env_name : 使用此环境
conda deactivate : 退出此环境
conda remove -n env_name --all : 删除指定环境

conda clean -p : 删除没有用的包,这个命令会检查哪些包没有在包缓存中被硬依赖到其他地方,并删除它们
conda clean -t : tar打包,通过conda clean -t可以将conda保存下来的tar包
conda clean -y -all : 删除所有的安装包及cache

添加镜像:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes

显示配置:

conda config --show
conda config --show-sources

移除镜像:

conda config --remove channels 'https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/' 

conda / conda help / conda -h / conda --help :

usage: conda-script.py [-h] [-V] command ...

conda is a tool for managing and deploying applications, environments and packag
es.

Options:

positional arguments:
  command
    clean        Remove unused packages and caches.
    config       Modify configuration values in .condarc. This is modeled after the git config command. Writes to the user .condarc file (C:\Users\Administrator\.condarc) by default.
    create       Create a new conda environment from a list of specified packages.
    help         Displays a list of available conda commands and their help strings.
    info         Display information about current conda install.
    init         Initialize conda for shell interaction. [Experimental]
    install      Installs a list of packages into a specified conda environment.
    list         List linked packages in a conda environment.
    package      Low-level conda package utility. (EXPERIMENTAL)
    remove       Remove a list of packages from a specified conda environment.
    uninstall    Alias for conda remove.
    run          Run an executable in a conda environment. [Experimental]
    search       Search for packages and display associated information. The input is a MatchSpec, a query language for conda packages. See examples below.
    update       Updates conda packages to the latest compatible version.
    upgrade      Alias for conda update.

optional arguments:
  -h, --help     Show this help message and exit.
  -V, --version  Show the conda version number and exit.

conda commands available from other packages:
  build
  convert
  debug
  develop
  env
  index
  inspect
  metapackage
  render
  server
  skeleton
  verify

conda info -h / --help :

usage: conda-script.py info [-h] [--json] [-v] [-q] [-a] [--base] [-e] [-s] [--unsafe-channels]

Display information about current conda install.

Options:

optional arguments:
  -h, --help         Show this help message and exit.
  -a, --all          Show all information.
  --base             Display base environment path.
  -e, --envs         List all known conda environments.
  -s, --system       List environment variables.
  --unsafe-channels  Display list of channels with tokens exposed.

Output, Prompt, and Flow Control Options:
  --json             Report all output as json. Suitable for using conda programmatically.
  -v, --verbose      Use once for info, twice for debug, three times for trace.
  -q, --quiet        Do not display progress bar.

conda create -h / --help :

usage: conda-script.py create [-h] [--clone ENV] [-n ENVIRONMENT | -p PATH]
                              [-c CHANNEL] [--use-local] [--override-channels]
                              [--strict-channel-priority]
                              [--no-channel-priority]
                              [--no-deps | --only-deps] [--no-pin] [--copy]
                              [--no-shortcuts] [-C] [-k] [--offline] [-d]
                              [--json] [-q] [-v] [-y] [--download-only]
                              [--show-channel-urls] [--file FILE]
                              [--no-default-packages]
                              [package_spec [package_spec ...]]

Create a new conda environment from a list of specified packages. To use the cre
ated environment, use 'source activate envname' look in that directory first.  T
his command requires either the -n NAME or -p PREFIX option.

Options:

positional arguments:
  package_spec          Packages to install or update in the conda environment.

optional arguments:
  -h, --help            Show this help message and exit.
  --clone ENV           Path to (or name of) existing local environment.
  --file FILE           Read package versions from the given file. Repeated file specifications can be passed (e.g. --file=file1 --file=file2).

Target Environment Specification:
  -n ENVIRONMENT, --name ENVIRONMENT Name of environment.
  -p PATH, --prefix PATH Full path to environment location (i.e. prefix).

Channel Customization:
  -c CHANNEL, --channel CHANNEL
                        Additional channel to search for packages. These are URLs searched in the order they are given (including
                        file:// for local directories). Then, the defaults or channels from .condarc are searched (unless
                        --override-channels is given). You can use 'defaults' to get the default packages for conda. You can also
                        use any name and the .condarc channel_alias value will be prepended. The default channel_alias is
                        http://conda.anaconda.org/.
  --use-local           Use locally built packages. Identical to '-c local'.
  --override-channels   Do not search default or .condarc channels. Requires
                        --channel.

Solver Mode Modifiers:
  --strict-channel-priority
                        Packages in lower priority channels are not considered if a package with the same name appears in a higher priority channel.
  --no-channel-priority
                        Package version takes precedence over channel priority. Overrides the value given by `conda config --show channel_priority`.
  --no-deps             Do not install, update, remove, or change dependencies. This WILL lead to broken environments and inconsistent behavior. Use at your own risk.
  --only-deps           Only install dependencies.
  --no-pin              Ignore pinned file.
  --no-default-packages Ignore create_default_packages in the .condarc file.

Package Linking and Install-time Options:
  --copy                Install all packages using copies instead of hard- or soft-linking.
  --no-shortcuts        Don't install start menu shortcuts

Networking Options:
  -C, --use-index-cache Use cache of channel index files, even if it has expired.
  -k, --insecure        Allow conda to perform "insecure" SSL connections and transfers. Equivalent to setting 'ssl_verify' to 'false'.
  --offline             Offline mode. Don't connect to the Internet.

Output, Prompt, and Flow Control Options:
  -d, --dry-run         Only display what would have been done.
  --json                Report all output as json. Suitable for using conda programmatically.
  -q, --quiet           Do not display progress bar.
  -v, --verbose         Can be used multiple times. Once for INFO, twice for DEBUG, three times for TRACE.
  -y, --yes             Do not ask for confirmation.
  --download-only       Solve an environment and ensure package caches are populated, but exit prior to unlinking and linking packages into the prefix.
  --show-channel-urls   Show channel urls. Overrides the value given by `conda config --show show_channel_urls`.

Examples:

    conda create -n myenv sqlite
<think>好的,用户想了解pipconda的区别,以及它们在虚拟环境中的使用是否会影响本机环境。我需要先回顾一下引用的资料,整理出两者的主要区别。 首先,根据引用[2],Conda既是包管理器也是环境管理器,可以创建隔离的环境,每个环境可以有不同版本的Python和包。而pip主要是Python的包安装工具,专注于Python包的管理。 引用[3]提到,为了确保pip安装的包只在虚拟环境中生效,需要在虚拟环境中先安装pip,这样使用虚拟环境内的pip安装的包才会被隔离。这说明如果正确使用虚拟环境中的pip,不会影响本机环境。 引用[4]讨论了pipconda在安装深度学习环境时的区别,可能涉及依赖管理和二进制兼容性。比如conda会处理非Python依赖,而pip可能不会,这可能导致环境问题。 用户还关心环境隔离机制。根据引用[1],conda install和pip install在虚拟环境中安装的包都会被限制在该环境中,删除环境后不会残留。因此两者的安装位置都在当前环境内,不影响本机。 需要比较两者的安装路径、依赖处理、包来源、环境隔离机制。同时要强调正确使用方法,比如在虚拟环境中优先使用conda,必要时用pip,并注意激活环境以避免影响系统。 可能用户担心同时使用condapip是否会导致冲突。根据经验,混用可能导致依赖冲突,但若在同一个环境中谨慎使用,通常可以管理。此外,要提醒用户注意安装顺序,先condapip,减少问题。 最后,生成相关问题要考虑用户可能的后续疑问,比如如何选择工具、混用的风险、环境清理等。</think>### pipconda命令对比及环境隔离机制 #### 一、核心区别 1. **功能定位** - **conda**:既是包管理器(支持Python、C/C++、R等语言的二进制包),也是环境管理器,可创建完全隔离的虚拟环境[^2]。例如: $$ \text{conda create -n env_name python=3.8} $$ - **pip**:仅作为Python包管理器,专注于PyPI仓库的Python包安装,无环境管理功能[^4]。 2. **依赖处理** - conda安装包时会自动解析非Python依赖(如MKL数学库),而pip仅处理Python依赖。 - 示例:安装`numpy`时,conda会连带安装优化后的底层库,而pip可能仅安装纯Python版本。 3. **安装路径** - 在虚拟环境中,`conda install`和`pip install`的包默认均存储在**当前环境目录**下(如`conda/envs/env_name/lib/python3.8/site-packages/`),删除环境后相关包完全清除,不影响本机系统[^1]。 #### 二、环境隔离机制 1. **conda虚拟环境** - 通过`conda create`创建独立环境,环境间完全隔离(包括Python解释器、系统工具链)。 - 激活环境后,`conda install`和`pip install`均受限于当前环境。例如: ```bash conda activate test_env pip install requests # 仅安装到test_env中 ``` 2. **pip系统环境隔离** - 若未激活虚拟环境,全局使用`pip install`会将包安装到系统Python路径,可能污染本机环境[^3]。 - **正确做法**:在虚拟环境中优先使用`conda install`,若包未在conda仓库中,再通过`pip install`安装。 #### 三、典型场景对比 | 场景 | conda | pip | |---------------------|--------------------------------|------------------------------| | 安装Python包 | `conda install numpy` | `pip install numpy` | | 安装非Python依赖 | 自动处理(如CUDA Toolkit) | 不支持 | | 环境复制 | `conda env export > env.yml` | `pip freeze > requirements.txt` |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值