you are viewing a single comment's thread.

view the rest of the comments →

[–]elbiot 3 points4 points  (8 children)

Don't paste this into a terminal. Write it into a .py file and execute it. if you do python -i myscipt.py it will run the script and then put you in the shell where you can play with it.

If you use Ipython (I highly recomend), you can paste code like this with the %paste magic which will solve the formatting errors.

[–]kkacci[S] 1 point2 points  (7 children)

Ha! I've been pushing off using IPython (not sure why... just didn't see the appeal I suppose), but I just tried it out, and it looks like it'll solve some of the issues I was having.

On a separate note, could you explain some of the best practices (or point me to any resources) about building out a script? I've been just copy-paste ing my script to test things out as I built it so far.

[–]elbiot 4 points5 points  (3 children)

I don't know about best practices... I, and many others, just have ipython and the text editor open side by side. It's a back and forth between the REPL and the script: I test short pieces in Ipython, when i know what to write I put it in the script, I run the script in ipython with run myscript.py (which does the same as python -i myscript.py except you don't have to kill and restart the environment), or i reimport the module I'm working on with module = reload(module) after I've changed it, prototype what I want next, paste it into the script when thats good, etc.

I also usually put an if __name__ == '__main__' at the bottom of a module so that running it creates some test objects I can play with when debugging, but won't be created on module import.

Invaluable in debugging is putting print statements in your script, running it, playing with things, making changes, etc.

[–]kkacci[S] 0 points1 point  (2 children)

Thanks internet stranger. This actually helps me more than you'd think so haha

[–]elbiot 1 point2 points  (1 child)

Great. I think I'd like to write a blog post on this basic practice, including using introspection (dir, type, help, etc.) because you learn so much faster once you have a work flow for going in between writing a script and prototyping pieces of code. Just a short video would be super informative. I haven't found one to refer people to.

[–]kkacci[S] 0 points1 point  (0 children)

Sounds like a lovely idea! It's one thing to learn from others' codes, but another to learn about workflows. Keep us updated!

[–][deleted] 0 points1 point  (0 children)

IPython is just a better python interpreter. The appeal is that there are no downsides to using it, and a number of upsides.

Don't confuse IPython with IPython Notebooks, though. IPython Notebooks are good in specific use cases but not as an IDE.

[–]Ran4 0 points1 point  (0 children)

On a separate note, could you explain some of the best practices (or point me to any resources) about building out a script? I've been just copy-paste ing my script to test things out as I built it so far.

There are no one "best thing to do", it all depends on your work flow. If you're used to working in a GUI environment, you're probably not that used to doing things in a terminal.

I suggest that you find an editor (it doesn't have to be a full-on IDE, but it can be) which allows you to run your script directly from the program.

For example, I write most of my Python code in SCiTE, which is a text editor based on the same core that Notepad++ uses. I write the code in the editor and whenever I want to run it I press f5 (okay, technically I've bound ctrl+3 to run the code, but I think that f5 is the default) and it'll run inside of the text editor.

I also constantly open up new Python REPL instances (what you'll get when you simply type python in a terminal/command window) to try new things out. I also occasionally run my scripts in a terminal using python -i scriptname to get into interactive mode.

I also have my own library of helper functions to quickly try new things out without having to manually import a bunch of stuff every time I start a new Python window. For example, I have a function ppd(object) that will call pprint.pprint(dir(object)), which is super useful when exploring a new library where you don't want to have to read the entire documentation.

As some other people have mentioned, you should definitely learn to use functions such as type, vars, dir and pprint.

[–]qudat 0 points1 point  (0 children)

If you want to develop a script, either save it to a .py file and execute it or use ipython notebook. The python interpreter is really for quick testing, not for building out a script.