How to determine packages used in a Python project that has no requirements.txt

How to determine packages used in a Python project that has no requirements.txt

Python projects that do not come with a requirements.txt can be a nightmare to work with. Luckily, there is a solution

·

3 min read

Have you ever cloned a python project on GitHub that did not include a requirements.txt and you could not make it run on your local machine due to compatibility issues? What if there was a solution that could crawl the whole project and generate a requirements.txt file for you for the packages used and their versions? Well, good news there is!

Prerequisites

  • You need to have python 3.5+ installed - Download python here

  • Pip package installer- Download pip from here

  • Venv for creating virtual environments - Download Venv from here

Working with projects that do not have a requirements.txt

I came across this issue when I was following machine learning tutorials on Microsft Learn. The GitHub repo only has the code with no requirements.txt. I was particularly interested in the code for extracting text from images and pdfs. The code looks as shown in the image below.

mslearn.png

You can see there is no requirements.txt. We have to first clone the whole repo then navigate to the specific folder we want to work on

repo.png

Start by cloning the whole repository.

git clone https://github.com/MicrosoftLearning/AI-102-AIEngineer.git

Then get into the specific folder we are interested in

cd AI-102-AIEngineer-master\20-ocr\Python\read-text\

The project structure is as shown below ``` AI-102-AIEngineer-master └───20-ocr | └───Python | | └───read-text | | | read-text.py | | | .env | | └───images | | | | Lincoln.jpg | | | | Note.jpg | | | | Rome.pdf


## Setting up an environment

Setting up an environment for a project ensures that the installs done for the project are within that environment without affecting global installs. This can also be useful when a specific package is only installed once on a specific project.
To set up a venv environment:

```shell
python -m venv name-of-environment

Name the environment folder that you wish. I'll name mine venv. My current folder structure now looks like ``` AI-102-AIEngineer-master └───20-ocr | └───Python | | └───read-text | | | read-text.py | | | .env | | | venv | | └───images | | | | Lincoln.jpg | | | | Note.jpg | | | | Rome.pdf


On windows activate the environment 
```shell
 cd venv\Scripts\ && activate && cd .. && cd ..

With the environment set, next, we need to install (Pigar)[pypi.org/project/pigar/]. Pigar is a tool used to generate requirements.txt for python projects. Install Pigar with pip

pip install pigar

To use pigar on our project simply run

pigar

You can find a generated requirements.txt on your project folder. Learn more about pigar from its documentation. With a requirements.txt, install all the dependencies using

pip install -r requirements.txt

Happy hacking!