导入错误:没有名为“tensorflow.python”的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41415629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:55:32  来源:igfitidea点击:

ImportError: No module named 'tensorflow.python'

pythontensorflowkerasneural-network

提问by zhaozheng

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

在这里,我想运行此代码以使用 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 :

一开始它必须安装keras,并且成功。但是当首先尝试运行代码时,错误是:

ImportError : No Moduled Name "tensorflow"

导入错误:没有模块名称“tensorflow”

then i install using pip :

然后我使用 pip 安装:

pip install tensorflow

pip 安装张量流

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

安装后,我尝试再次运行代码,收到另一条消息,如下所示:

ImportError : No Moduled Name "tensorflow.python"

导入错误:没有模块名称“tensorflow.python”

Message Errori dont have any idea with the error

消息错误我不知道该错误

回答by zhaozheng

Uninstall tensorflow:

卸载张量流:

pip uninstall tensorflow

Then reinstall it:

然后重新安装它:

pip install tensorflow

回答by Michal

for me upgrading piphelped,

对我来说升级有pip帮助,

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

回答by Narusan

Open a python shell and type:

打开一个 python shell 并输入:

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:

这将收集所有可用模块的列表。张量流不应该出现,因为它没有正确安装(根据traceback)。
然后:

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!

这将为您提供可以安装模块的系统路径列表。如果安装模块存在已知问题,我建议手动将文件移动到正确的系统路径。
系统路径取决于您使用的操作系统,所以不知道我无法告诉您将它移到哪里。
但是sys.path()可以!

Hope I could help, Narusan

希望我能帮上忙,Narusan

回答by Yuri Niitsuma

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

我在 Windows 10 中遇到了同样的问题。直到现在我不知道为什么。

But if I create an virtual environment

但是如果我创建一个虚拟环境

cd <your project path>

cd <your project path>

Install virtualenv

安装 virtualenv

pip install virtualenv

pip install virtualenv

Create the virtual environment

创建虚拟环境

virtualenv <envname>

virtualenv <envname>

Activate the env

激活环境

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

Then now you install tensorflow

然后现在你安装 tensorflow

(<envname>) $ pip install tensorflow

(<envname>) $ pip install tensorflow

And then run Hello World successfully.

然后成功运行Hello World。

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

*不要忘记每次虚拟环境jupyter、命令行等都需要激活或配置。

回答by Gursewak Singh

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

尝试将您的文件名更改为唯一的名称。显然里面存在同名的python脚本,这是导致问题的原因。我正在使用我的脚本,与 bert_base_tf_20.py 一起工作正常,但是当我将名称更改为 code.py 时,发生了这种情况。所以改回bert_code.py

Working fine

工作正常

回答by HISI

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.

尝试更改实际运行的python目录。并确保正在运行的 python 目录不是您下载 tensorflow 的位置。否则转到任何其他目录,你很好。我希望能解决你的问题。