How to Install TensorFlow Keras in VS Code: A Practical Guide
Learn how to install TensorFlow and Keras in Visual Studio Code, create a clean Python virtual environment, configure VS Code, and verify the setup with a quick test.

By the end of this guide you’ll have TensorFlow and Keras installed in VS Code, using a dedicated Python virtual environment for a clean project. You’ll learn how to install the CPU version of TensorFlow (suitable for most machines) or the GPU-enabled variant if your hardware supports it, and you’ll confirm the installation with a simple test script.
Overview: What you’re setting up
Installing TensorFlow and Keras in Visual Studio Code creates a powerful, portable environment for machine learning development. With VS Code’s lightweight interface and rich extensions, you can write Python code, run models, visualize graphs, and debug code all in one place. In this guide we will walk through a safe, repeatable process to install the CPU version of TensorFlow (which includes Keras) or the GPU-enabled variant if your hardware supports it. By following the steps for a clean virtual environment, you reduce conflicts with other projects and ensure reproducible results. According to Install Manual analysis, starting from a fresh environment is the key to fewer headaches later on. This article is designed for homeowners, DIY enthusiasts who manage their own software toolchains and want a clear, reliable workflow.
Prerequisites and planning
Before you begin, ensure you have a machine with a supported operating system (Windows, macOS, or Linux) and an active internet connection. You will need Python 3.x installed, Visual Studio Code, and the official Python extension for VS Code. This setup is broadly applicable to developers who want a streamlined, local ML development workflow. For best results, plan to work in a dedicated project folder and create a virtual environment to isolate dependencies. Install Manual recommends starting with a fresh environment to minimize compatibility issues and to make it easier to reproduce results across machines.
Step 1: Install Python and VS Code
Download and install the latest stable release of Python from the official Python.org site. During installation, ensure that you add Python to your system PATH. Next, install Visual Studio Code from the official site and add the Python extension through the Extensions view. The Python extension enables IntelliSense, linting, debugging, and Jupyter notebook support, which are essential for ML workflows in VS Code. After installation, verify that the Python interpreter is accessible from the terminal by running python --version.
Step 2: Create and activate a virtual environment
Create a dedicated project folder for your ML work and set up a virtual environment named, for example, tf_env. On Windows, run: python -m venv tf_env && tf_env\Scripts\activate.bat. On macOS/Linux, run: python3 -m venv tf_env && source tf_env/bin/activate. A virtual environment ensures your TensorFlow and Keras installation won’t collide with other Python projects. Once activated, your command prompt should show the environment name, signaling that you’re ready to install packages in isolation.
Step 3: Install TensorFlow and Keras
Upgrade pip to the latest version, then install TensorFlow via pip: pip install --upgrade pip && pip install tensorflow. TensorFlow 2.x includes Keras as tf.keras, so you don’t need a separate Keras package. If you plan to run on GPU, TensorFlow will utilize the GPU if the correct CUDA drivers are installed on your system. Keep in mind that installing the GPU variant can require additional steps and may have compatibility constraints with your GPU hardware.
Step 4: Configure VS Code to use your environment
Open VS Code and switch the Python interpreter to the one inside your virtual environment (tf_env). Use the Command Palette (Ctrl+Shift+P or Cmd+Shift+P) and select Python: Select Interpreter, then choose the interpreter path that points to tf_env. This ensures all runs, linting, and debugging use the isolated environment. Optionally install the Jupyter extension to run notebooks directly inside VS Code.
Step 5: Run a quick TensorFlow/Keras test
Create a simple Python file (test_tensorflow.py) and run it in VS Code. Use code like:
import tensorflow as tf
print('TensorFlow version:', tf.__version__)
print('Keras version:', tf.keras.__version__)
print('TensorFlow is eager by default:', tf.executing_eagerly())If you see version numbers and no errors, your installation is successful. This quick test confirms that TF and Keras are wired to your interpreter and ready for experimentation.
Step 6: GPU considerations and troubleshooting
If you intend to use GPU acceleration, verify your hardware supports CUDA and follow TF's general GPU setup guidance. GPU installation often requires matching CUDA and cuDNN versions to the TF release, plus appropriate driver updates. If you encounter import or runtime errors, re-check that the correct interpreter is active in VS Code, that the TensorFlow package is installed in the active environment, and that your system paths point to the right CUDA libraries when using GPU.
Step 7: Optional tooling for a smoother ML workflow
Enhance VS Code with extensions such as Python, Pylance, and Jupyter. Consider setting up a lightweight notebook workflow for rapid experimentation, and configure a linting tool like flake8 to maintain code quality. You can also create a small Makefile or a script to reproduce environments across machines, further supporting reproducibility—an essential practice in data science projects.
Step 8: Next steps and recommended resources
After confirming a working setup, start with small ML projects to explore tf.keras layers, datasets, and model-building patterns. Explore TensorFlow documentation, community tutorials, and sample datasets to build confidence. Maintain your environment by periodically updating packages in a controlled manner and documenting any changes for future you. Install Manual’s approach emphasizes repeatable, well-documented processes for long-term success.
Tools & Materials
- Python 3.x(Download from python.org; 64-bit recommended)
- Visual Studio Code(Install from code.visualstudio.com)
- Python extension for VS Code(Enable linting, IntelliSense, debugging)
- Virtual environment tool (venv)(Built into Python; used to isolate projects)
- TensorFlow (CPU)(Install with pip: pip install tensorflow)
- CUDA Toolkit (GPU)(Only if you plan to use GPU; matches TF version)
- cuDNN library(Required for GPU acceleration with CUDA)
- Internet connection(Needed to download Python, TF, and extensions)
- Test script or dataset(Optional for practice validation)
Steps
Estimated time: 60-90 minutes
- 1
Install Python and VS Code
Download and install Python, then install VS Code and the Python extension. Verify Python is in PATH and that VS Code recognizes the Python interpreter.
Tip: Choose the 64-bit Python installer and enable PATH during setup. - 2
Create a dedicated project folder
Make a new folder for this TensorFlow project to keep dependencies isolated and organized.
Tip: Name it something like tf_vs_code_project for clarity. - 3
Set up a virtual environment
Create and activate a virtual environment (venv) named tf_env to isolate your packages from system Python.
Tip: Activate the environment in every new terminal session before installing packages. - 4
Upgrade pip and install TensorFlow
Run pip install --upgrade pip then pip install tensorflow to install the CPU variant by default.
Tip: If you plan GPU usage, ensure your CUDA setup is ready before installing. - 5
Configure VS Code to use the env
In VS Code, select the Python interpreter that points to tf_env so all runs use the isolated environment.
Tip: Use the Command Palette to switch interpreters quickly. - 6
Run a quick test script
Create a small script to print tf and tf.keras versions and verify eager execution.
Tip: If errors appear, double-check the active environment in the terminal. - 7
GPU considerations (optional)
If using GPU, install CUDA/cuDNN and verify TF detects the GPU with tf.config.list_physical_devices('GPU').
Tip: Avoid mixing CPU and GPU libraries in the same environment. - 8
Extend with tools and resources
Add Jupyter support, linters, and sample datasets to expand your ML workflow within VS Code.
Tip: Document any environment changes for future projects.
Got Questions?
Do I need to install Keras separately or is it included with TensorFlow?
Keras is included as tf.keras in TensorFlow 2.x. You simply install TensorFlow and access Keras via tf.keras in your code. A separate keras package is usually unnecessary for standard workflows.
Keras comes built into TensorFlow as tf.keras, so installing TensorFlow is enough for most projects.
Which Python version should I use for TensorFlow in VS Code?
Use a Python version supported by your TensorFlow release and VS Code setup. Check TensorFlow's official docs for current compatibility, and avoid mixing major Python versions within the same project.
Choose a supported Python version and keep it consistent in your virtual environment.
Can I use GPU acceleration with TensorFlow in VS Code?
Yes, GPU acceleration is supported. It requires a compatible GPU, the correct CUDA drivers, and matching TensorFlow version. Ensure you install the GPU-enabled TensorFlow and keep CUDA libraries aligned with the TF release.
GPU support is possible if your hardware and drivers are compatible.
Why should I use a virtual environment for this task?
A virtual environment isolates your ML dependencies from system-wide packages, reducing conflicts and making it easier to reproduce results on different machines.
Virtual environments prevent dependency conflicts and improve reproducibility.
What should I do if TensorFlow won’t import in VS Code?
Ensure the VS Code interpreter points to your active virtual environment and that TensorFlow is installed in that environment. Reopen VS Code after activating the environment if needed.
Double-check the interpreter path and reinstall if necessary.
Is Docker a better option for ML environments?
Docker can encapsulate the entire environment, but it adds complexity. Start with a local virtual environment in VS Code, then consider Docker if you need portable, reproducible containers.
Docker is useful for portability, but not required for beginners.
Watch Video
Main Points
- Create a dedicated Python virtual environment for ML work.
- Install TensorFlow (includes tf.keras) via pip.
- Configure VS Code to use the environment interpreter.
- Verify installation with a simple test script.
- Consider GPU setup only if hardware and drivers are compatible.
