3

I am working on Windows, I find a difference in import behavior about conda created environment which I cannot understand, here the details

Case 1 (Success)

(base) C:\> conda activate <env-name>
(env-name) C:\> python
>>> import numpy
(Success)

Case 2 (Fail)

C:\> cd <path-to-conda-env>
C:\path-to-conda-env> python
>>> import numpy
(Fail)

I hit this issue with ssl package before, and it can be fixed by install the package from conda-forge rather then default, so it seem to be a issue with the package

What trouble me is the import statement seem to be loading something outside my conda env as I checked sys.path and sys.executable is same in both case

What I have missed here? Any input are welcome.

Regards

Bill
  • 336
  • 4
  • 16

2 Answers2

2

I reproduced the problem (identical sys.path, which excluded my initial guess: %PYTHONPATH%) on my side with Anaconda 2018.12. Environment setting (whether it's Ancaonda, VEnv or any other such tool) consists of (mainly) setting some environment variables.

After testing with some more modules (besides numpy and ssl), by looking at the errors, I realized that the modules that fail have other .dll dependencies of their own. Considering [MS.Docs]: Dynamic-Link Library Search Order, I displayed the contents of my %PATH% variable inside the Python process. On the conda enabled version, the paths below were present at the beginning:

>>> import os
>>> import pprint
>>>
>>> pprint.pprint(os.environ["PATH"])
('e:\\Install\\x64\\Anaconda\\Anaconda\\2018.12;e:\\Install\\x64\\Anaconda\\Anaconda\\2018.12\\Library\\mingw-w64\\bin;e:\\Install\\x64\\Anaconda\\Anaconda\\2018.12\\Library\\usr\\bin;e:\\Install\\x64\\Anaconda\\Anaconda\\2018.12\\Library\\bin;e:\\Install\\x64\\Anaconda\\Anaconda\\2018.12\\Scripts;e:\\Install\\x64\\Anaconda\\Anaconda\\2018.12\\bin;
# The rest of the dirs (regular ones)

Needless to say, that the problem disappeared after prepending those in my %PATH% before starting normal Python:

e:\Install\x64\Anaconda\Anaconda\2018.12>set PATH=e:\Install\x64\Anaconda\Anaconda\2018.12;e:\Install\x64\Anaconda\Anaconda\2018.12\Library\mingw-w64\bin;e:\Install\x64\Anaconda\Anaconda\2018.12\Library\usr\bin;e:\Install\x64\Anaconda\Anaconda\2018.12\Library\bin;e:\Install\x64\Anaconda\Anaconda\2018.12\Scripts;e:\Install\x64\Anaconda\Anaconda\2018.12\bin;%PATH%

e:\Install\x64\Anaconda\Anaconda\2018.12>python
Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> import ssl

But, you should always follow the recommended way (especially when not fully aware of what's going on), and that is activating the environment, because even if this works for this scenario, it might not work for others.

@EDIT0:

As I specified in one of the comments, in order to add the environment to PyCharm, follow the steps from [SO]: How to install Python using the “embeddable zip file” (@CristiFati's answer), with some mentions:

  • At step #4. make sure to select "Conda Environment" instead of "Virtualenv Environment"
  • Apparently, the problem persists when launching Python Console. It shouldn't be the case, seems like the environment is not set. Maybe it's because I didn't create an environment, I'm simply launching Python from the root Anaconda installation? Anyway, as a workaround (gainarie), I'm applying the same changes (setting %PATH%) for the Python Console (from "Settings -> Build, Execution, Deployment -> Console -> Python Console"), as shown in the image below:

    Img00 - Console settings

    After console restart, everythings works fine.

CristiFati
  • 38,250
  • 9
  • 50
  • 87
  • After you set *%PATH%*? What is the content of `os.environ["PATH"]`? Hopefully you didn't copy/paste the paths from the answer since they are only valid on my machine. What is the error (edit the question if you must)? – CristiFati Jan 22 '19 at 08:53
  • Thanks, actually I am hitting this issue with 2 scenario 1) with pyCharm 2) with IIS hosting django, both I have no idea how to activate the environment after a quite few search. And with my limited knowledge, it look like something monster to me so asked the question, thanks for the great answer! – Bill Jan 22 '19 at 08:55
  • deleted first comment as I saved it in accident, tried to add the PATHs added by conda activate under my envs and everything worked, thanks! – Bill Jan 22 '19 at 08:57
  • 1
    Here's how to set a *Python* interpreter in *PyCharm*: https://stackoverflow.com/questions/50818078/how-to-install-python-using-the-embeddable-zip-file/50819634#50819634. The only difference is that at step ***#4.***, you should select "*Conda Environment*" (instead of *Vitrualenv*), and it should work without any other changes. – CristiFati Jan 22 '19 at 09:09
  • Hi CristiFati, I am good with the interpreter part, the issue is with the Python Console in PyCharm, it will have the same import issue. With your first replay, I am able to find a setting to setup the path before calling the interactive console and that's solved issue on, thanks again! – Bill Jan 22 '19 at 09:23
  • I see, I have the same problem. https://medium.com/infinity-aka-aseem/how-to-setup-pycharm-with-an-anaconda-virtual-environment-already-created-fb927bacbe61. I didn't create any env, i just launched the anaconda installation, maybe that is the problem. – CristiFati Jan 22 '19 at 09:31
  • Edited the answer for the *Python Console* problem. It's just a workaround, but it does the trick. – CristiFati Jan 22 '19 at 10:38
0

You need to activate your environment. See that:

(base) C:\> conda activate <env-name>
(env-name) C:\> python
>>> import numpy
(Success)

There is a (base)which means that the active environment name is based. Try doing conda info --envs

to se a list of environments.

When you do:

C:\> cd <path-to-conda-env>
C:\path-to-conda-env> python
>>> import numpy
(Fail)

You are navigating to the folder of the environment but you are not using the python environment that it holds.

Try using:

which python

to see which python version you are using.

jalazbe
  • 1,801
  • 3
  • 19
  • 40