Skip to content

CharlesEdeki/python_env_bootstrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Python Project Setup Script

Description

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.

Features

  • Creates a Python virtual environment (or skips it if one already exists)
  • Checks if python3-venv is available and installs it automatically if not
  • Upgrades pip right after setup so you are never working with an outdated version
  • Generates a .gitignore file with the most common Python entries already included
  • Installs pandas and requests as the default packages
  • Logs everything to a setup.log file 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

Tech Used

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

How to Execute

1. Download or copy the script into your project folder and name it setup.sh

2. Give it permission to run

chmod +x setup.sh

3. Run it

./setup.sh

That is it. The script will handle everything from there.

Note: If python3-venv is not installed on your machine, the script will try to install it automatically using sudo apt. You may be asked for your password at that point.

Output

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.

Challenges Faced

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.

About

No description, website, or topics provided.

Resources

Code of conduct

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages