Beginning Python Question - Page 1
Forum section: electronics / general

author: bostonman
author_rank: Super Contributor
timestamp: March 17, 2022, 03:29:47 am
content: Sometime ago I began tinkering with Python a bit by installing it on an old XP machine.I noticed that it installed a 'command line' and a 'GUI'. Both of these seem to be the same thing except one is in a DOS window and the other is an actual Window. In both cases, it seems typing print("Hello World") gets 'Hello World' printed on the screen, but seems it's not a line-by-line list of code that I can save.Using Notepad++ allows me to type multiple lines of code, save it as 'file.py' and, if I click on it, will execute. Unless I type #input('Press Enter to Exit...') at the end, the window opens and closes at the blink of an eye.Up to now, think I'm doing things correctly. The problem seems to be, when I type many lines of code (ideally five or six because I'm just a beginner), and a line or lines contain an error, all I get is the flash of a window executing the program.The little C that I know involves something like Visual Studio, compiling, and, if an error exists, it will report which line(s) have errors without successfully compiling. Obviously it's not always an ideal error statement, but it helps know where the program is having issues.I'm looking for some initial guidance to understanding the setup for Python or how to type code and know if I'm typing errors. Up till now, I've placed the #input('Press Enter to Exit...') statement after a line, run the program, if the window doesn't blink open and then close, I move the statement after the next line, so on and so on.Obviously an easier way must exist. Also, after I create a file.py, how do I make it so it can't be edited? Again, with C, it's compiled, so you can't really take the .exe and see the list of C code.

author: retiredfeline
author_rank: Frequent Contributor
timestamp: March 17, 2022, 04:02:06 am
content: You can use VS to develop in Python, just install the appropriate addons.

author: Ian.M
author_rank: Super Contributor
timestamp: March 17, 2022, 04:06:12 am
content: You probably should use IDLE, the Python IDE. Its got a shell for executing Python code, an editor with syntax highlighting, a debugger + various other stuff.N.B. on Linux it doesn't install by default with Python so you may need to install it, maybe with:sudo apt-get install idleor whatever your distro uses to install packages.The idea of using MS Visual Studio for Python, (except if you are trying to integrate with compiled code in a MS centric language) is abhorrent!

author: retiredfeline
author_rank: Frequent Contributor
timestamp: March 17, 2022, 04:15:13 am
content: Why would it be abhorrent? VS is open source and cross-platform, and supports lots of programming environments. including some embedded platforms. It's not MS lock-in.

author: newbrain
author_rank: Super Contributor
timestamp: March 17, 2022, 06:51:44 am
content: Quote from: retiredfeline on March 17, 2022, 04:15:13 amNope, not in the least.You are thinking ofVisual StudioCode, which isopen source-ish(if you get the binaries, OpenSource if get the code orVS Codium).That said both VS (thecommunity editionis free to use) and VSC are excellent IDEs to develop in Python.The elephant in the room here is XP: I think neither runs on XP, and I'd have some doubts even with Windows 7.Any editor will do, notepad++ is fine too, especially at the beginning. As you progress to more complex stuff, though, you'll really want something more integrated.If the OP cannot use a better OS (W7, W10, a [d|r]ecent Linux for VSC), Idle probably still runs on XP.To avoid the flashing cmd window issue, just launch the python program from an already opened one.Edit: added links to Code, VS Codium and Visual Studio.According to its System Requirements, VS Code state is supported from Windows 8 onwards.

author: retiredfeline
author_rank: Frequent Contributor
timestamp: March 17, 2022, 06:55:49 am
content: Sure VS Code is the one I mean.BTW Python3 packages for XP ended at 3.4, and Python3's up to 3.10 now, although there seems to be a 3.6 build around. If you really want to learn the language and not hit feature not implemented issues, do yourself a favour and use a modern OS.

author: ledtester
author_rank: Super Contributor
timestamp: March 17, 2022, 09:39:26 am
content: Quote from: bostonman on March 17, 2022, 03:29:47 amHere's a short video on how to use IDLE which comes with python:https://youtu.be/bOvqYw1SZJgIDLE has a "Check module" menu item which will check your code for syntax errors.QuoteOne way is with file permissions -- remove write permission from the file and then the file cannot be changed.However, you generally can't prevent someone from reading the file if you also want them to be able to execute it. I.e., if someone can execute your python script they can also bring it up in a text editor and see what it does.Update: And here's a video demonstrating how to use the debugger in IDLE:https://youtu.be/CQin42wFC-wIt has all the usual debugger amenities -- step one statement, skip over one statement, execute until function call returns, etc.

author: tszaboo
author_rank: Super Contributor
timestamp: March 17, 2022, 11:13:47 am
content: For simple code, learning and such, familiarize yourself with Jupyter notebook.It runs in your browser, and it is super simple to start.You can run your code block by block, and the results are printed below. Everything is saved. It has code completion, and it is native python.Install the classic Jupyter Notebook with:pip install notebookTo run the notebook:jupyter notebook

author: RoGeorge
author_rank: Super Contributor
timestamp: March 17, 2022, 11:34:36 am
content: Try IDLE. It's a minimal editor, AFAIK on Windows it comes bundled with Python, so if you installed Python, you should have IDLE, too. Start it from the windows "Start" menu. IDLE has 2 windows, one for editing the Python code, and another to run/debug. There are many other ways to do it, just that IDLE usually installs together with Python in Windows (but I didn't used Windows lately).IDLE works directly from Windows and will invoke Python command line automatically when needed, so easier to start with.

author: rdl
author_rank: Super Contributor
timestamp: March 17, 2022, 11:50:14 am
content: Open the command window first and navigate to wherever your file.py is and type in the command manually. If it errors out the command window will stay open.

author: TomS_
author_rank: Frequent Contributor
timestamp: March 30, 2022, 09:49:48 pm
content: You don't need to keep moving a statement one line at a time until you figure out where the error is, the error message (stack trace) tells you exactly where it is. I would definitely spend a bit of time familiarising yourself with the output of a stack trace because it can be a very useful debugging tool.But maybe you can change your code a little bit to make this easier to work with. Assuming you are not creating a class to contain your code you could do the following:Code:def main():Put your code in hereif __name__ == "__main__":main()input('Press Enter to Exit...')This gives you an exit prompt before your program exits whether it errors or not.But to fix the whole issue of the window flashing up and disappearing, run your script from a terminal, i.e. the command prompt or inside an editor, directly. This way the window does not close once the script finishes running and you get to retain the output for later review.

author: rstofer
author_rank: Super Contributor
timestamp: June 02, 2022, 04:43:31 pm
content: It is worth knowing how to use Visual Studio Code as it can be used for a number of languages and on a number of platforms like Linux, macOS and Windows 8 and beyond. No, it won't work on XP but I left that platform a long time ago.If you have a wide monitor, it can be helpful to have two files displayed side-by-side at the same time. Your main code and the library code simultaneously.I also use Thonny for microPython on Windows. It works well enough but it surely isn't VS Code.gedit and geany work well on Linux. I run the code in a separate terminal from the editor. Works well!

author: SiliconWizard
author_rank: Super Contributor
timestamp: June 02, 2022, 05:34:45 pm
content: Python stopped being supported on Win 7 as well, starting with version 3.7 or 3.8, not sure which one. So you'd need to grab an older version.And not supported here means: not working at all, not just not being officially supported. At least if you use the official binaries. There are ways to build it for Win 7 at this point, but this is non-trivial and requires significant patching.This in turn has caused many applications using Python one way or another, which otherwise would be still working plenty fine, to stop working on Win 7.

author: Bassman59
author_rank: Super Contributor
timestamp: June 20, 2022, 07:29:00 pm
content: Quote from: SiliconWizard on June 02, 2022, 05:34:45 pmOne such application is KiCad. There was much gnashing of teeth by a dozen people running ancient hardware over KiCad dropping Python 2 and moving to whatever they call the latest Python 3 and the resulting end of support for Windows 7.

author: SiliconWizard
author_rank: Super Contributor
timestamp: June 20, 2022, 07:40:28 pm
content: It's also the case for mercurial (that I personally use.)And many other applications, since Python has become sort of the de-facto scripting engine for applications, which uh... bites.