Search code examples
pythoncmdanaconda

Import failure when calling Anaconda Python from Windows shell


I want to call my Anaconda Python from the Windows shell. The goal is to eventually call it from MATLAB using a system call, so I have to use the plain Windows shell, and not the "Anaconda prompt".

I have a simple Python script that I want to run, but it doesn't get past the import statements:

> "C:\ProgramData\Anaconda3\python.exe" lhs.py
Traceback (most recent call last): 
  File "lhs.py", line 8, in <module> 
    import numpy 
  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\__init__.py", line 140, in <module> 
    from . import _distributor_init 
  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\_distributor_init.py", line 34, in <module> 
    from . import _mklinit 
ImportError: DLL load failed: The specified module could not be found. 

EDIT

I made a simplified Python script (error.py) containing only one line:

import numpy

I then call it as:

"C:\ProgramData\Anaconda3\python.exe" error.py


Solution

  • TL;DR

    C:\ProgramData\Anaconda3\condabin\activate.bat YOURENVIRONMENTNAME && python N:\Temp\lhs.py
    

    Long Answer

    There is a similar issue reported on github. I found this by searching for the last few lines of your message. It is about this:

    File "C:\Users\hanna\Anaconda3\lib\site-packages\tensorflow\python_init.py",
    

    line 47, in import numpy as np File "C:\Users\hanna\Anaconda3\lib\site-packages\numpy_init_.py", line 140, in from . import _distributor_init File "C:\Users\hanna\Anaconda3\lib\site-packages\numpy_distributor_init.py", line 34, in from . import _mklinit

    There, they link to a conda troubleshooting page:
    Error messages like

    Intel MKL FATAL ERROR: Cannot load mkl_intel_thread.dll

    Cause
    NumPy is unable to load the correct MKL or Intel OpenMP runtime libraries. This is almost always caused by one of two things:

    1. The environment with NumPy has not been activated.

    2. Another software vendor has installed MKL or Intel OpenMP (libiomp5md.dll) files into the C:\Windows\System32 folder. These files are being loaded before Anaconda's and they're not compatible.  

    If you are not activating your environments, start with doing that.


    So let us try this first:

    C:\\ProgramData\\Anaconda3\\condabin\\activate.bat
    conda run -n yourenvironment C:\\path\\to\\your\\pythonfile\\lhs.py
    

    (Yes, from a normal cmd terminal)

    If that seems to work, you should be able to chain the two commands using &&. My own example looks like this:

    F:\Programme\Programme\Anaconda3\condabin\activate.bat && conda run -n textrecognition N:\Temp\temp.py
    

    This can be simplified further to

    F:\Programme\Programme\Anaconda3\condabin\activate.bat textrecognition && python N:\Temp\temp.py
    

    In case of the problem being number two, you have the option to load conda things before system32. This is supported in anaconda starting the following versions:

    Python 2.7.15 build 14

    Python 3.6.8 build 7

    Python 3.7.2 build 8

    To activate that, you need to set some environment variables to 1:

    Control environment variables:

    CONDA_DLL_SEARCH_MODIFICATION_ENABLE
    
    CONDA_DLL_SEARCH_MODIFICATION_DEBUG
    
    CONDA_DLL_SEARCH_MODIFICATION_NEVER_ADD_WINDOWS_DIRECTORY
    
    CONDA_DLL_SEARCH_MODIFICATION_NEVER_ADD_CWD
    

    To set variables on Windows, you may use either the CLI (Anaconda Prompt, for example) or a Windows GUI.

    CLI: https://superuser.com/questions/79612/setting-and-getting-windows-environment-variables-from-the-command-prompt/79614

    GUI: http://www.dowdandassociates.com/blog/content/howto-set-an-environment-variable-in-windows-gui/

    These should be set to a value of 1 to enable them. For example, in an anaconda prompt terminal:

    set CONDA_DLL_SEARCH_MODIFICATION_ENABLE=1