简体   繁体   中英

Visual Studio Code is rejecting the Tensorflow installation in a Virtual Environment

I created a virtual environment called env using

python -m venv env

.\\env\\Scripts\\activate.bat

pip install tensorflow

I verified tensorflow is in the env\\Lib\\site-packages folder

Next I loaded VS Code and created a workspace, added a python file, it prompted me to install pylint, I typed in python: select interpreter and I browsed to C:\\Users\\admin\\env\\Scripts folder

This is the command line at the beginning of the script

(env) PS C:\Users\admin\env\project>  cd 'c:\Users\admin\env\project'; & 'C:\Users\admin\env\Scripts\python.exe' 'c:\Users\admin\.vscode\extensions\ms-python.python-2020.8.106424\pythonFiles\lib\python\debugpy\launcher' '54436' '--' 'c:\Users\admin\env\project\face_gan.py'

This is the error I get when debugging the python file:

ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via `pip install tensorflow`
PS C:\Users\admin\env\project> & C:/Users/admin/env/Scripts/Activate.ps1

When I type in pip install tensorflow in VS Code terminal, it shows its already installed

(env) PS C:\Users\admin\env\project> pip install tensorflow
Requirement already satisfied: tensorflow in c:\users\admin\env\lib\site-packages (2.3.0)

I don't understand this, is it not running in virtual environment? Why is it executing C:/Users/admin/env/Scripts/Activate.ps1 at the end of the debugging session, not at the beginning

Lastly, is running python from the virtual environment folder C:\\Users\\admin\\env\\Scripts the same as using the activate.bat file or the source command? Does it automatically defer to using the C:\\Users\\admin\\env\\Lib folder, or is it still trying to use the default python installation to look for Tensorflow?

What step did I miss to make it use the virtual environment correct in VS Code?

First question: executing C:/Users/admin/env/Scripts/Activate.ps1 after debugging command make no difference. It just because it's the first command of the terminal. You can run it again to make a try.

Second question: Yes, that's the same. In your case, it will add 'C:\\Users\\admin\\env' and 'C:\\Users\\admin\\env\\lib\\site-packages' path to the PYTHONPATH variable.

You can through these codes to get the PYTHONPATH(the default search path for module files) variable value:

import sys
print(sys.path)

If you import 'tensorflow' directly. you will find you can import it correctly. It's a version problem. You should downgrade the version of the packages, and you can refer to this comment to get some useful information.

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