Ensuring TensorFlow 2.0 or PyTorch Is Installed

What will you learn?

In this tutorial, you will learn how to confirm the installation of essential deep learning libraries like TensorFlow 2.0 or PyTorch in your Python environment. We will explore methods to check for their presence and install them if necessary, ensuring a seamless setup process for machine learning projects.

Introduction to the Problem and Solution

In the realm of artificial intelligence and machine learning, having access to powerful deep learning frameworks such as TensorFlow 2.0 and PyTorch is fundamental. Whether you are running existing models, developing new solutions, or experimenting with cutting-edge concepts, these libraries play a crucial role in your workflow.

To address the common scenario of needing either TensorFlow 2.0 or PyTorch in your Python environment, we will guide you through techniques to verify their existence and handle installations if they are missing. By automating this process through Python scripts, you can streamline project setups, ensure compatibility across diverse environments, and focus on building innovative AI applications.


import subprocess
import sys

def check_installation():
        import torch
        print("PyTorch is installed.")
    except ImportError:
            import tensorflow as tf
            print("TensorFlow is installed.")
        except ImportError:
            print("Neither TensorFlow 2.0 nor PyTorch is installed.")
            response = input("Do you want to install TensorFlow 2.x? (yes/no): ")
            if response.lower() == 'yes':
                subprocess.check_call([sys.executable, "-m", "pip", "install", "tensorflow"])
                print("TensorFlow has been successfully installed.")
                subprocess.check_call([sys.executable, "-m", "pip", "install", "torch"])
                print("PyTorch has been successfully installed.")

if __name__ == "__main__":

# Copyright PHD


The provided script checks for the presence of PyTorch and TensorFlow in your Python environment. If either library is missing, it prompts you to choose which one to install and handles the installation process seamlessly using subprocess and pip. This automated approach ensures that essential deep learning libraries are available for your projects.

  • The script first tries to import PyTorch and then TensorFlow.
  • If neither library is found, it offers installation options based on user input.
  • Installation commands are executed using subprocess module for a hassle-free setup experience.
  1. How do I check which version of TensorFlow or PyTorch I have?

  2. import tensorflow as tf; 
  3. import torch; print(torch.__version__)
  4. # Copyright PHD
  5. Can I choose which version of TensorFlow or PyTorh gets installed?

  6. Yes! Specify the desired version number when modifying the installation command.

  7. What should I do if installation fails due to permissions?

  8. Use elevated permissions by running the script with administrative rights.

  9. Can this method be used for other packages than TensorFlow and PyTorch?

  10. Absolutely! Customize package names in import statements and installation commands accordingly.

  11. Is there a difference between installing via this script vs manually through terminal/command prompt?

  12. Both methods achieve the same result; however, automation saves time when setting up multiple environments consistently.

  13. What are some common errors during installations and their fixes?

    • Compatibility issues: Ensure python versions align with package requirements.
    • Network errors: Verify stable internet connection.
    • Permission errors: Run with appropriate privileges as needed.
  14. Can both libraries coexist in one environment?

  15. Yes! Having both libraries won’t cause conflicts unless specified by project dependencies.


By ensuring the availability of Tensorflow 2.x or Pytorch in your Python environment, you pave the way for seamless initiation of AI & ML projects. Automating checks and installations simplifies setup processes, allowing you to focus on innovation within these dynamic fields.

Leave a Comment