【问题标题】:How do I allow pip inside anaconda3 venv when pip set to require virtualenv?当 pip 设置为需要 virtualenv 时,如何在 anaconda3 venv 中允许 pip?
【发布时间】:2019-01-19 03:46:12
【问题描述】:

我刚刚使用这里的教程重建了我的 mac 环境:

https://hackercodex.com/guide/mac-development-configuration/ & 这里:https://hackercodex.com/guide/python-development-environment-on-mac-osx/

我想为 pip 需要一个 virtualenv,并通过打开来设置它:

vim ~/Library/Application\ Support/pip/pip.conf

并添加:

[install]
require-virtualenv = true

[uninstall]
require-virtualenv = true

然后,我按照指南设置带有 tensorflow 的 jupyter 笔记本,因为我正在尝试学习机器学习的 udemy 课程,该课程需要两者:https://medium.com/@margaretmz/anaconda-jupyter-notebook-tensorflow-and-keras-b91f381405f8

在本教程中,它提到您应该为 tensorflow 使用 pip install 而不是 conda install,因为 conda 包不受官方支持。

我可以通过运行在 conda 上安装 pip:

conda install pip

但是当我尝试运行时:

pip3 install tensorflow

我得到错误:

“找不到激活的 virtualenv(必需)。”

我知道为什么会出现此错误,只是不知道如何将代码更改为也接受在 anaconda venvs 中使用 pip 和 pip3。

我的 anaconda3 文件夹与我的所有其他虚拟环境一起位于我的 Virtualenvs 文件夹中。

我尝试通过在 ~/.bashrc 中定义一个新函数来暂时关闭限制:

cpip(){
PIP_REQUIRE_VIRTUALENV="0" pip3 "$@"
}

并使用它来代替,没有运气,不足为奇。

我认为问题可能出在我的 bash_profile 中:

# How to Set Up Mac For Dev:
# https://hackercodex.com/guide/mac-development-configuration/
# Ensure user-installed binaries take precedence
export PATH=/usr/local/bin:$PATH
# Load .bashrc if it exists
test -f ~/.bashrc && source ~/.bashrc


# Activate Bash Completion:
if [ -f $(brew --prefix)/etc/bash_completion ]; then
    source $(brew --prefix)/etc/bash_completion
fi


# Toggle for installing global packages:
gpip(){
   PIP_REQUIRE_VIRTUALENV="0" pip3 "$@"
}
# Toggle for installing conda packages:
cpip(){
   PIP_REQUIRE_VIRTUALENV="0" pip3 "$@"
}
# Be sure to run "source ~/.bash_profile after toggle for changes to
take effect.
# Run "gpip install" (i.e. "gpip install --upgrade pip setuptools
wheel virtualenv")


# added by Anaconda3 2018.12 installer
# >>> conda init >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$(CONDA_REPORT_ERRORS=false
'/Users/erikhayton/Virtualenvs/anaconda3/bin/conda' shell.bash hook
2> /dev/null)"
if [ $? -eq 0 ]; then
    \eval "$__conda_setup"
else
    if [ -f
"/Users/erikhayton/Virtualenvs/anaconda3/etc/profile.d/conda.sh" ];
then
        .
"/Users/erikhayton/Virtualenvs/anaconda3/etc/profile.d/conda.sh"
        CONDA_CHANGEPS1=false conda activate base
    else
        \export
PATH="/Users/erikhayton/Virtualenvs/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda init <<<

我希望能够在 anaconda3 的激活 'env's 中(& 仅在)中使用 pip (& pip3, pip2) 和 virtualenvs。

【问题讨论】:

    标签: python python-3.x anaconda virtualenv conda


    【解决方案1】:

    当你 conda install pip 时,一个新的 pip 被放置在你的 anaconda virtualenv 的 bin/ 目录中。每个 pip 都知道它是否/哪个 virtualenv 在里面,并且每个 pip 只在自己的 virtualenv 中安装包。你可以像/Users/erikhayton/Virtualenvs/anaconda3/bin/pip install tenserflow一样运行它

    您可以通过运行which pip3 来知道pip3 在哪里。

    当你 activate 一个 virtualenv 时,你的 shell 中的环境变量正在被修改。 virtualenv 的bin/ 目录放在你的PATH 中。如果您运行/Users/erikhayton/Virtualenvs/anaconda3/bin/activate,然后运行which pip3,您将看到不同的路径。

    另见Using Pip to install packages to Anaconda Environment

    【讨论】:

      【解决方案2】:

      通常当你使用虚拟环境时,你需要先activate它们才能使用它们。在某个地方,您可能需要运行一个命令来创建您的虚拟环境:

      virtualenv awesome_virtualenv
      

      然后让它激活:

      cd ~/Virtualenvs/awesome_virtualenv
      source bin/activate
      pip3 install tensorflow  # this will install TensorFlow into your awesome_virtualenv
      

      您可以根据需要创建任意数量的虚拟环境,并在每个环境中安装不同的库集。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-14
        • 1970-01-01
        • 2011-06-22
        • 2014-03-16
        • 2016-02-29
        • 2023-03-11
        • 1970-01-01
        • 2020-01-12
        相关资源
        最近更新 更多