简体   繁体   中英

ImportError: No module named 'tensorflow.python'

here i wanna run this code for try neural.network with python:

from __future__ import print_function 
from keras.datasets import mnist from 
keras.models import Sequential from 
keras.layers import Activation, Dense 
from keras.utils import np_utils 
import tensorflow as tf


batch_size = 128 nb_classes = 10 nb_epoch = 12

#input image dimensions img_row, img_cols = 28, 28

#the data, Shuffled and split between train and test sets (X_train, y_train), (X_test, y_test) = mnist.load_data()


X_train = X_train.reshape(X_train.shape[0], img_rows * img_cols)

X_test = X_test.reshape(X_test.shape[0], img_row * img_cols)

X_train = X_train.astype('float32') X_test = X_test.astype('float32') X_train /= 255 X_text /= 255

print('X_train shape:', X_train.shape) print(X_train_shape[0], 'train samples') print(X_test_shape[0], 'test samples')

#convert class vectors to binary category

Y_train = np_utils.to_categorical(y_train, nb_classes)

Y_test = np_utils.to_categorical(y_test, nb_classes)

model = Sequential()

model.add(Dense(output_dim = 800, input_dim=X_train.shape[1])) model.add(Activation('sigmoid')) model.add(Dense(nb_classes)) model.add(Actiovation('softmax'))

model.compile(loss = 'categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) #crossentropy fungsi galat atau fungsi error dipakai kalo class biner



#model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch = nb_poch, verbose=1, validation_data=(X_test, Y_test))

score = model.evaluate(X_test, Y_test, verbose = 0) print('Test Score : ', score[0]) print('Test Accuracy : ', score[1])

at the beginning it must install keras, and success. but when try to run the code at the first the error is:

ImportError: No Moduled Name "tensorflow"

then i install using pip:

pip install tensorflow

after installation i try to run code again, got another message like this:

ImportError: No Moduled Name "tensorflow.python"

Message Error i dont have any idea with the error

Uninstall tensorflow:

pip uninstall tensorflow

Then reinstall it:

pip install tensorflow

for me upgrading pip helped,

pip install --upgrade pip
pip uninstall tensorflow
pip install tensorflow

I have the same problem in Windows 10. Until now I don't know why.

But if I create an virtual environment

cd <your project path>

Install virtualenv

pip install virtualenv

Create the virtual environment

virtualenv <envname>

Activate the env

  • Windows Powershell: .\\<envname>\\Scripts\\activate
  • Unix with Bash or zsh: source <envname>/bin/activate

Then now you install tensorflow

(<envname>) $ pip install tensorflow

And then run Hello World successfully.

*Don't forget that you need to activate or configure everytime the virtual environment jupyter, command-line, etc.

Open a python shell and type:

help('modules')   

This will gather a list of all available modules. tensor flow should not show up, as it is not installed correctly (according to the traceback ).
Then:

import sys
sys.path()

This will give you a list of system paths where modules can be installed. If there is a known issue with installing a module, I recommend moving the files manually to the right system path.
The system path depends on the OS you are using, so without knowing that I can't tell you where to move it.
But sys.path() can!

Try changing your file name to something unique. Apparently the python script with same name exits inside, this is the one thats causing the issue. I was using my script, was working fine with bert_base_tf_20.py but when i changed the name to code.py , this happened. So changed it back to bert_code.py

Working fine

If you have python 3.6 and up (most likely), a pip3 package will be installed by default. Installing tensorflow using pip3 will make the path of the installation visible to python. So try

pip3 install tensorflow

First response ever, hope it helps!

我在运行名为 tensorflow.py 的 python 文件时遇到了同样的问题,重命名后问题消失并且文件开始正常运行。

pip install --upgrade pip 

这对我有用

安装 tensorflow 1.15 解决了我的问题

There's another problem which is not mentioned here, and took me a bit to figure out. If you have python installed on C:\\Program Files\\Python , when installing tensorflow, pip will default to another directory. Removing python from C:\\Program Files\\Python and installing it in another directory such as C:\\Python fixed the issue for me.

try these steps

pip install --upgrade pip
pip install tensorflow 

For me reinstalling with

 tensorflow==2.5.0

using pip worked.

try to change the actual running python directory. and make sure that running python directory is not where you downloaded tensorflow. else go to any other directory and you're fine. i hope that solves your probleme.

I had the same problem. After 2h I realized, that it was only because the file name was also "tensorflow". I don't know why, but ostensibly, this confused python. After I renamed the file to "tensorflow_test", everything worked fine and the error didn't occure anymore. Doesn't have to work for you , but could work for someone still looking for an answer on this topic today.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM