0

I have got the answer for "how, inside a python script can I install packages using pip?" which is;

from pip._internal import main as pipmain

pipmain(['install', 'package-name'])

For backwards compatibility you can use:

try:
   from pip import main as pipmain
except ImportError:
   from pip._internal import main as pipmain

But if I run it,I get some output showing download information and progress. Is there any way to not get that(Make the code to just do its business behind the curtain)?

I tried a suggested answer(Below), but it doesnt seem to work.

import sys,os
def blockPrint():
    sys.stdout = open(os.devnull, 'w')
def enablePrint():
    sys.stdout = sys.__stdout__
blockPrint()    
try:
    from pip import main as pipmain
except ImportError:
    from pip._internal import main as pipmain
pipmain(['install', 'numpy'])
enablePrint()
print("hello")
19042003
  • 1
  • 1
  • @Carcigenicate I tried a few of the solutions but it doesn't seem to work.I'm new to python so I hope you are kind enough to give specific,to the point answer. – 19042003 Sep 13 '20 at 15:07
  • Does this answer your question https://stackoverflow.com/questions/8391411/how-to-block-calls-to-print – Krk Rama Krishna Sep 13 '20 at 15:19
  • @KrkRamaKrishna I tried but it still shows details of installation.Ive edited the question including that. – 19042003 Sep 14 '20 at 05:27

1 Answers1

0

I don't if this is what you what, but you can hide the console window by renaming your .py file to a .pyw file.

Andrej
  • 2,743
  • 2
  • 11
  • 28