Jump to content
  • Advertisement
Sign in to follow this  
v0dKA

Using the Python Interpreter

This topic is 4413 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have several questions about using Python in interactive mode (from the shell). When one is writing code inside a block (in other words, the prompt is displayed as "..." instead of ">>>"), Python does not immediately show the output of your statements. I was hoping that it would compile what I typed and show me the output at least for the first iteration. The reason is that my last program was essentially contained within a giant loop. As such, I saw no advantage to using the interpreter. Another question about loops: as mentioned, my last loop was quite complex, since most of the logic of the program was inside of it. Let's say I make a typo in the second-to-last line. What Python does for you is report the error, and return back to the primary prompt (>>>). Uh-oh... does that mean that everything I typed in the loop so far is discarded and I have to start it over again? Is there a way to just return to where you were before you made the mistake and take over from there? Finally, let's say that I wanted everything I typed in the prompt to be exported to a .py script (perhaps the program did not turn out to be as discardable as I expected). Here's what the Python tutorials say on the matter:
Quote:
2.2.2 Executable Python Scripts On BSD'ish Unix systems, Python scripts can be made directly executable, like shell scripts, by putting the line #! /usr/bin/env python (assuming that the interpreter is on the user's PATH) at the beginning of the script and giving the file an executable mode. The "#!" must be the first two characters of the file. On some platforms, this first line must end with a Unix-style line ending ("\n"), not a Mac OS ("\r") or Windows ("\r\n") line ending. Note that the hash, or pound, character, "#", is used to start a comment in Python. The script can be given a executable mode, or permission, using the chmod command: $ chmod +x myscript.py
This is clearly aimed at *nix systems. What do I do on Windows? By the way, the Python interpreter does recognize this line: #! C:/ python I'm not entirely sure what this line does, but Python greets me back with the secondary prompt ("..."). What is Python waiting for? What am I supposed to write? I would appreciate any input on these questions. I would also like some clarification on just what the purpose of the interactive compiler is, as opposed to just using a non-interactive editor. Is the interpreter more aimed to "sandbox" programs where you just want to experiment with some feature? Discardable programs that accomplish a single, simple task that you won't need to do again? Or did they really intend the interpreter to be used for quick programs and full-fledged programs alike?

Share this post


Link to post
Share on other sites
Advertisement
I think (like all good forum posts) you ended up atleast partially answering your own questions. The purpose of the interactive interpeter is to fool around with the language more than anything else. When you are writing any remotely sizable program your going to want to have something you can edit/rerun.

As far as the scripting thing goes, on windows you just need to associate .py file with the interpeter (no need for a special line at the top).

Share this post


Link to post
Share on other sites
Quote:
Original post by Illumini
I think (like all good forum posts) you ended up atleast partially answering your own questions. The purpose of the interactive interpeter is to fool around with the language more than anything else. When you are writing any remotely sizable program your going to want to have something you can edit/rerun.

As far as the scripting thing goes, on windows you just need to associate .py file with the interpeter (no need for a special line at the top).


Thanks for the advice.

But could you explain the second part, exporting from the shell to a .py file? If I write a "sandbox" program in the shell, but decide to save it, surely I would have to execute some command in order to save it?

Share this post


Link to post
Share on other sites
Quote:
Original post by v0dKA
Quote:
Original post by Illumini
I think (like all good forum posts) you ended up atleast partially answering your own questions. The purpose of the interactive interpeter is to fool around with the language more than anything else. When you are writing any remotely sizable program your going to want to have something you can edit/rerun.

As far as the scripting thing goes, on windows you just need to associate .py file with the interpeter (no need for a special line at the top).


Thanks for the advice.

But could you explain the second part, exporting from the shell to a .py file? If I write a "sandbox" program in the shell, but decide to save it, surely I would have to execute some command in order to save it?


No, what he means is, associate the .py extension with python.exe ( or whatever your version is ).

In Explorer, go Tools-> Folder Options...
Selected the "File Types" tab
Scroll down to find the PY file type, click change, and set it to run with python.

From that point on, you can just double click a .py file, and python will automatically run and execute it.

Share this post


Link to post
Share on other sites
Right, if you actually use the interpeter to enter code and then want to save it I think your out of luck. That's not its purpose.

Share this post


Link to post
Share on other sites
If you are using the interpreter on the command line, i.e. from a console window, you should be aware that you can copy text out of console windows: see the options under the icon (it's a menu heading) in the top-left of the window. Just copy the text and paste it into a text document somewhere else, then change the file extension to .py . (If you have Windows configured to assume you're an idiot and protect you from file extensions, you may need to jump through some hoops to make it *really* have a .py extension, or perhaps disable those settings. If you are successful, your saved file should appear in the explorer with a green Python snake icon.) You may need to touch up the formatting, get rid of interpreter prompts and so on.

If you're using IDLE, you can similarly copy directly out of the interpreter window, into a new IDLE source file window. Two caveats:

- IDLE allows you to "copy" an empty selection, nullifying whatever was on the clipboard before.

- IDLE's clipboard will die when IDLE does (this has irritated me many times when trying to copy stuff to an external editor).

And similarly, you can paste text to the prompt, at either the command line or the IDLE interpreter, so you can "go back" that way to at least a limited extent.

As for your first question, the interpreter doesn't do that because there's no meaningful way it could be done. Consider for example:


>>> [23, 45, 92 *
...


What would you expect it to display? You're in the middle of a list so it's ok to wrap lines, but you're also in the middle of an expression, so it can't be evaluated until complete. Not to mention, the list isn't closed, so reporting [23, 45, (anything here)] would be a bit of a lie. No, the only thing it can (and does) do is wait until it has a complete expression. (BTW, if you look into the library a little, there are some modules with some rather interesting contents that practically build an interpreter for you... you can cause the interpreter to run itself, and add a layer of interpretation, repeat ad nauseam... wasting extra CPU/memory resources that way... ;) )

Share this post


Link to post
Share on other sites
Quote:
Original post by v0dKA
I have several questions about using Python in interactive mode (from the shell).

When one is writing code inside a block (in other words, the prompt is displayed as "..." instead of ">>>"), Python does not immediately show the output of your statements. I was hoping that it would compile what I typed and show me the output at least for the first iteration. The reason is that my last program was essentially contained within a giant loop. As such, I saw no advantage to using the interpreter.


Python does execute every statement as you enter it, however, if you start a loop, then that loop statement is not completed until you conclude the loop. When you conclude that loop, it will run and you'll see results.

Typing into the interpreter window is not really a way to use Python normally; rather, it's a way to test constructs and short snippets of code. For typical programs, you write a file and have it execute that. Just save the file with a .py extension and double-click it to run, or open it up from IDLE or the Python IDE of your choice.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!