18

I am trying to use the nets from the official mnist directory of tensorflows model repository. On my windows system I receive this error:

C:\Users\ry\Desktop\NNTesting\models\official\mnist>mnist_test.py
Traceback (most recent call last):
  File "C:\Users\ry\Desktop\NNTesting\models\official\mnist\mnist_test.py",line 24, in <module>
    from official.mnist import mnist
ModuleNotFoundError: No module named 'official'

I have followed their official directions and set my python path using

set PYTHONPATH="PYTHONPATH:"%cd%"

and can confirm that

PYTHONPATH="$PYTHONPATH:C:\Users\ry\Desktop\NNTesting\models"

and I have also installed the dependencies successfully. Does anyone have experience using these models on a windows system and can help me with this pathing issue? I'm not sure what I have done incorrectly here.

Thanks

1
  • 1
    I had the same problem on macOS because I used this export: PYTHONPATH="$PYTHONPATH:~/relative/path/to/models". But tilde expansion only happens at the beginning of a word, so I fixed it with PYTHONPATH="$PYTHONPATH:$HOME/relative/path/to/models". Hope this helps someone. Dec 5, 2018 at 11:40

11 Answers 11

33
pip install tf-models-official
2
  • Works fine on Google Colab Apr 11, 2021 at 21:50
  • Solve problem with from official.nlp import optimization Sep 11 at 7:00
6

For Google Colab I needed to add the the model dir also to the Systems path:

!git clone https://github.com/tensorflow/models.git

import os
os.environ['PYTHONPATH'] += ":/content/models"

import sys
sys.path.append("/content/models")
5

if anyone has this problem make sure that the python path variable doesn't have quotations in it. For some reason, the readme has quotations around it.

Here is the correct way to set it

PYTHONPATH=path\to\models
1
  • How do I use that, can you provide an example?
    – David
    Apr 12 at 19:33
3

I had exactly the same question as you did, and the following solution solved this problem.

There is an error in the tensorflow/models/official README.md

https://github.com/tensorflow/models/tree/master/official

Wrong

export PYTHONPATH="$PYTHONPATH:/path/to/models" 

Correct

export PYTHONPATH=$PYTHONPATH:/path/to/models 
1

The Official Models are made available as a Python module. To run the models and associated scripts, add the top-level /models folder to the Python path with the command: export PYTHONPATH="$PYTHONPATH:/path/to/models"

FROM README

1

Go to models folder and do

export PYTHONPATH=$PYTHONPATH:$PWD
0

I had the same problem. Did you use windows 10? Make sure you run the command prompt as "administrator". I used it in VS code at first, no warning, and didn't work. But it worked when I run a separate prompt window as "administrator".

set PYTHONPATH=path\to\models

then run the model.

0

I was setting up to run the NMT model and ran into the same problem. It took me bit to figure out exactly which folder should be added to PYTHONPATH.

I tried several folders inside my example's directory with no luck. I finally understood what that import was trying to tell me... "from official.transformer.utils import tokenizer" means "add the parent of directory 'official' to PYTHONPATH".

For me, this was just the top-level 'models-master' directory that I obtained from GitHub. Once I added /path/to/models-master, I was past this obstacle.

0

add the model directory to PYTHONPATH.

import os
os.environ['PYTHONPATH'] += ':/content/models/research/:/content/models/research/slim/'
0

jupyter. Clone from official git and manually appended the path to sys.

!git clone https://github.com/tensorflow/models.git

import sys
sys.path.append("C:/Windows/System32/models")
sys.path
0

For someone struggling in mac os (M1 or any other) set

os.environ['PYTHONPATH'] = 'PYTHONPATH:relative/path/to/Tensorflow/models'

from jupyter notebook as somehow it is not taking it from shell variable so setting it up manually worked also installing

pip install tf-models-official

did not worked for me though it says that dependency is already satisfied.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.