Python Development Best Practices: Virtual Environments and requirements.txt
Discover the Game-Changing Secret of Elite Programmers and why their code doesn't break.
Hello, legends! It's your buddy Neokai, back with the third issue of our awesome newsletter, Hustle Data. To be honest guys, this week has been overwhelming for me with the work surrounding me. I've had a ton of work on my plate, and I can't believe how quickly this week flew by. But here I am, on a sunny Saturday morning(Yeah I thought I would be able to deliver it on Saturday), ready to share some exciting stuff with you all.
After that, I grabbed my laptop and got down to business. This week, my focus has been on delving deep into Linear Algebra, and I wanted to write on this topic, However, due to the recent Python update, I came across something unusual problem and thus decided to share the importance of Python virtual environments in this issue. Don’t worry guys, I’m planning to curate an excellent Linear Algebra guide that covers Linear Algebra’s essence deeply in Data Science. And let me tell you, legends, it's been quite the journey!
OVERVIEW
It's common to observe that experienced developers kick-start their workflow by setting up a virtual environment. Whether they are creating a YouTube tutorial, building a full-stack Python application, or just helping you out on Stack Overflow, virtual environments are a common practice among great programmers. When I was a beginner, I tried using virtual environments without understanding them completely. I ended up chuckling that it’s the same thing and there’s no use for it. One day, Suddenly, all the libraries I had previously installed seemed to disappear, causing my code to break and generating errors within import statements.
I googled lot many things and got to know when the Python version gets updated, sometimes the pip tends to point to the newest version by default and It might seem that all the libraries vanished, but they don’t. Instead of trying the PIP list, if we try PIP<earlier-version> list, one can view all the libraries installed.
Command: pip list
Command: pip3.10 list
I fixed this problem by visiting Environment Variable in Windows, and check if there are two different paths for Python pointing differently, and then just updating them. However, this is not a good practice.
After all the research I went through, I learned that the best practice is to store packages needed for each script in the requirements.txt
file and also separate environments so that each script has its own virtual environment with packages in compatible versions.
VIRTUAL ENVIRONMENT
Imagine you are working on Project A which requires a version of the Matplotlib library as X and there’s another Project B which requires a version of the Matplotlib library as Y. If we don’t have a virtual environment set up, both the projects are going to use a similar Matplotlib version installed, However, virtual environments help us to work with multiple versions of python with the multiple versions of different python packages at the same time.
First, we need to have a library called virtualenv
installed. You can do it by running pip install virtualenv
in your terminal window.
Now creating a virtual environment is fairly simple. All you need to do is open your terminal window and run the following command - virtualenv ProjectName
The Project Name can be anything of your choice.
Once you have created a virtual environment, such as "projectA," this will create a folder named “projectA” which contains environment details inside. Now navigate inside the folder and activate the environment.
Change the directory to the project folder.
Activate Virtual Environment.
We can confirm if virtual is activated when we notice that the command prompt or terminal prompt changes to indicate that you are now working within the virtual environment.
Now when we run the
pip list
inside this virtual environment, you will notice that only pre-installed packages are available such aspip
,setuptools,
andwheel
.
To deactivate the virtual environment all we need to do is run this command inside the folder -
Scripts\deactivate.bat
REQUIREMENTS.TXT
When we are working on projects with Python virtual environments, we can create a requirements.txt file that allows you to store all the packages used in your project with their available exact versions used.
Let’s install numpy
, pandas
, and matplotlib
libraries in our virtual environment.
Now it’s time to create requirements.txt
file
In conclusion, virtual environments and the requirements.txt file are the most important tools for Python developers. They provide a way to isolate project dependencies, manage different versions of Python, and ensure consistent and reproducible environments across different machines.
These practices contribute to productive collaboration, easier deployment, and efficient development workflows, ultimately increasing the reliability of Python projects.
If you have made it here, you are already on your way to following best programming practices. Best of luck in your journey and Stay tuned for further updates from Hustle Data.