Search code examples
pythontensorflowpycharm

Tensorflow : ImportError: DLL load failed


I am using Windows 10, Python 3.7.8 and PyCharm.

First, I installed Python 3.7.8. Then I installed PyCharm.

I am trying to run Python code, to be specific Tensorflow code. I follow these instructions here: https://www.tensorflow.org/site-assets/downloads/marketing/cert/Setting_Up_TF_Developer_Certificate_Exam.pdf Page 4-8.

As in the instructions I create a new PyCharm project:

exa1

Then in the preferences for this Project, choosing Python Interpreter I install the following packages:

  • tensorflow
  • tensorflow-datasets
  • numpy
  • pillow
  • urllib3

All get installed succesfully:

exa2

Then I create a new Python file:

exa3

I run the following code:

import tensorflow as tf
print(tf.version)

exa4

However, I get error(s): "ImportError: DLL load failed: The specified module could not be found"

error1 error2

How can I fix this?


Solution

  • SHORT ANSWER

    According to this thread : https://github.com/tensorflow/tensorflow/issues/35618#issuecomment-596631286

    downloading that lastest microsoft redistributable C++ from here :

    https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads

    will solve your problem

    EXTRA 1 : Use Docker as a Python Interpreter in Pycharm

    Pycharm has a usefull tool that I personally use all the time : Docker as an interpreter.

    This implies to install Docker but once you got it, you can download pretty much all the images with dependecies pre-installed and ready for work !

    In your case :

    1. If you dont have it Download Docker for windows (https://hub.docker.com/editions/community/docker-ce-desktop-windows/)

    2. Run this command from you cmd : docker pull tensorflow/tensorflow

    3. check that you have the tensorflow image by running docker images command. you should get this output :

    enter image description here

    1. Open new project in pycharm

    2. go to Setting | Project | Project interpreter

    3. Click on the three vertical dots to Add a new interpreter

    4. you should get to this window (on the left click on "Docker"): enter image description here

    5. click on "OK"

    And Let's code !

    Now if you need another big dependecy like this one, you can always reproduce those steps with the specific dependency image from docker hub

    For further details, here is the official tutorial from JetBrains : How to make Docker container as an interpreter in PyCharm

    EXTRA 2 : Use Docker as a Python Interpreter in VSCode

    For some of users that using python in VSCode (I personally don't), I've found those steps to achieve the same configuration (Docker as python interpreter) in VSCode :

    1. Install the Python extension

    2. Install the Remote - Containers extension

    3. Open the Command Pallette and type Remote-Containers, then select the Attach to Running Container... and selecet the running docker container

    4. VS Code will restart and reload

    5. On the Explorer sidebar, click the open a folder button and then enter /code (this will be loaded from the remote container)

    6. On the Extensions sidebar, select the Python extension and install it on the container

    7. Whenprompet on which interppreter to use, select /usr/local/bin/python

    8. Open the Command Pallette and type Python: Configure Tests, then select the unittest framework

      Source for this part : Setting Up a Python Remote Interpreter Using Docker