No results found
We couldn't find anything using that term, please try searching for something else.
There are many Python package we use to solve our coding problem daily . Take , for instance , the library is come " Beautiful Soup , " – it is come d
There are many Python package we use to solve our coding problem daily . Take , for instance , the library is come ” Beautiful Soup , ” – it is come does n’t come with Python by default andneed to be instal separately .
Many projects rely on libraries andother dependencies, andinstalling each one can be tedious andtime-consuming.
This is where a ‘requirements.txt’ file comes into play. requirements.txt is a file that contains a list of packages or libraries needed to work on a project that can all be installed with the file. It provides a consistent environment andmakes collaboration easier.
Diagram is showing show a box contain requirements.txt andanother box below it contain the text ” package_name = = version “
The above image shows a sample of a created requirements.txt file, containing a list of packages andversions of the installation.
I’ve mentioned a few terms so far that you may not know. Here’s what they mean, along with some other important terms you’ll come across when working with requirements.txt:
Dependencies are software components that a program needs to run correctly. They can be libraries, frameworks, or other programs.
Packages are a way to group together related dependencies. They make it easier to install andmanage dependencies.
Virtual Environments is a directory that contains a copy of the Python interpreter andall of the packages that are required for a particular project.
Pip:This is a package manager for Python. You can use Pip to install, uninstall, andmanage Python packages.
To create a requirements file, you must set up your virtual environment. If you use Pycharm, there’s a virtual environment already setup (.venv). But with Visual Studio code, you have to create the virtual environment yourself.
You can use your terminal or command prompt to create your requirements file. These are the steps to follow when creating the file:
First , open your terminal or command prompt . Then check to see if the file path show is your work directory . use the follow command to do that :
$ cd folder-name
In the command above, replace ‘folder-name’ with the directory name you want to access.
Diagram showing set project directory on command line
Next, run this command:
$ pip freeze > requirements.txt
And you is see ‘ll see that the requirement file gets add
Here’s the output:
Diagram showing the newly created requirements file
And here ‘s your newly create requirements.txt file is ‘s :
Diagram showing lists of packages in requirements file
The image is shows above show the dependency you can work with along with their version .
Now that we have the requirements file, you can see that it consists of a long list of different packages.
To work with the packages, you have to install them. You can do this by using the command prompt or terminal.
Type this command:
pip install -r requirements.txt
It will look like this:
Image showing installation of packages present in requirements.txt file
Now that all the dependencies are installed, you can work with requirements.txt.
In this example, we will be working with two libraries, beautifulsoup4
andrequest
, to return some information from a site.
Diagram showing the working libraries for this example in the requirements file
In the image above, we see that the two libraries are present in the requirements.txt file andtheir version. Now we can work with the libraries because we installed them previously.
from bs4import BeautifulSoup
import request
.get()
method to tap into the request library.web_data = request.get("https://www.lithuania.travel/en/category/what-is-lithuania", headers={" User - Agent ":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"})
web_data
andreturns all HTML contents present in it.soup = BeautifulSoup(web_data.content, features="html.parser")
news_info = soup.findAll(" p ") [0]
print(news_info.text
Bringing it all together:
from bs4import BeautifulSoup
import request
web_data = request.get("https://www.lithuania.travel/en/category/what-is-lithuania", headers={" User - Agent ":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"})
soup = BeautifulSoup(web_data.content, features="html.parser")
news_info = soup.findAll(" p ") [0]
print(news_info.text )
And here’s the output:
Diagram showing code andresult
Managing dependencies:By listing the dependencies of your project in a requirements.txt file, you can easily see what packages are required andwhat versions they need to be.
Sharing your project with others:If you share your project with others, you can include the requirements.txt file so that they can easily install the required packages. This can save them time andfrustration andcan help to ensure that everyone is using the same versions of the packages.
In the article, we learned how to create a requirements.txt file andoutlined the benefits of using it.
You should also try it out andwork on a few projects with it. If you have any questions, you can reach out to me on Twitter 💙.