Introduction
Welcome to my 43rd blog post on the Python journey. On day 43, I learned about a very interesting concept in Python of the Virtual environment. This can help run multiple versions of any package and work on multiple projects without any dependencies on a single machine. Let's dive into more details.
So let's get started......
Virtual Environment
It is a tool used to isolate one Python environment from another on a single machine allowing one to work on multiple versions of Python or any package on multiple projects without conflicts.
This can be useful when working on projects that have conflicting package versions or packages that are not compatible with each other.
We can use 'venv' module that comes with Python to create the virtual environment.
Example -
Create a virtual environment and activate it.
# Create a virtual environment
python -m venv myenv
# Activate the virtual environment (Linux/macOS)
source myenv/bin/activate
# Activate the virtual environment (Windows)
myenv\Scripts\activate.bat
Once the virtual environment is activated, any packages that are installed using pip will be installed in the virtual environment, and not in the global Python environment.
Packages installed in the virtual environment allow us to have a separate set of packages for each project, without affecting the packages installed in the global environment.
Deactivate the virtual environment
To deactivate the virtual environment, you can use the deactivate command:
# Deactivate the virtual environment
deactivate
' requirement.txt ' file
A requirements.txt file lists the packages and their versions that your project depends on.
This file can be used to easily install all the required packages in a new environment.
To create a requirements.txt file, use the 'pip freeze' command, which outputs a list of installed packages and their versions. For example:
Example -
# Output the list of installed packages and their versions to a file
pip freeze > requirements.txt
To install the packages listed in the requirements.txt file, you can use the pip install command with the -r flag:
# Install the packages listed in the requirements.txt file
pip install -r requirements.txt
Using a virtual environment and a requirements.txt file can help us manage the dependencies for your Python projects and ensure that our projects are portable and can be easily set up on a new machine.
Resources Used
You can watch the video of Day#43 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. Through this Blog post, we learned about creating a virtual environment which is a tool to isolate multiple Python versions and its packages on a single machine. This enables us to work on multiple projects and packages without conflicts. We also looked at how to activate and deactivate virtual environments and make use of ' requirement.txt ' file
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #43
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.