This is a bash script I wrote to automate the boring stuff I kept doing manually every time I started a new Python project. Every time I had to create a virtual environment, upgrade pip, set up a .gitignore and install packages, I was typing the same commands over and over. So I wrote a script that does all of it in one go. You just run it and your project is ready.
- Creates a Python virtual environment (or skips it if one already exists)
- Checks if
python3-venvis available and installs it automatically if not - Upgrades pip right after setup so you are never working with an outdated version
- Generates a
.gitignorefile with the most common Python entries already included - Installs
pandasandrequestsas the default packages - Logs everything to a
setup.logfile so you can see exactly what happened - Color coded terminal output so it is easy to tell what is info, what succeeded and what went wrong
| Tool | Purpose |
|---|---|
| Bash | The scripting language the whole thing is written in |
| Python3 venv | Creates the isolated virtual environment |
| pip | Installs and upgrades Python packages |
| tee | Writes terminal output to the log file at the same time |
1. Download or copy the script into your project folder and name it setup.sh
2. Give it permission to run
chmod +x setup.sh3. Run it
./setup.shThat is it. The script will handle everything from there.
Note: If
python3-venvis not installed on your machine, the script will try to install it automatically usingsudo apt. You may be asked for your password at that point.
When everything goes well the terminal output looks like this:
[INFO] Starting project setup...
[INFO] Creating virtual environment...
[SUCCESS] Virtual environment activated
[INFO] Upgrading pip...
[SUCCESS] pip upgraded successfully
[INFO] Creating .gitignore...
[SUCCESS] .gitignore created
[INFO] Installing required packages...
[SUCCESS] Packages installed successfully
[SUCCESS] Project setup complete!
Each line is color coded in the terminal. Blue for info, green for success, yellow for warnings and red for errors. The same output also gets saved to setup.log in your project folder automatically.
Getting the virtual environment activation to work inside the script
Bash scripts run in their own subprocess so activating a virtual environment inside a script does not automatically carry over to your current terminal session. I had to understand how source works and why it behaves differently inside a script versus typing it directly in the terminal. It took a bit of reading to get my head around it.
Figuring out how to log output to a file and the terminal at the same time
I wanted the output to show up in the terminal while also being saved to setup.log. The trick was using tee with process substitution: exec > >(tee -a "$LOG_FILE") 2>&1. It looks confusing at first but basically it redirects everything (including errors) to both places at once.
The color codes
Getting the ANSI color codes right was a small but annoying thing. I kept forgetting to add ${NC} at the end of each message to reset the color, which caused the whole terminal to stay green or red after a line printed. Simple fix once I spotted it but it tripped me up a couple of times.