OMP: Error #15 Initializing libiomp5md.dll, but found mk2iomp5md.dll already initialized

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
3 min

banner

# OMP: Error #15 Initializing libiomp5md.dll, but found mk2iomp5md.dll already initialized

The Python error "OMP: Error #15: Initializing libiomp5md.dll, but found mk2iomp5md.dll already initialized" occurs when you have conflicting installations of packages.

To solve the error, set the KMP_DUPLICATE_LIB_OK environment variable to True.

Here is the complete error message.

shell
The kernel (user Python environment) has terminated with error code 3. This may be due to a bug in your code or in the kernel itself. Output captured from the kernel process is shown below. OMP: Error #15: Initializing libiomp5md.dll, but found mk2iomp5md.dll already initialized. OMP: Hint: This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see http://www.intel.com/software/products/support/.

# Setting the KMP_DUPLICATE_LIB_OK environment variable in your Python script

One way to solve the error is to set the KMP_DUPLICATE_LIB_OK environment variable to the string "True" in the Python script in which the error occurs.

main.py
import os os.environ['KMP_DUPLICATE_LIB_OK'] = 'True' # The rest of your code below

set kmp duplicate lib ok environment variable to true in python script

We used the os.environ mapping object to set the KMP_DUPLICATE_LIB_OK environment variable to the string "TRUE".

Make sure to place the two lines at the top of the Python script in which the error occurred (above any other import statements).

If you set the environment variable after the rest of your code runs, it won't have an effect.

# Setting the KMP_DUPLICATE_LIB_OK environment variable in your shell

Alternatively, you can set the KMP_DUPLICATE_LIB_OK environment variable in your shell.

If you are on macOS or Linux, open bash or zsh and issue the following command.

shell
# For macOS and Linux export KMP_DUPLICATE_LIB_OK=True

set kmp duplicate lib ok environment variable to true in unix shell

If you are on Windows and use Command Prompt (CMD), run the following command.

shell
# For Windows Command Prompt set KMP_DUPLICATE_LIB_OK=True

If you are on Windows and use PowerShell, run the following command instead.

shell
# For Windows PowerShell $env:KMP_DUPLICATE_LIB_OK=1

Try to rerun your Python script after setting the KMP_DUPLICATE_LIB_OK environment variable.

# Solving the error when using Anaconda

If you got the error when using Anaconda, try to install the nomkl package.

shell
conda install nomkl --channel conda-forge

conda install nomkl package

Then run the following command.

shell
conda install numpy scipy pandas tensorflow

And finally, remove the mlk package.

shell
conda remove mkl mkl-service

If the error persists after running the command, try to recreate your virtual environment.

  1. First, deactivate the old virtual environment.
shell
conda deactivate # Rerun the installation command conda install nomkl --channel conda-forge
  1. Then create the new virtual environment.
shell
conda create --name my-env
  1. Activate your new virtual environment.
shell
conda activate my-env

Make sure to replace the my-env placeholder with your preferred virtual environment name.

  1. Reinstall your packages.
shell
conda install -c anaconda numpy

If the error persists and you use Anaconda, try to rename/delete the libiomp5md.dll.

The file should be located in one of these directories:

  • ~/anaconda3/lib/libiomp5md.dll
  • ~/anaconda3/Library/bin/libiomp5md.dll
  • ~/anaconda3/envs/YOUR_ENV_NAME/Library/bin/libiomp5md.dll
  • C:\Users\YOUR_USER\Anaconda3\envs\YOUR_ENV_NAME\Library\bin\libiomp5md.dll

If you don't know where Anaconda is installed, issue the where conda command.

shell
where conda

issue where conda command

If you don't want to delete the libiomp5md.dll file, simply rename it to something else to have a backup.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.