Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

Import Error of tensorflow and keras in VS Code (Anaconda)

I just reeinstalled anaconda and visual studio code and try to set up an environment with the keras and tensorflow package. I'd like to use VSC for programming and I use anaconda to install packages. I managed to set up a new environment in anaconda and installed the packages in this environment using the anaconda prompt. I installed the gpu version of the packages in the following order: conda install -c anaconda tensorflow-gpu conda install -c anaconda keras-gpu Actually, I am not quite sure if it is necessary to install the gpu version...( I also installed some other packages like scipy-learn, pandas,...) When I open Spyder and test if the packages are available, I get no errors. However, when I used VSCode (started from the Anaconda Navigator) I can import neither keras nor tensorflow. I get the error

import tensorflow # Imports tensorflow ModuleNotFoundError: No module named 'tensorflow'

or

import keras # Imports keras ModuleNotFoundError: No module named 'keras'

The python file "TestPackages.py" that I like to run looks like this

import numpy as np 
import matplotlib.pyplot as plt
import pandas as pd 
import seaborn as sns 
import tensorflow as tf
import keras

However, when I try to import the packages (tensorflow/keras) in the Terminal, I get no errors. The Terminal starts with something like this:

(MyEnv) D:....

That looks good since VLC has recognized my Environment (MyEnv). When i call Python with

(MyEnv) D:....>python

and then try

>>>import tensorflow as tf

I get no errors (same thing happens if I try to import keras this way). So, what's the reason for these import errors when I run the beforementioned file TestPackages.py?

(Versions: python 3.8.5, keras 2.4.3, tensorflow 2.3.0)

question from:https://stackoverflow.com/questions/65925844/import-error-of-tensorflow-and-keras-in-vs-code-anaconda

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...