0

This is a follow up question (not directly related, but maybe related in some way)

I am working with this code:

#! python3
# password.py - An insecure password locker program.
PASSWORDS = {'email': 'F7minlBDDuvMJuxESSKHFhTxFtjVB6',
 'blog': 'VmALvQyKAxiVH5G8v01if1MLZF3sdt',
 'luggage': '12345'}  

import sys,pyperclip
if len(sys.argv) < 2:
    print('Usage: python pw.py [account] - copy account password')
    sys.exit()
account = sys.argv[1] # first command line arg is the account name

if account in PASSWORDS:
    pyperclip.copy(PASSWORDS[account])
    print('Password for ' + account + ' copied to clipboard.')
else:
    print('There is no account named ' + account)
input('press ENTER to exit')

And trying to run it from the python.exe app. I have followed the guidlines from my textbook:

enter image description here

And created a .bat batch file with the following name:

@py.exe C:\PythonScripts\password.py %*

(C:\PythonScripts is my path)

So, my problem is:

When I double click the program (or win+R and type it the program) the window pops up for a split second. If for example I put input("press enter to exit") in the beginning, it ask me to press centre (once) and then the window vanishes. I know there is a problem in one of the steps I'm doing here because if I execute the program in the command prompt, it runs:

enter image description here

enter image description here

When I double click the icon password.py very quickly multiple times, this is the window that i'm talking about (that shows for a split second). If I put input("press enter") in the beginning of my code, there will be the words "press enter" and i'll click enter to exit. But then the program doesn't seem to execute properly (the window closes immediately).

Slim Shady
  • 220
  • 3
  • 18
  • im confused, are you just trying to run a python script file? – griffin_cosgrove Jun 30 '20 at 13:38
  • @griffin_cosgrove Yes, and I'd like to just be able to double click it (with the .exe file) and not type in to command prompt every time – Slim Shady Jun 30 '20 at 13:42
  • what do you mean the .exe file, the python script will still be a .py file? am i missing something here – griffin_cosgrove Jun 30 '20 at 13:51
  • Put `input()` before `sys.exit()`. Double-clicking a file doesn't pass arguments to it – ForceBru Jun 30 '20 at 14:02
  • @griffin_cosgrove see my edit – Slim Shady Jun 30 '20 at 14:03
  • @ThePoorJew, it does run, that's why "the window pops up for a split second" - it's just completes too quickly because you never supplied any arguments, so it exits (`sys.exit()`) almost immediately – ForceBru Jun 30 '20 at 14:07
  • @ForceBru this worked, I now have "'Usage: python pw.py [account] - copy account password'" printed out, but the program exits when I type in something. How do I make it run normally? Normally as in like in the Command Prompt? – Slim Shady Jun 30 '20 at 14:07
  • 1
    @ThePoorJew, you need to supply the command-line arguments, as you did in the CMD. I think you can run "py.exe your_script.py the arguments" from the Run tool – ForceBru Jun 30 '20 at 14:09
  • @ForceBru actually typing password.py email did the work for me. Thank you – Slim Shady Jun 30 '20 at 14:12

1 Answers1

2

I think this is what you are looking to do. Alternatively, there is more information on this Stack Overflow question.

Excerpt from the first link:

Right click on the icon of hello.py and check Properties->Opens with setting. To be sure, you may want to click Change...->Browse... and select the python executable. Set the checkmark Always use the selected program to open this kind of file.

Passing arguments Lets create a shortcut that passes some arguments to the script.

  • hello.py -> Right click -> Create Shortcut

  • Shortcut to hello.py -> Right click -> Properties

  • Add --somevalue at the end of Target

      - "C:\Documents and Settings\Administrator\Desktop\hello.py" --somevalue
    
  • Click Apply

Double clicking on Shortcut to hello.py should now bring up a console window displaying something similar to

  • hello world "['C:\\Documents and Settings\\Administrator\\Desktop\\hello.py", '--somevalue']"

If this works including the --somevalue output you are all done. Otherwise, read on to the interesting part.

griffin_cosgrove
  • 419
  • 8
  • 16