pip: Could not find an activated virtualenv (required)

32,480

Solution 1

Open your ~/.bashrc file and see if this line is there -

export PIP_REQUIRE_VIRTUALENV=true

It might be causing the trouble. If it's there, change it to false and run -

source ~/.bashrc

If not, run export PIP_REQUIRE_VIRTUALENV=false from terminal.

Note: everything works the same if you have .bash_profile instead of .bashrc in your current user's root directory.

Solution 2

@Bibhas has it; +1 to look for export PIP_REQUIRE_VIRTUALENV=true in ~/.profile or ~/.bashrc. You can confirm the setting in your current shell with env |grep PIP_REQUIRE_VIRTUALENV.

This setting is a good safety check; more often than not, you'll want to be installing things into virtualenvs. However, sometimes you do want to be working with the global/system python. In those cases, take a look at --isolated:

Run pip in an isolated mode, ignoring environment variables and user configuration.

$ pip install --upgrade pip
Could not find an activated virtualenv (required).
$ pip install --upgrade pip --isolated
Requirement already up-to-date: pip in /usr/local/lib/python2.7/site-packages
$ pip freeze --isolated
...

Solution 3

An additional solution to those already presented is to add a shell command that will allow you to install py packages by temporarily overriding the default setting. Add this to your ~/.profile, ~/.bashrc or wherever you maintain your shell's exports/settings (in my case, ~/.zshrc).

syspip(){
    PIP_REQUIRE_VIRTUALENV="" pip "$@"
}

With this simple addition, you can install pip packages to the system via syspip install <package>.

Solution 4

Verify contents of ~/.pip/pip.conf like:

[global]
index=https://pypi.python.org/simple/

require-virtualenv=false

if previous it was set like require-virtualenv=true

Solution 5

Another place where you may possibly have this "lock" is the pip.conf file. In my case I had one in my ~/Library/Application Support/pip folder and forgot about it.

Typical content of the file could be:

[install]
require-virtualenv = true

[uninstall]
require-virtualenv = true

Similar to other answers, false should be changed to true in the file.

Share:
32,480
amg
Author by

amg

I am here to learn.

Updated on August 11, 2020

Comments

  • amg
    amg over 3 years

    I am trying to instal virtualenv and/or virtualenvwrapper on a mac osx 10.8.3

    I have been fighting with python for the last two days. Finally I was able to install python 2.7.4 using brew. Before I had virtualenv installed using easy_install. Then I tried to uninstall it, trying to get my computer in the same situation as the one of my colleagues. Maybe I uninstalled it with success, maybe not. I don't know how to test it. Now I am supposed to install virtualenv using -

    pip install virtualenv
    

    But it gives me -

    Could not find an activated virtualenv (required).
    

    pip install virtualenvwrapper gives exactly the same output.

    Also the variable: PIP_RESPECT_VIRTUALENV is null:

    echo $PIP_RESPECT_VIRTUALENV
    

    How can I solve this issue?

    Thanks

  • amg
    amg almost 11 years
    Indeed the line export PIP_RESPECT_VIRTUALENV=true was there. So I commented it. Then source run the file again, but it would still not work. So I opened a new terminal, and now it worked. Thank you VERY much.
  • cbron
    cbron about 10 years
    Yea, it has to be set to false for me.
  • Admin
    Admin over 9 years
    For anyone else coming across this: Please note the difference between PIP_RESPECT_VIRTUALENV (as mentioned in the original question) and PIP_REQUIRE_VIRTUALENV, which is the solution mentioned above.
  • JeremyDouglass
    JeremyDouglass almost 6 years
    Trouble leading to this error could also be called by other settings in .bashrc, such as VIRTUALENVWRAPPER_PYTHON. Running export PIP_REQUIRE_VIRTUALENV=false and then a pip uninstall / install virtualenv worked in my case.
  • Eir Nym
    Eir Nym over 5 years
    also this file could be found at ~/.pip/pip.conf
  • Robson
    Robson about 5 years
    How this is different from the currently accepted solution?
  • Roberto Font
    Roberto Font about 5 years
    Well it’s on a different file that’s why I post it. Because with the guide here I could not find it
  • JGurtz
    JGurtz over 3 years
    Would appreciate a canonical link to the mentioned advice. I generally agree with virtualenv being a good idea for writing applications. There are times when one is not writing a "project"*. It's a one-liner, say, or some small, throwaway test cases. Virtualenv is just annoying and impedes in these cases, particularly when things like pyyaml are not there by default. But, maybe I'm doing it wrong? Always looking to upgrade my knowledge.
  • JGurtz
    JGurtz over 3 years
    See also the other answer about ~/.pip/pip.conf. The environment will override the pip.conf setting, but maybe it's better to change it at the source if you need to change the behavior.