Bill
Bill

Reputation: 530

conda looking for library outside activate environment

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

Upvotes: 3

Views: 2623

Answers (2)

CristiFati
CristiFati

Reputation: 41116

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.

Upvotes: 2

jalazbe
jalazbe

Reputation: 2005

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.

Upvotes: 0

Related Questions