Python for Beginners: Installing, Creating Virtual Environments, and Freezing Requirements Made Easy

Photo by David Clode on Unsplash

Python for Beginners: Installing, Creating Virtual Environments, and Freezing Requirements Made Easy

A Comprehensive Guide to Setting up Python Projects with Virtual Environments

Welcome to the world of Python! In this beginner's guide, we'll walk you through the steps of installing Python, creating virtual environments, and freezing requirements. Learning and understanding the role of virtual environments changed the way I start developing a Python project and made sure I don't make a mess of my Python install.

A virtual environment is a personalised playground for each of your Python projects. It keeps them separate, helps manage dependencies, ensures consistency, and simplifies project cleanup. It's like having your own private space to play and explore without any interference from other projects or your base Python installation.


Step 1: Installing Python:

  1. Visit the official Python website at www.python.org.

  2. Look for the "Downloads" section and choose the version of Python suitable for your operating system.

  3. Download the Python installer and run it.

  4. During installation, make sure to check the box that says "Add Python to PATH" (Windows) or "Install for all users" (macOS and Linux).

  5. Follow the prompts and complete the installation.

Step 2: Creating Virtual Environments: Virtual environments help keep your Python projects organized and isolated. Here's how to create one:

  1. Open your command prompt or terminal.

  2. Navigate to or create a directory where you want to create your project (e.g., cd Documents/MyProject).

  3. Run the following command to create a virtual environment:

    • For Windows:

        python -m venv venv
      
    • For macOS and Linux:

        python3 -m venv venv
      

You can name the environment something more meaningful than 'venv'. I keep it as that because Github will ignore it by default when using the Python template.

  1. Activate the virtual environment:

    • For Windows:

        venv\Scripts\activate
      
    • For macOS and Linux:

        source venv/bin/activate
      

Step 3: Selecting the Interpreter in VS Code: VS Code allows you to select the Python interpreter for your project. Make sure you have the Python extension installed.

Here's how you can choose the Python interpreter:

  1. Open the folder where your virtual environment is located.

  2. Open the command palette by selecting "View" -> "Command Palette" from the top menu or using the shortcut Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (macOS).

  3. In the command palette, search for "Python: Select Interpreter" and select it.

  4. A list of available interpreters will be displayed. Choose the one corresponding to your virtual environment (e.g. "venv").

  5. VS Code will remember your selection and use it for running and debugging Python code within the project.

Great! You've successfully configured the interpreter for your project in VS Code.

Step 4: Freezing Requirements: Freezing requirements helps ensure consistent dependencies for your project. Follow these steps:

  1. With your virtual environment activated, install the necessary packages using the pip package manager:

     pip install streamlit
    
  2. Once you have all the required packages, create a "requirements.txt" file that lists them by running this command:

     pip freeze > requirements.txt
    
  3. You now have a "requirements.txt" file that captures all the packages and versions needed for your project. If you open the file you'll see a list of packages and the version.

Why freezing dependencies is a good idea?

Freezing requirements offers several benefits:

  1. Reproducibility: By documenting the exact package versions, you ensure that anyone working on your project can recreate the same development environment.

  2. Collaboration: When collaborating with others, sharing the "requirements.txt" file allows them to set up the same environment effortlessly. Everyone involved can work on the project with confidence, knowing they have the necessary dependencies.

  3. Deployment: When deploying your application to a production environment or sharing it with others, the "requirements.txt" file serves as a recipe for setting up the correct environment. It helps ensure that the required packages and versions are installed, reducing the chances of compatibility problems.

Best Practices:

  • It's a good practice to create a virtual environment for each project to keep dependencies separate.

  • Keep your virtual environment active while working on a project to ensure you're using the correct dependencies.

  • Regularly update your virtual environment and installed packages by running:

      pip install --upgrade pip
      pip install --upgrade -r requirements.txt
    
  • Consider using version control, such as Github, to manage your code and include the "requirements.txt" file in your repository for easy sharing. Also, remember to use the Github template for Python so it automatically ignores your virtual environment.

I wrote this to have a guide for when I get a fresh PC install to get up and running quickly. Hopefully, this is useful and easy to follow.

Here I am on LinkedIn, please also like and subscribe if you enjoy the read and to force me to keep writing :)

Did you find this article valuable?

Support Data Marquis by becoming a sponsor. Any amount is appreciated!