Automating Python Project Setup with Raycast Extension

In an earlier post, I shared a shell script to quickly set up new Python projects. Now, I’ve taken it a step further by creating a Raycast extension, making it even easier to start a new Python project with just a few keystrokes (I wrote about another Raycast extension to display Spotify song info here).

Here's what this script does:

  • Prompts for a project name as a command-line argument.
  • Creates a new project directory with the specified name.
  • Sets up a virtual environment within the project directory.
  • Installs required Python dependencies.
  • Creates sub-directories for source code, tests, and documentation.
  • Generates essential files like main.py, requirements.txt, and README.md.
  • Initializes a Git repository for version control.
  • Opens the main.py file in Visual Studio Code.

I wanted to create a Raycast extension for this script so that I could start new Python projects without even opening the terminal. This makes the process much quicker and more streamlined.

Creating the Raycast Extension

  1. Set Up the Raycast Script:
    • Open Raycast and go to the Extensions tab.
    • Click on "Create Script Command" and select "New Script Command."
  2. Add the Following Script:
    • Replace <YOUR_GITHUB_USERNAME> with your GitHub username.
    • Use the actual path to your project directory in the cd command.
#!/bin/bash

# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title New Python Project
# @raycast.mode compact

# Optional parameters:
# @raycast.icon 🤖
# @raycast.packageName Python Projects
# @raycast.argument1 { "type": "text", "placeholder": "Project name" }

# Documentation:
# @raycast.author Amit Jotwani
# @raycast.authorURL https://twitter.com/amit

# Replace <YOUR_GITHUB_USERNAME> with your actual GitHub username
GITHUB_USERNAME="ajot"

# Get project name from the argument
project_name=$1

# Check if project name is provided
if [ -z "$project_name" ]; then
  echo "Error: Please provide a project name."
  exit 1
fi

# Create project directory
cd "$HOME/Dropbox/dev_projects/python_projects" || { echo "Error: Directory $HOME/Dropbox/dev_projects/python_projects does not exist."; exit 1; }
mkdir "$project_name"
cd "$project_name" || { echo "Error: Failed to navigate to project directory."; exit 1; }

# Initialize virtual environment
virtualenv -p python3 venv
source venv/bin/activate

# Install dependencies (e.g., requests and numpy)
# pip install numpy

# Create project structure
mkdir src tests docs

# Create main script
touch src/main.py

# Create requirements.txt file
touch requirements.txt

# Create README.md file
touch README.md

# Initialize version control (Git)
git init

# Create a new repository on GitHub
gh repo create "$project_name" --private

# Set up the remote repository
git remote add origin "https://github.com/$GITHUB_USERNAME/$project_name.git"

echo "Current working directory: $(pwd)"
echo "Git repository initialized and remote repository set up."

echo "Project setup complete. Happy coding!"

# Open main.py in Visual Studio Code in a new window, and bring main.py to focus
code --new-window . --goto src/main.py

Setting Up the Extension

  • Save the script and make it executable by running chmod +x <script_name>.sh.
  • Open Raycast, go to "Extensions" and click "Import".
  • Select your script file and import it into Raycast.

Using the Extension

Now, whenever you need to start a new Python project, just use this Raycast command, enter the project name, and the automation will handle the rest.