3

Here's the program:

if os.name == 'posix' and getpass.getuser() != 'root':
  from subprocess import call

  call(["sudo", sys.executable, os.path.realpath(__file__), "--root-install"])

When I run it from the terminal it works fine:

> [sudo] Password for user:

But when I run it from PyCharm the terminal just stays blank.
I have also tried setting stdin=sys.stdin, stdout=sys.stdout manually, but that did not change anything.

What am I doing wrong here?

8
  • pycharm terminal doesn't support the sudo getpass method. You probably want pexpect: stackoverflow.com/questions/18046508/… Commented May 8, 2017 at 16:26
  • Thanks for the input. I thought about providing the password to sudo, but then how would I aquire said password? getpass.getpass does not seem to work in PyCharm either. Commented May 8, 2017 at 17:04
  • nope, getpass also needs the original terminal. You could try to detect it, and if not a normal terminal (testing for hasattr(sys.stdin,"isatty")) and if it's not a normal terminal, use some GUI like wx or tk to prompt for a password (hiding the letters) Commented May 8, 2017 at 17:07
  • hasattr(sys.stdin,"isatty") is always True. Both in a normal terminal and in PyCharms. Alternatives? Commented May 8, 2017 at 20:11
  • oh. It's false with Pyscripter. Okay... maybe hasattr(sys.stdin,"fileno") Commented May 8, 2017 at 20:15

1 Answer 1

1

PyCharm and IDEs generally don't like getpass-like inputs. Since sudo asks for the password in such a way, it's not runnable from a redirected IDE console.

Redirecting stdin from Popen won't change anything either.

Workaround: run the sudo command from a terminal. Example with xterm (sorry, I don't know much about nowadays terminals):

call(["xterm","-e","sudo", sys.executable, os.path.realpath(__file__), "--root-install"])
Sign up to request clarification or add additional context in comments.

3 Comments

Works! Not pretty but I'm just glad there is some solution. Very clever!
glad it works! I wonder why I didn't think of that sooner... Running windows here. Other problems :)
Had some windows issues too, but I know how to work around those haha, linux hacks are new for me. A standalone python program which installs its on dependencies surely is a challange for me.

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.