3

With Python 3.7 on OS X I set up a virtual environment then

    $ source venv/bin/activate
    $ pip install numpy
    $ which pip 
pip is /Users/me/PycharmProjects/Test1/venv/bin/pip
(venv) 

But rather than installing in the virtual environment numpy is installed in

/usr/local/lib/python2.7

and numpy doesn't appear in pip list

The issue occurs with both Python installed via the Python download or via brew.

What possible settings could be causing the package to be installed in the wrong location.

Gazzer
  • 4,524
  • 10
  • 39
  • 49
  • Try `$ which pip` and `$ which pip3` and see if it gives you different results. – Patrick Conwell Feb 11 '19 at 15:12
  • `head -1 /Users/me/PycharmProjects/Test1/venv/bin/pip` ? – phd Feb 11 '19 at 15:26
  • @phd `#!/Users/me/PycharmProjects/Test1/venv/bin/python` – Gazzer Feb 11 '19 at 15:38
  • @PatrickConwell They are both the same. `pip3 install numpy` gives the same result. – Gazzer Feb 11 '19 at 15:39
  • 1
    Everything looks good. – phd Feb 11 '19 at 15:45
  • 1
    By default virtualenv creates new envs with the python interpreter that virtualenv was installed with. You could try with a new one: `$ virtualenv -p python3.7 newenv`. But since virtualenv came bundled with Pycharm....please double-check the output of: `head $(which virtualenv) ; ls -la $(which pip) ; ls -la $(which virtualenv) `. – stop.climatechange.now Feb 11 '19 at 15:59
  • @Alex `which virtualenv` is not found. I assume that the system one is not needed in the context of Pycharm. `which pip` gives me as expected the `venv\bin\pip` path which `which pip2` gives the same plus the `/usr/local/bin/pip3`. Also as expected. – Gazzer Feb 11 '19 at 16:11
  • @phd Yep, this is why it's so maddening. My first time to use Python, and I can't get the environment set up! – Gazzer Feb 11 '19 at 16:12
  • `ls -la Test1/venv/bin/python` ? If not symlinked to python3, then it is actually python2.. In the dir where the virtualenv script is located: `$ head -2 virtualenv`. It will show the python version it's been installed with. @Gaz – stop.climatechange.now Feb 11 '19 at 16:25
  • Pycharm uses its own supplied version, but it is version 3: `./python --version` `Python 3.7.2` – Gazzer Feb 11 '19 at 16:29
  • 1
    If this virtualenv really uses python3, would it work if you install packages in [their gui](https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-upgrading-packages.html)? Had it been configured correctly per their [instruction here](https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html)? Or you could create a new venv with a non-virtualenv tool `python3 -mvenv newenv` and try to [associate it with Pycharm](https://www.techcoil.com/blog/how-to-associate-a-virtualenv-environment-with-a-python-project-in-pycharm/) for testing.. – stop.climatechange.now Feb 11 '19 at 16:49
  • @Alex I have perhaps narrowed something down. If I do `cd` `pip3 install colorama` `pip3 list` then colorama does not appear. However it has been installed in the 2.7 folder. Something is causing pip to install in that folder however I run it. (It's nothing in `.bash_profile` which I have checked). – Gazzer Feb 11 '19 at 16:51
  • @Alex I'm at this stage because the gui doesn't work. It says 'installed' but the same issue. It doesn't appear with PyCharm, but appears in 2.7 as noted in my original post. There must be something somewhere causing `pip` to install in that folder. It's as if there is a 'hard' override somewhere making `pip` install in the wrong place. But I'm not experienced enough with python (first day!) to know what it could be. (2am here, so I'll step off for a bit). – Gazzer Feb 11 '19 at 16:54
  • 1
    @Alex As I suspected, an invisible setting turned out to be the culprit. See below. – Gazzer Feb 12 '19 at 03:00

3 Answers3

7

To answer my own question.

There was an invisible

~/.config/pip/pip.conf 

file. That contained these lines:

[global]
target = /usr/local/lib/python2.7/site-packages

This file was a few years old, so I'm unsure how it got there but removing it resolved the issue.

Gazzer
  • 4,524
  • 10
  • 39
  • 49
  • 2
    Nice catch! By default there's no pip.conf, but it can be created by hand to customize things. All possible pip.conf locations (per-user, per-venv, and system-wide/global, and how they override each other) are [listed here](https://pip.pypa.io/en/stable/user_guide/#config-file). If someone faces the issue, they could use `pip config list` [command](https://pip.pypa.io/en/stable/reference/pip_config/) to see the active configuration, or `locate pip.conf` and `find` it. – stop.climatechange.now Feb 12 '19 at 12:27
  • @Alex How I wish I'd known about pip config list before :-) I was tearing my hair out by the end of it. But as you do, I learnt a lot along the way. – Gazzer Feb 13 '19 at 09:10
2

What worked for me :

  1. I just created a pip.ini file by myself on my venv root folder .
  2. filled that file like this :

[global]

target=D:\Dropbox\online store\django\ve\lib\site-packages

3)after restarting venv, by using this command

python -m pip install <package name>

now I am able to install packages on my venv ( instead of being installed globally )

4seasonn
  • 75
  • 6
1

I had this issue because I renamed the root directory of my project. Looking at the venv/bin/activate script, I could see references to the old name.

Could probably fix it by hand, but I just deleted the venv folder and re-created it.

NotBadJon
  • 13
  • 1
  • 5
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 07 '22 at 15:38