57

I am trying to instal virtualenv and/or virtualenvwrapper on a Mac OS X 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?

wovano
  • 4,543
  • 5
  • 22
  • 49
Pietro Speroni
  • 3,131
  • 11
  • 44
  • 55

7 Answers7

117

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.

Denialos
  • 956
  • 7
  • 16
Bibhas Debnath
  • 14,559
  • 17
  • 68
  • 96
  • 7
    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. – yvandermeer Jan 20 '15 at 09:57
  • 3
    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. – JeremyDouglass May 22 '18 at 02:18
10

@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
...
JCotton
  • 11,650
  • 5
  • 53
  • 59
  • 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. – JGurtz Oct 23 '20 at 06:53
6

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>.

PerryAJ
  • 106
  • 1
  • 4
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

N N K Teja
  • 197
  • 1
  • 7
1

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.

Vitalii
  • 4,267
  • 1
  • 40
  • 45
1

Important to heed @JCotton's advice here-- keeping your pip setup so as to only install into virtualenvs is a great practice.

His solution to get virtualenv setup again of pip install --upgrade pip --isolated is exactly what should be done.

You should NOT turn off requiring a virtualenv, either by config file or by editing ~/.bash_rc or ~/.bash_profile, to get your project's pip packages installed. We're only doing it here because OP needs virtualenv itself installed.

In general, I see people get this message when their virtualenv wasn't setup correctly for their project in the first place. Reminder that to create a virtualenv with its own python and pip so that you don't run into the "could not find an activated virtualenv" error, you run virtualenv -p python3

Micah
  • 10,295
  • 13
  • 66
  • 95
  • 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 Oct 23 '20 at 06:50
-1

for matchbook you must go to '.bash_profile '

1) open with your favorite editor in terminal

nano .bash_profile OR vim .bash_profile

2) find the text line that says

export PIP_REQUIRE_VIRTUALENV=true

3) delete it or set it equal to "false"

4) finally restart your terminal