0

What I'm trying to do seems really simple but I can't find a way to do it. I'm trying to use the module Pipreqs in a script, but I have to use subprocess.call() because Pipreqs doesn't have a way to use it in a script. Pipreqs uses logging to log info and I don't want that. Pipreqs also doesn't have a quiet mode. I tried to use logger.setLevel(logging.WARNING) but since I'm calling it through subprocess.call() it still prints info. I've also tried importing Pipreqs and setting the logging level to warning and that also doesn't work. Is there any way to disable this output? My code right now is the following:

import subprocess
import logging
import pipreqs
logger = logging.getLogger("pipreqs")
logger.setLevel(logging.WARNING)
subprocess.call(["pipreqs", "--force","/path/to/dir"])
3
  • Can't you just redirect stdout and ignore it? Commented Jun 27, 2020 at 20:06
  • It still prints the info Commented Jun 27, 2020 at 20:09
  • I haven't tried redirecting is to PIPE, but I've tried redirecting is to /dev/null and that didn't work. I could try redirecting it to PIPE but I don't want to buffer to fill up and I'm not even sure if that would work Commented Jun 27, 2020 at 20:10

1 Answer 1

2

You won't have access to the logger for an external process. The subprocess module does have flags for disabling output though.

subprocess.call(
  ["pipreqs", "--force","/path/to/dir"],
  stdout=subprocess.DEVNULL,
  stderr=subprocess.DEVNULL
)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I tried that before, but using a different way. I guess my way doesn't work
Yeah, stdout/stderr redirection isn't entirely uniform yet. E.g. contextlib.redirect_stdout() doesn't work for subprocesses, and it doesn't work for output generated by the logging module either -- even by the same process.

Your Answer

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.