Skip to content

Instantly share code, notes, and snippets.

@richardoey
Created April 6, 2021 06:20
Show Gist options
  • Save richardoey/87c161535568242dc6af1f9b82370183 to your computer and use it in GitHub Desktop.
Save richardoey/87c161535568242dc6af1f9b82370183 to your computer and use it in GitHub Desktop.
How to automatically install required packages from a python script as necessary?

How to automatically install required packages from a python script as necessary?

Based on here

Let's assume that your Python script is example.py:

import os
import time
import sys
import fnmatch
import requests
import urllib.request
from bs4 import BeautifulSoup
from multiprocessing.dummy import Pool as ThreadPool 
print('test')

You can use pipreqs to automatically generate a requirements.txt file based on the import statements that the Python script(s) contain. To use pipreqs, assuming that you are in the directory where example.py is located:
pip install pipreqs
pipreqs .

It will generate the following requirements.txt file:
requests==2.23.0
beautifulsoup4==4.9.1

which you can install with:

pip install -r requirements.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment