1

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
3
  • Why do you need it? Just install them once before running your program. Commented Oct 8, 2019 at 9:51
  • @sanyash : And what if I want to develop on one machine and deploy on another, I would need dependencies to be installed there as well, Right ? Commented Oct 8, 2019 at 9:56
  • 1
    You can create a file requirements.txt and execute pip install -r requirements.txt. It is better than installing inside python code. More info here: stackoverflow.com/questions/41457612/… Commented Oct 8, 2019 at 10:00

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

first solution did not work getting error ( pip._internal.main(['install', pckg]) AttributeError: module 'pip' has no attribute '_internal')

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.