bndr / pipreqs

pipreqs - Generate pip requirements.txt file based on imports of any project. Looking for maintainers to move this project forward.
Apache License 2.0
6.38k stars 388 forks source link

How to run pipreqs programatically inside python #451

Closed Eboubaker closed 4 months ago

Eboubaker commented 4 months ago

other libraries like pyarmor and pyinstaller allow you to launch the program from a python script. example:

import PyInstaller.__main__
PyInstaller.__main__.run([
    '--onefile',
    '--windowed',
    'main.py',
])

is equivalent to

pyinstaller --onefile --windowed main.py

How can I do this with pipreqs? We can't use the exec() function due to security reasons...

Eboubaker commented 4 months ago

ok i figured something

from pipreqs import pipreqs
from collections import defaultdict
args = defaultdict(lambda: None)
args['<path>'] = 'dist/'
args['--savepath'] = 'reqs.txt'
pipreqs.init(args)
pyinstaller_args = []
with open('reqs.txt', 'r', encoding='utf-8') as f:
    needs = [n.split('==')[0] for n in f.readlines()]
Eboubaker commented 4 months ago

or i can just directly use get_all_imports which is what i need in this case

needs = pipreqs.get_all_imports('dist/')
for n in needs:
    pyinstaller_args.append('--hidden-import=' + n)