#🔒 Python automatically gets closed after being opened by hand.

130 messages · Page 1 of 1 (latest)

wicked locust
#

I made a python script, but it instantly closes if I try to run it by double clicking. when I run it through cmd, that doesn’t happen. Yes, I have multiple inputs in my file. and I am also using sqlite3.

paper dirgeBOT
#

@wicked locust

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

wide cloak
#

that's normal behavior

#

it's not python that's closing; it's the cmd.exe (or powershell.exe) window that it's running in

#

well python is "closing", in the sense that the python process exits. But that's what you'd want.

wicked locust
wide cloak
#

there might be a way to tell Windows to not do that. But if it were me, I'd just not start it by clicking an icon.

wicked locust
wide cloak
# wicked locust no? how?

unless your python program is a server that you intend to run forever, it is likely doing some work, and then exiting when that work is finished.

wide cloak
#

that's what I'd do

wicked locust
wide cloak
#

yes?

#

what detail have I missed?

wicked locust
#

well I have multiple inputs

#

it shouldn’t

wide cloak
#

I honestly don't know what "multiple inputs" means

wicked locust
#

literally

#

input()

wide cloak
#

oh, so it's interactive.

wicked locust
#

which requires inputs from the yser

#

user

wide cloak
#

I don't think that's relevant though

wicked locust
#

yes

wide cloak
#

what's relevant is: do you want the program to run forever, or not?

wicked locust
wide cloak
#

it is already a console application 🙂

wicked locust
wide cloak
#

ok, does your program have a "while True" or similar in it?

wicked locust
wicked locust
#

multiple

wide cloak
#

that's the accepted terminology

#

can you post the code? Maybe the real problem is that it's exiting unexpectedly

#

and since the window goes away, you can't read any error message that it might have printed

wicked locust
#

and it can be ran on windows 10

wide cloak
#

I am still waiting to see the code

wicked locust
#

how do I make the ‘

#

on a phone

wide cloak
#

no idea 😦

#

if you're serious about programming, you use a real computer.

wicked locust
wicked locust
#

rn

wide cloak
#

well wait until you can, then re-ask the question 🤷

wicked locust
#

yhe code was done on my computer

#

import sqlite3

con = sqlite3.connect("password_manager.db")
cur = con.cursor()

cur.execute("""CREATE TABLE IF NOT EXISTS informations (
title text,
username text,
password text
)""")

cur.execute("""CREATE TABLE IF NOT EXISTS masterpassword (
masterpassword text
)""")

result = cur.execute("""SELECT masterpassword FROM masterpassword""")

apass = ""
setpass = result.fetchone()

if setpass:
print("A Password Already Exists")
while True:
print("Please Enter Your Password.")
apass = input()
if apass == setpass[0]:
print("Password Is Correct.")
break
else:
print("Incorrect Input.")
else:
print("You haven't Set A Masterpassword Yet. Select A New Password:")
while True:
newpass = input()
if len(newpass) < 7:
print("Your Password Must Be At Least 8 Characters Long.")
else:
print("Successfully set your new password.")
break
cur.execute("INSERT INTO masterpassword VALUES (?)", [newpass])

while True:
a = 0
print("--------------------------------------------------------")
print("What would you like to do?")
print("1)Browse through Informations.")
print("2)Add Information.")
print("3)Close App")
choice = str(input())
if choice == "1":
title = cur.execute("""SELECT title FROM informations""").fetchall()
username = cur.execute("""SELECT username FROM informations""").fetchall()
password = cur.execute("""SELECT password FROM informations""").fetchall()
if title:
for i in title:
print("---------------------------")
print(title[a][0])
print(username[a][0])
print(password[a][0])
a += 1
print("---------------------------")
else:
print("--------------------------------------------------------")
print("""
_ _ _
| | | | | |
| | | | | |
|| || ||
(
) () ()
""")
print("You Have NO logged account information.")

elif choice == "2":
    print("What is the account branded? e.g. Youtube Account/Twitter Account")
    title = input()
    print("What is the account username?")
    username = input()
    print("What is the account password?")
    password = input()
    cur.execute("""INSERT INTO informations VALUES (?, ?, ?)""", (title, username, password))
    con.commit()
elif choice == "3":
    break
else:
    print("Invalid Option.Try Again.")

con.commit()
con.close()

paper dirgeBOT
#

Hey @wicked locust!

It looks like you are trying to paste code into this channel.

You seem to be using the wrong symbols to indicate where the code block should start. The correct symbols would be ```, not """.

Here is an example of how it should look:
```
Hello, world!
```

This will result in the following:

Hello, world!```
You can **edit your original message** to correct your code block.
wide cloak
#

that should be OK. Gimme a few while I look at it

#

I can't prove it, but I suspect your problem is that something in your loop has raised an exception.

wicked locust
#

import sqlite3

con = sqlite3.connect("password_manager.db")
cur = con.cursor()

cur.execute("""CREATE TABLE IF NOT EXISTS informations (
            title text,
            username text,
            password text
            )""")

cur.execute("""CREATE TABLE IF NOT EXISTS masterpassword (
            masterpassword text
            )""")

result = cur.execute("""SELECT masterpassword FROM masterpassword""")

apass = ""
setpass = result.fetchone()

if setpass:
    print("A Password Already Exists")
    while True:
        print("Please Enter Your Password.")
        apass = input()
        if apass == setpass[0]:
            print("Password Is Correct.")
            break
        else:
            print("Incorrect Input.")
else:
    print("You haven't Set A Masterpassword Yet. Select A New Password:")
    while True:
        newpass = input()
        if len(newpass) < 7:
            print("Your Password Must Be At Least 8 Characters Long.")
        else:
            print("Successfully set your new password.")
            break
    cur.execute("INSERT INTO masterpassword VALUES (?)", [newpass])    

while True:
    a = 0
    print("--------------------------------------------------------")
    print("What would you like to do?")
    print("1)Browse through Informations.")
    print("2)Add Information.")
    print("3)Close App")
    choice = str(input())
    if choice == "1":
        title = cur.execute("""SELECT title FROM informations""").fetchall()
        username = cur.execute("""SELECT username FROM informations""").fetchall()
        password = cur.execute("""SELECT password FROM informations""").fetchall()
        if title:
            for i in title:
                print("---------------------------")
                print(title[a][0])
                print(username[a][0])
                print(password[a][0])
                a += 1
                print("---------------------------")
        else:
            print("--------------------------------------------------------")
            print("""
             _   _   _ 
            | | | | | |
            | | | | | |
            |_| |_| |_|
            (_) (_) (_)
            """)
            print("You Have NO logged account information.")
            
    elif choice == "2":
        print("What is the account branded? e.g. Youtube Account/Twitter Account")
        title = input()
        print("What is the account username?")
        username = input()
        print("What is the account password?")
        password = input()
        cur.execute("""INSERT INTO informations VALUES (?, ?, ?)""", (title, username, password))
        con.commit()
    elif choice == "3":
        break
    else:
        print("Invalid Option.Try Again.")

con.commit()
con.close()
wide cloak
#

Since you don't catch exceptions, that will break the loop and exit the program, and that will close the window.

wicked locust
#

uhm why does it work on windows10 tho?

wide cloak
#

no idea

#

I don't use Windows

wicked locust
#

linux or mac?

wide cloak
#

both

#

I use linux for cloud servers, and mac for my laptop

wicked locust
#

linux on a mac?

worn glade
#

just tried it locally and it works (well, at the very least it doesn't close instantly)

wicked locust
#

oh alright

wide cloak
#

I use "linux on a mac" occasionally if I'm too lazy to deploy my code and run it on Linux

worn glade
#

linux but i doubt it matters

wide cloak
#

well it might.

#

Perhaps there's something in there that, on Windows 11, causes an exception.

wicked locust
#

i had a friend try it too

ionic root
wide cloak
#

@wicked locust do this on Windows 11: start a command window, then at the prompt, type py -3 c:/users/path/to/this/program.py and see what happens

wicked locust
#

but when I convert it to exe it doesnt work on work on win10 too 😦

wide cloak
#

if you're lucky, it'll die with an exception message, and then we can figure out what to do from that.

wide cloak
wicked locust
wide cloak
#

oh.

#

I've never actually gotten pyinstaller to work, and I strongly suspect that it adds a lot of complexity

#

I wouldn't use it if I didn't absolutely have to

wicked locust
#

but else your code is litteraly free for everyone to grab thats why I convert to exe

wide cloak
#

mm hmm

#

I simply don't worry about people stealing my code. Problem solved.

wicked locust
wide cloak
#

🤷

winter relic
wicked locust
#

doesnt really matter

wide cloak
#

up to you -- bragging rights with your friends, or simple reliable code.

wicked locust
winter relic
#

An exe can be reverse engineered to get the source code

#

Especially with using pyinstaller

wicked locust
#

ooh

#

okay but what about my problem?

winter relic
#

I'd run the executable in a cmd window

wicked locust
#

fine

#

it just opens like an application

#

like it is intended

winter relic
#

Where's the problem?

wicked locust
#

when I open it by double clicking the terminal opens and closes

#

and the application sort of dies

winter relic
#

screen record it

wicked locust
#

well thats not really possible

winter relic
#

then pause it when it's open and see what the error is

#

why?

wicked locust
#

well my laptop is dead and I am going to sleep in a little while

#

but I do have the code

#

and the file

winter relic
#

oh ok

#

well when you have the time, that's what I do

#

Screen record it and see what's happening

#

I'm also going to sleep so I can't run it rn

wicked locust
#

i mean I would probably take longer too

#

lets say 9-10 hours

winter relic
#

I would, but I have an extremely tough day tomorrow so I won't be around to help much, sorry

#

But other people may be able to

wicked locust
#

will the channel be closed?

#

by tomorrow?

winter relic
#

uhhhhh no clue, you'll have to ask someone else on that

#

you can always open a new one with a link to this

wicked locust
#

i’ll just open another one

#

yeah

#

thabks

#

gn

cerulean willow
#

Does the problem only happen when you turn your file to exe?

paper dirgeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.