Python 3.6中无法导入TensorFlow求助(NVIDIA 940MX显卡)
Hey there, let's work through this TensorFlow import issue you're facing with your NVIDIA 940MX and Python 3.6 setup. From the package list you shared, you've got tensorflow-gpu 1.7.0 installed—but the critical missing pieces are the CUDA and cuDNN dependencies that TensorFlow's GPU version relies on. Here's a step-by-step fix tailored to your setup:
Your NVIDIA 940MX uses the Maxwell architecture, which fully supports CUDA 9.0—this is the exact version required for TensorFlow 1.7.0. Avoid newer CUDA versions, as they won't play nice with your TF version.
Since CUDA 9.0 is a legacy version, you'll need to grab it from NVIDIA's archive:
- Download the installer matching your OS (Windows/Linux/macOS) from NVIDIA's legacy CUDA downloads.
- During installation, make sure to select the "CUDA" component (you can skip extras like GeForce Experience if you don't need them).
- After installing, add the CUDA binaries to your system PATH to let Python find them:
- Windows: Add
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\binandC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\libnvvpto your system PATH via Environment Variables. - Linux: Add these lines to your
~/.bashrc(or~/.zshrcif you use Zsh):
Then runexport PATH=/usr/local/cuda-9.0/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64:$LD_LIBRARY_PATHsource ~/.bashrcto apply changes. - macOS: Similar to Linux, add the paths to your shell profile (like
~/.bash_profile).
- Windows: Add
cuDNN is a GPU-accelerated library TensorFlow needs for deep learning operations. You'll need a free NVIDIA developer account to download it:
- Grab cuDNN 7.0.5 for CUDA 9.0 from NVIDIA's cuDNN archive.
- Extract the zip file, then copy the files into your CUDA directory:
- Windows: Copy
cuda/include/cudnn.htoC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\include; copycuda/lib/x64/cudnn64_7.dllandcuda/lib/x64/cudnn.libtoC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\lib\x64. - Linux: Copy
cuda/include/cudnn.hto/usr/local/cuda-9.0/include; copycuda/lib64/libcudnn*.so*to/usr/local/cuda-9.0/lib64. Then run:sudo chmod a+r /usr/local/cuda-9.0/include/cudnn.h /usr/local/cuda-9.0/lib64/libcudnn*.so* - macOS: Copy the include and lib files to
/usr/local/cuda/includeand/usr/local/cuda/librespectively.
- Windows: Copy
- Close and reopen your terminal/command prompt to ensure PATH changes take effect.
- Run
nvcc --versionto confirm CUDA 9.0 is installed properly. - Now test TensorFlow in Python:
Ifimport tensorflow as tf print(tf.__version__) print(tf.test.is_gpu_available())is_gpu_available()returnsTrue, you're all set!
- Windows DLL errors: Double-check that all CUDA and cuDNN DLLs are in your PATH, and confirm you're using a 64-bit Python 3.6 (TensorFlow 1.7 doesn't support 32-bit Python).
- Linux driver issues: Make sure your NVIDIA drivers are up to date—they need to be version 384.81 or newer to work with CUDA 9.0.
- Reinstall TensorFlow: If all else fails, try a clean reinstall:
pip uninstall tensorflow-gpu -y pip install tensorflow-gpu==1.7.0
内容的提问来源于stack exchange,提问作者Hansa Tharuka

