You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用TensorFlow官方MNIST网络时出现No module named 'official'错误求助

Fixing "ModuleNotFoundError: No module named 'official'" in Windows for TensorFlow MNIST Tests

Hey there, let's get this sorted out. That ModuleNotFoundError usually means Python can't locate the official module directory, even if you tried setting PYTHONPATH. Here are the most reliable fixes tailored for your Windows setup:

1. Double-check your PYTHONPATH setup

First, make sure you're pointing to the root of the TensorFlow Models repo, not the official/mnist subdirectory. Your repo root is C:\Users\ry\Desktop\NNTesting\models in this case.

  • Temporary fix (for current cmd session):
    Open your command prompt, navigate to any directory, and run:

    set PYTHONPATH=C:\Users\ry\Desktop\NNTesting\models;%PYTHONPATH%
    

    Then go back to the official/mnist folder and run the test again:

    cd C:\Users\ry\Desktop\NNTesting\models\official\mnist
    python mnist_test.py
    

    Note: This only lasts until you close the cmd window.

  • Permanent fix (persists across sessions):

    1. Right-click "This PC" → "Properties" → "Advanced system settings" → "Environment Variables".
    2. Under "User variables", click "New".
    3. Set Variable name to PYTHONPATH, Variable value to C:\Users\ry\Desktop\NNTesting\models.
    4. Click "OK" to save, then restart your command prompt for changes to take effect.

2. Add the path directly in your test script

If setting PYTHONPATH still doesn't work, you can manually add the repo root to Python's path at the top of mnist_test.py:

import sys
sys.path.append("C:\\Users\\ry\\Desktop\\NNTesting\\models")

# Then the rest of the imports
from official.mnist import mnist

This bypasses any environment variable issues and ensures Python can find the official module.

3. Check for virtual environment conflicts

If you're using a virtual environment (like venv or conda), make sure:

  • You've activated the virtual environment before setting PYTHONPATH or running the test.
  • The PYTHONPATH is set within the activated environment session, or you've added the repo path to the virtual environment's site-packages directory via a .pth file:
    1. Navigate to your venv's Lib\site-packages folder (e.g., C:\Users\ry\Desktop\NNTesting\venv\Lib\site-packages).
    2. Create a new file named tensorflow-models.pth.
    3. Inside the file, write the full path to your models repo root: C:\Users\ry\Desktop\NNTesting\models.
    4. Save the file, then restart your Python session.

4. Verify directory structure

Double-check that your models repo has the correct structure. You should see:

models/
├── official/
│   ├── mnist/
│   │   ├── mnist.py
│   │   └── mnist_test.py
│   └── ... (other official modules)
└── ... (other repo files)

If the official folder is missing or misplaced, re-clone the TensorFlow Models repo to get the correct structure.


内容的提问来源于stack exchange,提问作者Ryan Brady

火山引擎 最新活动