简体   繁体   English

当 pip 设置为需要 virtualenv 时,如何在 anaconda3 venv 中允许 pip?

[英]How do I allow pip inside anaconda3 venv when pip set to require virtualenv?

I've just rebuilt my mac environment using the tutorials here:我刚刚使用这里的教程重建了我的 mac 环境:

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

I want to require a virtualenv for pip, and have set that by opening:我想为 pip 需要一个 virtualenv,并通过打开来设置它:

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

and adding:并添加:

[install]
require-virtualenv = true

[uninstall]
require-virtualenv = true

Then, I followed a guide to set up jupyter notebooks w/tensorflow, because I am trying to follow a udemy course on machine learning that requires both: https://medium.com/@margaretmz/anaconda-jupyter-notebook-tensorflow-and-keras-b91f381405f8然后,我按照指南设置带有 tensorflow 的 jupyter 笔记本,因为我正在尝试学习机器学习的 udemy 课程,该课程需要两者: https ://medium.com/@margaretmz/anaconda-jupyter-notebook-tensorflow- 和-keras-b91f381405f8

During this tutorial, it mentions that you should use pip install instead of conda install for tensorflow, because the conda package isn't officially supported.在本教程中,它提到您应该为 tensorflow 使用 pip install 而不是 conda install,因为 conda 包不受官方支持。

I can install pip on conda just fine by running:我可以通过运行在 conda 上安装 pip:

conda install pip

But when I try to run:但是当我尝试运行时:

pip3 install tensorflow

I get the error:我得到错误:

"Could not find an activated virtualenv (required)." “找不到激活的 virtualenv(必需)。”

I know why I'm getting this error, I just don't know how to change my code to ALSO accept use of pip & pip3 inside anaconda venvs.我知道为什么会出现这个错误,我只是不知道如何将我的代码更改为也接受在 anaconda venvs 中使用 pip 和 pip3。

My anaconda3 folder is inside my Virtualenvs folder, along with all of my other virtual environments.我的 anaconda3 文件夹与我的所有其他虚拟环境一起位于我的 Virtualenvs 文件夹中。

I've tried temporarily turning off the restriction by defining a new function in ~/.bashrc:我尝试通过在 ~/.bashrc 中定义一个新函数来暂时关闭限制:

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

and using that instead, with no luck, not surprisingly.而是使用它,没有运气,这并不奇怪。

I think the problem may be here, inside my bash_profile:我认为问题可能出在我的 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 <<<

I want to be able to use pip (& pip3, pip2) in both (& only in) anaconda3's activated 'env's and virtualenvs.我希望能够在 anaconda3 的激活 'env' 和 virtualenvs 中(& 仅在)中使用 pip (& pip3, pip2)。

When you conda install pip , a new pip is placed inside your anaconda virtualenv's bin/ directory.当你conda install pip时,一个新的 pip 被放置在你的 anaconda virtualenv 的bin/目录中。 Each pip knows whether/which virtualenv it's inside of, and each pip only installs packages inside its own virtualenv.每个 pip 都知道它是否/哪个 virtualenv 在里面,并且每个 pip 只在自己的 virtualenv 中安装包。 You can run it like /Users/erikhayton/Virtualenvs/anaconda3/bin/pip install tenserflow您可以像/Users/erikhayton/Virtualenvs/anaconda3/bin/pip install tenserflow一样运行它

You can know where pip3 is by running which pip3 .您可以通过运行which pip3来知道pip3在哪里。

When you activate a virtualenv, environment variables in your shell are being modified.当你activate一个 virtualenv 时,你的 shell 中的环境变量会被修改。 The virtualenv's bin/ directory is placed in your PATH . virtualenv 的bin/目录放在你的PATH中。 If you run /Users/erikhayton/Virtualenvs/anaconda3/bin/activate and then which pip3 , you'll see a different path.如果您运行/Users/erikhayton/Virtualenvs/anaconda3/bin/activate然后which pip3 ,您将看到不同的路径。

See also Using Pip to install packages to Anaconda Environment另请参阅使用 Pip 将软件包安装到 Anaconda 环境

Usually when you use virtual environments, you need to activate them first before you can use them.通常当您使用虚拟环境时,您需要先activate它们才能使用它们。 Somewhere along the line, you would have needed to run a command to create your virtual environment:在某个地方,您需要运行一个命令来创建您的虚拟环境:

virtualenv awesome_virtualenv

Then to make it active:然后使其激活:

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

You can create as many virtual environments as you want and install different sets of libraries in each.您可以根据需要创建任意数量的虚拟环境,并在每个环境中安装不同的库集。

The problem is that pip doesn't recognise the conda environment as being not the global environment.问题是pip没有将 conda 环境识别为不是全局环境。 It doesn't look like the pip authors intend to fix this (for good reasons I think btw).看起来pip作者并不打算解决这个问题(我认为有充分的理由)。 On the conda side there seems to be no movement either (considering this github issue that has not seen any movement over the past year).conda方面似乎也没有任何动静(考虑到这个 github 问题在过去一年中没有任何动静)。 So basically, we'll have to do our own scripting :).所以基本上,我们必须自己编写脚本:)。

This means that whenever we activate a conda environment, we either need to make it look like we're also in a virtual environment, or we switch off PIP_REQUIRE_VIRTUALENV .这意味着每当我们激活conda环境时,我们要么需要让它看起来像我们也在虚拟环境中,要么我们关闭PIP_REQUIRE_VIRTUALENV The solution below uses the latter option (but I can imagine the former working just as well).下面的解决方案使用后一个选项(但我可以想象前者也可以工作)。 There is unfortunately no global activate hook in conda , but there are per environment hooks .不幸的是conda中没有全局activate钩子,但每个环境都有钩子 So all we need to do is run the following 2 commands in our environment:所以我们需要做的就是在我们的环境中运行以下 2 个命令:

    echo "export PIP_REQUIRE_VIRTUALENV=false" > "$CONDA_PREFIX/etc/conda/activate.d/dont-require-venv-for-pip.sh"
    echo "export PIP_REQUIRE_VIRTUALENV=true" > "$CONDA_PREFIX/etc/conda/deactivate.d/require-venv-for-pip.sh"

Now whenever we activate this conda environment, PIP_REQUIRE_VIRTUALENV will be set to false , and it will be reset to true as soon as wel deactivate the environment.现在,每当我们激活这个conda环境时, PIP_REQUIRE_VIRTUALENV将被设置为false ,一旦我们停用环境,它将被重置为true

Since we want to (easily) install this is into all our environments, I made a function which I placed in my .zshrc (should work just as well in your .bashrc / bash_profile ).由于我们想(轻松)将它安装到我们所有的环境中,我制作了一个放在我的.zshrc中的函数(应该在你的.bashrc / bash_profile中同样有效)。

function allow_pip_in_conda_environment() {
    # abort if we're not in a conda env (or in the base environment)
    if [[ -z "$CONDA_DEFAULT_ENV"  || "$CONDA_DEFAULT_ENV" == "base" ]]; then
        echo "Should be run from within a conda environment (not base)"
        return
    fi
    ACTIVATE="$CONDA_PREFIX/etc/conda/activate.d/dont-require-venv-for-pip.sh"
    DEACTIVATE="$CONDA_PREFIX/etc/conda/deactivate.d/require-venv-for-pip.sh"

    # abort if either the activate or the deactivate hook already exists in this env
    if [[ -f "$ACTIVATE" || -f "$DEACTIVATE" ]]; then
        echo "This hook is already installed in this conda environment"
        return
    fi

    # write the hooks (create dirs if they don't exist)
    mkdir -p "$(dirname "$ACTIVATE")"
    mkdir -p "$(dirname "$DEACTIVATE")"
    echo "export PIP_REQUIRE_VIRTUALENV=false" > "$ACTIVATE"
    echo "export PIP_REQUIRE_VIRTUALENV=true" > "$DEACTIVATE"

    # switch off PIP_REQUIRE_VIRTUALENV in the current session as well
    export PIP_REQUIRE_VIRTUALENV=false
}

Now every time I run into a dreaded Could not find an activated virtualenv (required).现在每次我遇到一个可怕Could not find an activated virtualenv (required). , all I need to do is run allow_pip_in_conda_environment , and it fixes it in my current session, and forever after in this conda environment. ,我需要做的就是运行allow_pip_in_conda_environment ,它会在我当前的会话中修复它,并且永远在这个conda环境中。

(PS: same code also works with mamba) (PS:同样的代码也适用于曼巴)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM