Automate Your Project Setup

Automate Your Project Setup

with simple Python code

Tired of setting up project directories manually? Streamline workflow by automating tasks like creating directories, files, and setting up virtual environments. You're in the right place.!

In this blog post, I'll walk you through the process of creating a Python script to automate project setup. Whether you're a seasoned developer looking to optimize your workflow or a beginner eager to dive into automation, this guide is for you.

Why Automate Project Setup?

Setting up a new project involves repetitive tasks such as creating directories, initializing Git repositories, creating configuration files, and more. These tasks, while necessary, can be time-consuming and error-prone if done manually. By automating the setup process, you can ensure consistency across projects, reduce the chance of human error, and focus more on actual development tasks.

Requirements

For this tutorial, you'll need:

  • Basic knowledge of Python programming

  • A text editor or an IDE of your choice

  • Python installed on your system

Step 1: Define Project Structure

Before diving into coding, it's essential to define the structure of your project. Decide on the directory layout, subdirectories, and files you want to create for each new project. Common subdirectories include src for source code, docs for documentation, data for data files, and .env for environment variables.

Step 2: Write the Python Script

Now, let's start coding! We'll create a Python script that prompts the user for input, such as the parent directory path and project name, and then generates the project structure accordingly.

We'll use Python's built-in os module for file and directory operations and the subprocess module to execute system commands.

Here's the script:

import os
import subprocess


def create_project():
    # Step 1: Prompt for the parent directory path
    parent_dir = input("Enter the parent directory path: ")

    # Step 2: Prompt for project directory name
    project_name = input("Enter the project directory name: ")

    # Step 3: Sub-directories to be hard coded
    subdirectories = [
        "doc",
        "src",
        "data",
        "notes",
        ".env",
    ]

    # Step 4: Files to be created
    files_to_create = [
        "README.md",
        "scribble_pad.md",
    ]

    # Step 5: Create project directory
    project_path = os.path.join(
        parent_dir, project_name.lower()
    )  # Convert to lowercase
    os.makedirs(project_path, exist_ok=True)  # Ensure parent directory exists

    # Step 6: Create subdirectories
    for subdir in subdirectories:
        subdir_path = os.path.join(project_path, subdir)
        os.makedirs(subdir_path, exist_ok=True)  # Ensure subdirectories are created
        # Create main.py and test.py within the "src" subdirectory
        if subdir == "src":
            with open(
                os.path.join(subdir_path, "main.py"), "w"
            ) as f:  # Convert to lowercase
                pass  # Create an empty main.py file
            with open(
                os.path.join(subdir_path, "test.py"), "w"
            ) as f:  # Convert to lowercase
                pass  # Create an empty test.py file

    # Step 7: Create files
    for file_name in files_to_create:
        file_path = os.path.join(
            project_path, file_name.lower()
        )  # Convert to lowercase
        with open(file_path, "w") as f:
            pass  # Create an empty file

    # Step 8: Copy content from root .gitignore to project .gitignore if it exists
    root_gitignore_path = os.path.join(parent_dir, ".gitignore")
    project_gitignore_path = os.path.join(project_path, ".gitignore")
    if os.path.exists(root_gitignore_path):
        with open(root_gitignore_path, "r") as src_file, open(
            project_gitignore_path, "w"
        ) as dest_file:
            dest_file.write(src_file.read())

    # Step 9: Open cmd window and load project directory
    subprocess.run(f"cd {project_path} && start cmd", shell=True)

    # Step 10: Create virtual environment and activate
    subprocess.run(
        f"cd {project_path} && python -m venv .venv && .venv\\Scripts\\activate",
        shell=True,
    )


if __name__ == "__main__":
    create_project()

Step 3: Customize and Expand

Feel free to customize the script according to your specific project requirements. You can add more subdirectories, create additional files, or integrate version control systems like Git.

Step 4: Testing and Refinement

Before using the script in an actual project, it's essential to test it thoroughly in different environments and scenarios.

Step 5: Sharing and Collaboration

Once you're satisfied with your script, consider sharing it with the community! You can publish it on platforms like GitHub, GitLab, or Hashnode, where other developers can discover, use, and contribute to it.


Congratulations! You've learned how to automate project setup using Python scripting. By automating repetitive tasks, you can boost your productivity, maintain consistency across projects, and focus on what matters most—building great software.

Happy coding! 🚀