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.

Shamim Ahmad
  • 808
  • 3
  • 22
  • 40
  • Why do you need it? Just install them once before running your program. – sanyassh Oct 08 '19 at 09: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 ? – Shamim Ahmad Oct 08 '19 at 09: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: https://stackoverflow.com/questions/41457612/how-to-use-requirements-txt-to-install-all-dependencies-in-a-python-project – sanyassh Oct 08 '19 at 10:00

1 Answers1

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])
Kostas Charitidis
  • 2,991
  • 1
  • 12
  • 23
  • first solution did not work getting error ( pip._internal.main(['install', pckg]) AttributeError: module 'pip' has no attribute '_internal') – Shamim Ahmad Oct 08 '19 at 10:10