3

Working in Windows PowerShell, I created a virtual environment within Anaconda using

>create conda -n test python=2.7.8

I activated it using

>activate test
activating environment "test"...

The test env contains no packages. Python is the only thing in there. I checked the test\Lib\site-packages folder and it is empty. I started python

>python
>>>import pandas
>>>pandas.Series(range(1,5))
0  1
1  2
2  3
3  4
dtype: int64

I expected error messages because there is no pandas installed in this virtual env. Instead it seems to be importing pandas from the global Anaconda environment, where pandas is installed.

I thought virtual environments were supposed to be isolated. Have I misunderstood how they are supposed to work? If they are supposed to be isolated, what might the problem be?

2 Answers2

0

New to stack overflow, but I think I can help. First, the commands I needed to run were a bit different (I am on Linux). That is, in fact, how virtual environments are supposed to work. A few things to try: conda create --dry-run -n test python=2.7.8 to see if pandas is being installed to the venv (I don't know why it would). Another idea, when you activate test does the prompt actually change to the venv? Maybe see: virtualenv-in-powershell

Community
  • 1
  • 1
barrymoo
  • 140
  • 3
  • barrymoo - I did use the --dry-run switch and it reported that the only thing it was putting in 'test' was python, no other packages. From PowerShell, the prompt did not change upon activating 'test'. I've since tried activating from Cmd Prompt. The prompt does change. Trying to import pandas does produce ImportError: No module named pandas. Why is the behavior so different in PowerShell. Does this mean I can't use PowerShell to access Anaconda envs? Would I see this same behavior if I used virtualenv? –  Feb 07 '15 at 05:19
  • Looking at the link I provided in my answer, it appears that you can use venv in PowerShell. However, it requires some modifications. One of the users suggests looking for a `Scripts\test.ps1` for PowerShell activation. – barrymoo Feb 07 '15 at 16:52
  • barrymoo - Thank you for your help. I did follow your link but did not understand. I have very little experience with PowerShell and none with bat scripts. For now I'm satisfied to know to not use PS to work in my conda venv, and stick to Cmd Prompt. I am new to programming generally and am at the instruction following stage. It's very easy to find myself in completely unknown territory. I hope to revisit the PS + venv issue in the future when I have learned more. I would vote your answer up, but it appears I don't have enough points to do that. –  Feb 08 '15 at 21:14
0

Windows has a little idiosyncrasy that can lead to confusing situations like this, which is that it always implicitly puts . (the current directory) in the front of the search PATH. This means that if you are currently in the Anaconda directory when you start python, it will run the python.exe in that directory, regardless of what the rest of your PATH looks like (the activate command, in case you don't know, works by modifying the order of directories in the PATH variable).

The solution is to cd away from the Anaconda directory before starting Python.

asmeurer
  • 86,894
  • 26
  • 169
  • 240
  • I tried this and it still behaved the same. I was in Powershell, `>cd c:\ `, now the prompt says `PS C:\>`, `>activate test`. The PS prompt does not change. `>python`, `>>>import pandas`, no error, pandas works fine. The 'test' env is clearly not isolated. –  Feb 10 '15 at 18:32
  • `activate` doesn't work in Powershell currently unfortunately. – asmeurer Feb 10 '15 at 21:43