conda looking for library outside activate environment

后端 未结 2 323
太阳男子
太阳男子 2021-01-20 13:25

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)

相关标签:
2条回答
  • 2021-01-20 13:52

    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.

    0 讨论(0)
  • 2021-01-20 13:59

    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:

      After console restart, everythings works fine.

    0 讨论(0)
提交回复
热议问题