I am a new bee in python programming, and want to write a python script file which I can use as an 'import' statement on top of first executable file of my python program which in turn will download all the required dependencies (pip install ).
Please help me with it.
asked Oct 8, 2019 at 9:42
Shamim Ahmad
8084 gold badges23 silver badges40 bronze badges
1 Answer 1
Try this using the pip module:
import pip
packages = ['numpy', 'pandas'] # etc (your packages)
for pckg in packages:
if hasattr(pip, 'main'):
pip.main(['install', pckg])
else:
pip._internal.main(['install', pckg])
an alternative would be:
import subprocess
import sys
packages = ['numpy', 'pandas'] # etc
for pckg in packages:
subprocess.call([sys.executable, "-m", "pip", "install", pckg])
answered Oct 8, 2019 at 9:47
Kostas Charitidis
3,1231 gold badge15 silver badges23 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Shamim Ahmad
first solution did not work getting error ( pip._internal.main(['install', pckg]) AttributeError: module 'pip' has no attribute '_internal')
lang-py
requirements.txtand executepip install -r requirements.txt. It is better than installing inside python code. More info here: stackoverflow.com/questions/41457612/…