r/learnpython • • Apr 16 '24

Ran about 8 Python scripts back-to-back, now none will execute

I got a little carried away writing automation scripts in ChatGPT when my .py files started closing instead of printing. Most were between 20-70 lines and imported between 1-4 packages. I’m still able to execute from the command line, just not from the .py files themselves. Is this something that happens when executing from .py files? I was hoping to share the files with coworkers but they won’t be too happy if the files stop working after a handful of runs. Did I do something wrong?

Edit: a little more info - I copy/pasted the code that GPT wrote into a .py file. The first handful of times I did this it would run, after about the 5th time the code that was just working-I didn’t edit at all-just closed. I wasn’t using an IDE, just running as a .py file.

0 Upvotes

21 comments sorted by

7

u/chooseyourshoes Apr 16 '24

This is how AI ends up taking over. People mindlessly adding code that AI generated and running it repeatedly on their machines. Lmfao we’re fucked.

6

u/wildpantz Apr 16 '24

If you just double clicked your .py files, they will close if they are finished executing the code

you could add one more line in the end and test opening then:

a = input("This prevents windows from closing until you give it input")

5

u/NYX_T_RYX Apr 16 '24

Quick fire questions - have you ever written code before?

Have you ever written python before?

What is your code supposed to do (ie what did you tell gpt you wanted)?

What is your code? (no one can tell you what it is actually going to do if we don't know what it is)

And finally, when you asked gpt for code, did you then prompt "please explain this code"?

I'm no expert, but the way you're describing things appears you don't fully understand what you've done (not a criticism, everyone starts somewhere and god knows I still dunno how to describe half the stuff I'm doing - it's an issue with teaching yourself, I know what this thing is. No clue what it's called so I can't describe it well)

py files started closing instead of printing

I'm not even sure what this line means, but I assume you mean the place you're writing the code closes, and it doesn't show (print()) an output?

In short, I suspect they're working exactly as written. It's short code (I'd expect less than 100 lines as a beginner effort to take seconds to complete running). Most likely they're not actually doing what you wanted anyway, but the lack of output will be simply cus they're closing as soon as either a. They reach the last line or b. They reach a line with an error (exception).

4

u/Buttleston Apr 16 '24

no, this is not something that normally happens.

When you say "they don't run from the .py files", do you mean when you run them inside of your IDE?

When they close immediately, it's usually because it crashed. A common problem might be that you're running them in a different environment within your IDE than on the command line, and one of those environments is, say, missing dependencies or has the wrong versions, etc.

Aside from that, there's not enough information here to give any real advice

5

u/NYX_T_RYX Apr 16 '24

I think issue 1 is OP is writing these with gpt and, I presume, has no idea what is actually going on.

I think from the description - they were running them in an ide, but trying to run the .py file from explorer (etc) appears to instantly close.

I suspect the issue is whatever the file is trying to do can't be done from the location OP is trying to run it (relative file paths as a first guess)

1

u/Buttleston Apr 16 '24

Oh, yeah, even if a python interpreter is set up to auto-run files with a .py extension, that probably won't work without some setup

3

u/MiniMages Apr 16 '24

can't really help unless you share the code.

no one can tell you what is wrong with your code.

3

u/Sinusoidal_Fibonacci Apr 16 '24

Yes, you’re using ChatGPT.

3

u/Swipecat Apr 16 '24

I’m still able to execute from the command line, just not from the .py files themselves.

Can you clarify what you mean by this? What do you mean by "execute from the command line" and "executing from .py files"? i.e., what are you typing in each case?

5

u/rasputin1 Apr 16 '24

this is learnpython not learnhowtoavoidcodingbyhavingchatgptdoeverything

1

u/SunnyFoxes Apr 16 '24

redirecting to new subreddit. . . . . . . . subreddit not found... I'm disappointed.

2

u/Genotabby Apr 16 '24

Idk maybe Chatgpt removed the print statements? When the program automatically closes when there are no print statements, it could mean the program completed successfully and the terminal terminated. There would be an error message if there was some problem

2

u/CaptainFoyle Apr 16 '24

What do you mean with "running as a .py file"?

Also, perhaps it would help to learn coding instead of copy pasting shit from ChatGPT

1

u/Unlikely-Sympathy626 Apr 21 '24

Apart from don’t copy and paste gpt, and show us code so we can help.

Try running them front to front. lol.

But seriously you are not describing an issue. Well you are but not in way conductive to learning or providing help.

If interested in programming, pick up a book. Gpt is not going to teach you much and more often than not a waste of time unless if you got the basics it can really help getting boilerplate code up and running

0

u/[deleted] Apr 16 '24

[deleted]

6

u/NYX_T_RYX Apr 16 '24

Basically. Gpt is actually quite shit at math - it tried to tell me X=y/100 could give 0 division.

Anyways, useful tool for sure, but just the way OP is describing what's happening tells me they don't know anything about python, did a bit of googling and realised it's possible to do XYZ, then asked gpt to do XYZ, and didn't think to follow up with "thank you. Explain this code." So they understand it.

2

u/Genotabby Apr 16 '24

Gpt3.5 is shit at math. Gpt4 is much better but it helps if you ask it to come up with it as a code and analyse the result.

1

u/NYX_T_RYX Apr 16 '24

Perhaps.

But I'm not going to pay for a gpt4 when I get copilot free, and copilot (thus far) significantly better at writing useable code.

0

u/[deleted] Apr 16 '24

[deleted]

1

u/NYX_T_RYX Apr 16 '24

Curious...

Well, the code given to it was literally y=X/100 so no unorthodox self-dividing methods ;)

Good to know it isn't entirely a hallucination though!

1

u/blahreport Apr 16 '24

But already experienced programmers could become more efficient and reduce the number of available his jobs. I’m certainly more efficient on boilerplate stuff using CGPT. Also the models will improve. I’m sharpening up my plumbing skills myself