ImportError: No module named examples.tutorials.mnist - python

I am very frustrated by this error, what I did is getting the code from tensor flow tutorial to import moist:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
However when I run the python shows:
File "/Users/kevinling/Desktop/Machine Learning/tensorflow.py", line 2, in
from tensorflow.examples.tutorials.mnist import input_data
ImportError: No module named examples.tutorials.mnist
When I check into the directory, the file is perfectly there:
And the directory is:
enter image description here
The input_data.py is like:
The input_data.py

Just rename your example from "tensorflow.py" to anything else and it will work. The interpreter is trying to import the necessary files from your script.

Did you already install tensorflow? If not, follow their install instructions or simply install using pip:
pip install tensorflow
Now, make sure you are NOT currently in a folder where tensorflow is located, and try running your script.
python your_script.py

Related

from gcn.utils import * ModuleNotFoundError: No module named 'gcn.utils'. Why can't it find it?

https://github.com/tkipf/gcn
I am trying to run this graph neural network code from github on my google colab. When I run the train.py file it gives me this error:
from gcn.utils import * ModuleNotFoundError: No module named 'gcn.utils'
I ran !python3 /content/drive/MyDrive/GNN/gcn/setup.py install - the setup file
and installed gcn as well - !pip install gcn
also made sure the directory was pointed to the right address.
What else can I do?
The folder name is gcn,there are some conflicts,change the folder name, or remove all the gcn. from imports of gcn.xx.
such as: from gcn.utils -> from utils

ModuleNotFoundError: No module named 'onnxruntime'

I'm taking a Microsoft PyTorch course and trying to implement on Kaggle Notebooks but I kept having the same error message over and over again: "ModuleNotFoundError: No module named 'onnxruntime'". I've checked everywhere possible if I could find a solution to it but none, I even tried installing it manually using pip in the notebook, but it's still not working. I've checked the official onnxruntime website and documentation but there's nowhere it states anything about something being outdated or any other issue. Someone help. My code won't run because it says "onnxruntime is not defined". Here are my imports:
%matplotlib inline
import torch
import onnxruntime
from torch import nn
import torch.onnx as onnx
import torchvision.models as models
from torchvision import datasets
from torchvision.transforms import ToTensor
and the code cell I'm trying to run
session = onnxruntime.InferenceSession(onnx_model, None)
input_name = session.get_inputs()[0].name
output_name = session.get_outputs()[0].name
result = session.run([output_name], {input_name: x.numpy()})
predicted, actual = classes[result[0][0].argmax(0)], classes[y]
print(f'Predicted: "{predicted}", Actual: "{actual}"')
And you can find the complete notebook here: https://www.kaggle.com/faisalalbasu/complete-model
the error occurs because "import" cannot find onnxruntime in any of the paths, check where import is searching and see if onnxruntime is in there.
check what path pip install installs to, that way in the future you won't have the same problem! :)

How to import AdversarialModel, simple_gan, gan_targets in colab

I am importing keras_adversarial and cloning its git too but it cannot import 'AdversarialModel'
i am working on gans, built generator and discriminator. now i want to combine them but colab is giving error while importing the modules of keras_adversarial
import keras_adversarial
from keras_adversarial import AdversarialModel, simple_gan, gan_targets
from keras_adversarial import AdversarialOptimizerSimultaneous,
normal_latent_sampling
1 import keras_adversarial
----> 2 from keras_adversarial import AdversarialModel, simple_gan,
gan_targets
3 from keras_adversarial import AdversarialOptimizerSimultaneous,
normal_latent_sampling
ImportError: cannot import name 'AdversarialModel'
The Colab environment only have a core set of packages installed. To add some third-party packages as keras_adversarial, you can execute installation script direcrly in Colab cells, with ! symbol before each command to indicate that these are command line bash code, not Python.
In your case, you need to do:
!git clone https://github.com/bstriner/keras_adversarial.git
!cd keras_adversarial
!python setup.py install

osx - Mac - Python 3.5 - ImportError: No module named 'cubicspline' when testing python package

I recently downloaded a package and when testing the package to see if everything works correctly I get the error ImportError: No module named 'cubicspline'. When following the trail to see where is error occurs I found that cubicspline.py (the file not being found) is in the same folder as extcurve_s16.py (the file calling cubicspline).
File "/Users/Austin/anaconda/lib/python3.5/site-packages/isochrones/schlafly/extcurve_s16.py", line 4, in <module>
import cubicspline
I've checked the permissions on the folder and I'm able to both read and write. There is also an __init__.py file in the folder. Any ideas here? I can't figure out why it wouldn't be able to call a file that is in the same folder. Here is the exact chunk of code for reference, as can be seen import numpy works fine.
import numpy
import cubicspline
The issue was resolved by cloning the isochrones package from Github instead of using pip install isochrones. There was some type of issue with the pip version.

How to change Keras backend (where's the json file)?

I have installed Keras, and wanted to switch the backend to Theano. I checked out this post, but still have no idea where to put the created json file. Also, below is the error I got when running import keras in Python Shell:
Using TensorFlow backend.
Traceback (most recent call last): File "", line 1, in
import keras File "C:\Python27\lib\site-packages\keras__init__.py", line 2, in
from . import backend File "C:\Python27\lib\site-packages\keras\backend__init__.py", line 64, in
from .tensorflow_backend import * File "C:\Python27\lib\site-packages\keras\backend\tensorflow_backend.py",
line 1, in
import tensorflow as tf ImportError: No module named tensorflow
When running python -c "import keras; print(keras.__version__)" from Windows command line, I got:
Using TensorFlow backend. Traceback (most recent call last): File
"", line 1, in File
"C:\Python27\lib\site-packages\keras__init__.py", line 2, in
from . import backend File "C:\Python27\lib\site-packages\keras\backend__init__.py", line 64, in
from .tensorflow_backend import * File "C:\Python27\lib\site-packages\keras\backend\tensorflow_backend.py",
line 1, in
import tensorflow as tf ImportError: No module named tensorflow
Can someone please help? Thanks!
After looking at keras sources (this place):
Start up your python-binary and do the following
import os
print(os.path.expanduser('~'))
# >>> C:\\Users\\Sascha' # will look different for different OS
This should be the base-directory
Keras will build an folder .keras there where keras.json resides (if it was already created). If it's not there, create it there
Example: C:\\Users\\Sascha\\.keras\\keras.json'
In case of Ubuntu,the following worked for me:
The '.keras' folder is present in your home directory,but is hidden.So,you need to unhide the hidden files in your home directory.
You can see the hidden files in Ubuntu by
View-> show hidden files or
pressing ctrl+H.
You can now see the '.keras' folder in your home directory.Inside that folder,you will see the 'keras.json' file which you can modify to switch the keras backend to theano according to the official documentation https://keras.io/backend/
"Can’t find your keras.json file? : Windows
On most systems the keras.json file (and associated sub-directories) will not be created until you open up a Python shell and directly import the keras package itself.
If you find that the ~/.keras/keras.json file does not exist on your system, simply open up a shell, (optionally) access your Python virtual environment (if you are using virtual environments), and then import Keras:
$ workon keras_tf
$ python
>>> import keras
>>> quit()
"
Referenced from : keras-with-tensorflow/theano-backend
For those with a python shell open:
import os
with open(os.path.expanduser('~')+'\\.keras\\keras.json','w') as f:
new_settings = """{\r\n
"epsilon": 1e-07,\r\n
"image_data_format": "channels_last",\n
"backend": "theano",\r\n
"floatx": "float32"\r\n
}"""
f.write(new_settings)
import keras
You can directly use,
import os
os.environ['KERAS_BACKEND']='theano'
or
os.environ['KERAS_BACKEND']='tensorflow'
In case you want to change the config, the json is available here: ~/.keras/keras.json
To do this dynamically in python 2.7 you can run:
from keras import backend as K
import os
def set_keras_backend(backend):
if K.backend() != backend:
os.environ['KERAS_BACKEND'] = backend
reload(K)
assert K.backend() == backend
set_keras_backend("theano")
For Linux systems, the hidden .keras directory will be created in the user’s home directory. To observe whether or not it has been created, run the following command from your home directory (the -a allows you to see hidden files and directories).
ls –a
If the directory is there, then cd into it and modify the keras.json file. If it’s not there, then create the directory with
mkdir .keras
Then create the file with
touch keras.json
Then edit the file to make the config changes you referenced to change the backend engine to Theano.
This process is covered fully in this video.
The official Keras Documentation may help you. This link shows how to change the backend of Keras. You have to change the ~/.keras/keras.json from 'backend': 'tensorflow', to 'backend': 'theano'.
Just to add this informative post. I'm using anaconda for my task. And imported keras through my anaconda python. So keras got installed in
C:\Users\username\AppData\Local\Continuum\Anaconda3\Lib\site-packages
There are 2 folders in site-packages: keras & Keras-1.0.8.dist-info.
In Keras-1.0.8.dist-info, there's a file called metadata.json. This by default has "Theano" as backend. So, if you change that to tensorflow, you will get tensoflow backend.
Hope it will help someone who has might type of issues.
Simplest Solution:
Google's TensorFlow is default backend for keras but for example if u want to change it with theano then
First check if the alternate backend you wish to work with is installed successfully by importing it in python shell:
import theano as th
if that works fine
Step 2: if you have installed keras, theano in virtualenv then go to virtualenv directory
e.g virtualenv/virtual/lib/python2.7/site-packages/keras/backend/
open init.py
change line 27
Default backend: TensorFlow.
_BACKEND = 'theano'
and thats it
open python shell
and import keras
import keras as kd
Type following and press enter on command prompt:
%USERPROFILE%/.keras/keras.json
Change backend in the opened text file and save it.
In ubuntu you can use this command to open keras.json file in vi editor and editing and saveing
sudo vi $HOME/.keras/keras.json
or use the following for opening in gedit
sudo gedit $HOME/.keras/keras.json

Categories