Automate upgrading pip in a Python virtual environment [venv]

A Bash script automates creating a Python virtual environment and upgrading pip. The script uses `python -m venv`, activation commands, and `pip install --upgrade pip`, then gets moved to `/usr/local/bin` so you can run it from anywhere with `source`.
Chapters
You know how every time you create a new Python virtual environment, there's several steps involved? First, you have to create the virtual environment, and then you have to activate it, and then you'll probably get prompted to upgrade pip in that process. All this could be annoying after a while, especially upgrading pip every time you create a virtual environment. Now let's go ahead and create this script that will automate this process for us. And please feel free to use any text editor that you are comfortable with using. In my case, I'm going to be using Vim. And if you want to follow along, please do. For me here, as you can see, I'm in my desktop. as my directory. And you can be in any directory so long as you know where you're in. For me, I'm using my desktop directory. And what I want to do first of all is create a file where I will have a script live in. Next thing you want to do is press I on your keyboard. That way you're able to edit the file. And the first part of the script I want to do is create the virtual environment. So what we're doing here, essentially we're just replicating the steps we usually do manually into this script in a step-by-step process so that it can execute it in the same steps as well. The next step is I want to activate the virtual environment. The next step, which is why we're here today, is upgrade pip. So we don't do this again manually ever again. And that's it for our code that we need to run this program. The next step you want to do is want to save this file. And if you're following along, press Escape on your keyboard and type in wq. And the wq here stands for write and then quit. Just enter and it's already saved. And if you do list, you should be able to see that file created already. It's right here. And this is what you're looking for. So if it's created there, the next thing we want to do is we want to make this file executable. By making it executable, we'll be able to run it afterwards as To do that, you want to change the mod of the file and make it executable. And we just put the name of the file there. What you want to do next is move this file to your PATH. That way it's executable in any file within your system. I'm moving it to /usr/local/bin. And you can see here as a verification, it's under /usr/local/bin where we actually want it to be. Now let's test it out and see if it works. What you want to do is type in a source. That way it executes within this shell itself and it can activate the virtual environment here. If you don't type that, it might not do that. So make sure you do that and the name of your executable file. And also after that, give it the name of the virtual environment. Remember that dollar sign we put it earlier? This is where you're replacing that with the name of the file that you want. So in this case, I'm putting it as virtual environment. And as you can see, it created a virtual environment. So this is the virtual environment file it created. And it went also ahead and after that, it upgraded pip. Now if I do pip list, you can see it shows us the pip is the latest version and everything else. It doesn't give us the warning that it usually gives us. And that's it. You never have to upgrade pip anymore. Give it a shot, let me know what you think, and see you and catch you in the next video. Bye! Bye!
More from Tony

Summarize Videos and Research Companies with CrewAI + Gemini Models [Quickstart Guide]

How to Make Your AI Agents More Reliable with CrewAI Task Guardrails (Step-by-Step Tutorial)








