Author Topic: Beginning Python Question  (Read 1429 times)

0 Members and 1 Guest are viewing this topic.

Offline bostonman

  • Super Contributor
  • ***
  • Posts: 1034
  • Country: us
Beginning Python Question
« on: March 17, 2022, 03:29:47 am »
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.
 

Offline retiredfeline

  • Frequent Contributor
  • **
  • Posts: 390
  • Country: au
Re: Beginning Python Question
« Reply #1 on: March 17, 2022, 04:02:06 am »
You can use VS to develop in Python, just install the appropriate addons.
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 11539
Re: Beginning Python Question
« Reply #2 on: March 17, 2022, 04:06:12 am »
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 idle or 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!  |O
« Last Edit: March 17, 2022, 07:48:36 am by Ian.M »
 

Offline retiredfeline

  • Frequent Contributor
  • **
  • Posts: 390
  • Country: au
Re: Beginning Python Question
« Reply #3 on: March 17, 2022, 04:15:13 am »
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.
 

Online newbrain

  • Super Contributor
  • ***
  • Posts: 1443
  • Country: se
Re: Beginning Python Question
« Reply #4 on: March 17, 2022, 06:51:44 am »
VS is open source
Nope, not in the least.
You are thinking of Visual Studio Code,  which is open source-ish (if you get the binaries, OpenSource if get the code or VS Codium).

That said both VS (the community edition is 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.
« Last Edit: March 17, 2022, 08:26:38 am by newbrain »
Nandemo wa shiranai wa yo, shitteru koto dake.
 

Offline retiredfeline

  • Frequent Contributor
  • **
  • Posts: 390
  • Country: au
Re: Beginning Python Question
« Reply #5 on: March 17, 2022, 06:55:49 am »
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.
« Last Edit: March 17, 2022, 07:18:04 am by retiredfeline »
 
The following users thanked this post: newbrain

Online ledtester

  • Super Contributor
  • ***
  • Posts: 2514
  • Country: us
Re: Beginning Python Question
« Reply #6 on: March 17, 2022, 09:39:26 am »
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.

Here's a short video on how to use IDLE which comes with python:

https://youtu.be/bOvqYw1SZJg

IDLE has a "Check module" menu item which will check your code for syntax errors.

Quote
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.

One 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-w

It has all the usual debugger amenities -- step one statement, skip over one statement, execute until function call returns, etc.
« Last Edit: March 17, 2022, 09:52:48 am by ledtester »
 

Offline tszaboo

  • Super Contributor
  • ***
  • Posts: 6208
  • Country: nl
  • Current job: ATEX product design
Re: Beginning Python Question
« Reply #7 on: March 17, 2022, 11:13:47 am »
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 notebook
To run the notebook:
jupyter notebook
 

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 4272
  • Country: ro
Re: Beginning Python Question
« Reply #8 on: March 17, 2022, 11:34:36 am »
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.

Offline rdl

  • Super Contributor
  • ***
  • Posts: 3348
  • Country: us
Re: Beginning Python Question
« Reply #9 on: March 17, 2022, 11:50:14 am »
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.
 

Offline TomS_

  • Frequent Contributor
  • **
  • Posts: 745
  • Country: gb
Re: Beginning Python Question
« Reply #10 on: March 30, 2022, 09:49:48 pm »
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: [Select]
def main():
    Put your code in here

if __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.
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9315
  • Country: us
Re: Beginning Python Question
« Reply #11 on: June 02, 2022, 04:43:31 pm »
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!

« Last Edit: June 02, 2022, 04:46:36 pm by rstofer »
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 10171
  • Country: fr
Re: Beginning Python Question
« Reply #12 on: June 02, 2022, 05:34:45 pm »
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.
 

Offline Bassman59

  • Super Contributor
  • ***
  • Posts: 2502
  • Country: us
  • Yes, I do this for a living
Re: Beginning Python Question
« Reply #13 on: June 20, 2022, 07:29:00 pm »
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.

One 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.

 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 10171
  • Country: fr
Re: Beginning Python Question
« Reply #14 on: June 20, 2022, 07:40:28 pm »
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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf