How to run script from interpreter?
Yes, it's been covered, but not quite to my satisfaction. Here's an example simple script: # Very simple script bar = 123 I save this as "foo.py" somewhere Python can find it >>> import foo >>> bar Traceback (most recent call last): File "", line 1, in NameError: name 'bar' is not defined # Oops, it's in a different namespace and I have to prefix EVERYTHING with "foo.". This is inconvenient. >>> foo.bar 123 Start a new session... >>> from foo import * >>> bar 123 Do a little editing: # Very simple (new) script bar = 456 Save as foo.py, back to the interpreter: >>> reload(foo) Traceback (most recent call last): File "", line 1, in NameError: name 'foo' is not defined # Oops, it's not in the namespace, so there is NO way to use reload Start a new session... >>> execfile('foo.py') Traceback (most recent call last): File "", line 1, in IOError: [Errno 2] No such file or directory: 'foo.py' # Oops, it's on the path, but execfile can't find it >>> import os,sys >>> os.chdir('W:/Code') >>> execfile('foo.py') >>> bar 456 Do some more editing: # Very simple (even newer) script bar = 789 Save it as foo.py, back to the interpreter: >>> execfile('foo.py') >>> bar 789 That works, but nothing is very convenient for debugging simple scripts. If I run the script from a command prompt it works, but I lose all my other stuff (debugging functions, variables, etc.). More a comment than a question but seems like sometimes execfile() is the right tool. Regards, Allen -- https://mail.python.org/mailman/listinfo/python-list
Re: How to run script from interpreter?
On Saturday, February 17, 2018 at 8:50:48 AM UTC-6, Steven D'Aprano wrote: > > For me, the tool I use is a set of re-usable tools: > > - a text editor; > - a system command prompt in a terminal/console window; > - a Python REPL for running code snippets and looking up help(obj). > > Other people prefer a IDE like Spyder (or many others). Either way, > they're designed for the edit-run-debug-edit cycle. I hadn't looked at Spyder, it seems very useful. > I edit the source code of my script in the text editor, then run it from > the command prompt using > > python myscript.py > > If I need the debugger, I use: > > python -i myscript.py > ... > then edit the script to make the tests work. > > The best part of this is each time you run the script, it is guaranteed > to be running *fresh*, with no left-over cruft from previous runs. Good point. But some of that is stuff I'm using, so maybe it has to go in the script. And this from Ian seems to work for me: > py> import foo # Get the module to reload > py> reload(foo) # Reload it > py> from foo import * # Update all the names previously *-imported Thanks all for some good advice. Regards, Allen -- https://mail.python.org/mailman/listinfo/python-list
Re: Python file location
Thanks to all for suggestions. Linux has "too much freedom" :-) Regards, Allen -- https://mail.python.org/mailman/listinfo/python-list
Python file location
I have an older laptop I use for programming, particularly Python and Octave, running a variety of Debian Linux, and I am curious if there is a "standard" place in the file system to store this type of program file. OK, I know they should go in a repository and be managed by an IDE but this seems like way overkill for the kind of programming that I do, normally a single file. Any suggestions welcome, thanks. Regards, Allen -- https://mail.python.org/mailman/listinfo/python-list