DEV Community

David Marquis
David Marquis

Posted on • Originally published at davidmarquis.hashnode.dev

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

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
Enter fullscreen mode Exit fullscreen mode

For macOS and Linux:

python3 -m venv venv
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

For macOS and Linux:

source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

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.

Interpreter

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

  2. 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:

  • With your virtual environment activated, install the necessary packages using the pip package manager:
pip install streamlit
Enter fullscreen mode Exit fullscreen mode
  • Once you have all the required packages, create a "requirements.txt" file that lists them by running this command:
pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode
  • 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. Requirements

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
Enter fullscreen mode Exit fullscreen mode
  • 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 :)

Top comments (0)