Thursday, September 25, 2025
HomeLanguagesHow to Automatically Install Required Packages From a Python Script?

How to Automatically Install Required Packages From a Python Script?

When working in python having to use libraries you don’t know or using a new pc, it is a hectic job to install all the libraries one by one. Each time you have to find out the name of the library and install it one by one. But if we know libraries like pipreqs which automatically installs all the required libraries to run the program, it will make our job so easy and we can focus on the code rather than wasting time installing the libraries one by one.

What is Pipreqs?

It is a python library that Generates pip requirements.txt file based on imports of any project and then you can install all of them in one go.

Installing Pipreqs:

Run this command on your PC to install the pipreqs library.

pip install pipreqs

Step 1: Go to the directory in which your python script is present for example assume if we take this code in our directory.

Python3




import os
import requests
import urllib.request
from bs4 import BeautifulSoup
print('GFG is the best')


Step 2:In this directory run the following command that will create a requirement file.

pipreqs

Using pipereqs

The requirement file will look like this in our case.

sample requirement.txt file

This shows that request and beautifulesoup4 library are not present in our system and we must install them to run our program.

Step 3: Now run this command to install all the libraries required to run the program

pip install -r requirements.txt
installing all the libraries

installing all the libraries

Now if we run the code it will successfully run the code.

RELATED ARTICLES

Most Popular

Dominic
32320 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6683 POSTS0 COMMENTS
Nicole Veronica
11854 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11910 POSTS0 COMMENTS
Shaida Kate Naidoo
6795 POSTS0 COMMENTS
Ted Musemwa
7071 POSTS0 COMMENTS
Thapelo Manthata
6755 POSTS0 COMMENTS
Umr Jansen
6762 POSTS0 COMMENTS