Python shell automatically load changes - python

I really love python because I love interactive development. There's one area where python appears to fall short, however, and that's in the area of automatically reloading changed files. Basically, what I want to have happen is to be able to modify a python file on-disk and then have my running python instance automatically reload the changed module to allow me to immediately access my changes in the REPL so I can test them out. Basically, I want some sort of watch command.
I happen to use the bpython shell because I think it's the best one available, but this feature is so important to me that I'd be willing to switch to any other python shell that does it right. Is it possible?

Something like tail -f in python + reload().
I really think they should make the tags OS and Python version mandatory though.

If you're trying to "test out" your code, perhaps you should be looking into doing automated unit tests instead of testing your code repeatedly and manually. It'll allow you to test more code quicker and waste less of your precious, precious development time.
Personally, I use unittest with py.test as the runner.

Related

How to force Pycharm to run all unit tests?

I have a single unit test file, and I can run tests within Pycharm. Fine. However, it seems that Pycharm skips tests based on some criteria regarding code change. Something like, if the code in a test method hasn't changed, and/or if the code that a particular test method is testing hasn't changed, it won't run the test. This has caused a lot of pain, and has let a bug fall through that wasn't caught until much later on. So my question is, how to stop Pycharm from automatically skipping tests and force it to run all tests?
Why is skipping tests the default behaviour anyway? This seems absolutely outrageous to me, but please correct me if I'm wrong.
EDIT
Oops my bad. I ran the tests by pressing the keyboard shortcut control-shift-R on my Mac OS, which normally runs a Python script, but it doesn't actually run the whole test file, and only runs a single test (where the caret is) instead. This (keyboard shortcut having different behaviours) is a little bit misleading in my opinion, but regardless, my description of the problem is wrong and I was wrong. Sorry!
Make sure all your test methods staring with a test_.
Usually I have 2 Tests configurations: All and Current.
tests folder is located in the root of PyCharm project
I use python path to specify certain test to run.
You can easily copy the reference to certain test

How can I communicate with a backgrounded Python instance?

Environment preamble:
I write Python code in Vim for Maya on Linux. I'm using nose for test discovery and execution, and I'm using its attribute plugin to decorate Maya-only test classes. I have mappings in Vim to run the non-Maya tests in regular Python, and the Maya tests through Maya's mayapy executable, which gives me access to the scene, and the maya.cmds module.
Actual problem:
mayapy takes 6 seconds to start up every time I hit my in-Maya tests Vim mapping. It gets really tedious. The non-Maya tests happen as fast as I can hit their mapping. It's helped me abstract more away from Maya, to avoid that tiny, all-day pain, but I'd still like it gone. 6 seconds starts to feel like a long time. There are tricks for working asynchronously in Vim, but I don't like to move ahead without seeing if my tests passed. I've asked Autodesk (makers of Maya), and they say there's no way around the startup time.
Question:
What I think I want is to fire up mayapy in the background, and then when I hit my Vim mapping, have it connect to the waiting Python instance and call the nose.run() command. I feel like mkfifo is the way to go, but I'm unsure of the particulars. How can I do this (through mkfifo or otherwise)?
I am assuming mayapy is like a repl and you can hit commands on to it like the mentioned nose.run(). If that is the case, I would recommend using the vim plugin tslime which allows you to be able to send stuff from a vim buffer to another tmux pane / window.

Python outputs only after script has finished in Komodo Edit

Forgive me if it's a silly question. I'm new to Python and scripting languages. Now I'm using Komodo Edit to code and run Python programs. Each time I run it, I have to wait until the program finished execution to see my "print" results in the middle. I'm wondering if it's possible to see realtime outputs as in a console. Maybe it is caused by some preference in Komodo?
Another question is that I know in the interpreter, when I store some variables it will remember what I stored, like in a Matlab workspace. But in Komodo Edit, every time the program runs from the beginning and store no temporary variables for debugging. For example if I need to read in some large file and do some operations, every time I have to read it in again which takes a lot of time.
Is there a way to achieve instant output or temporary variable storage without typing every line into the interpreter directly, when using other environments like Komodo?
The Python output is realtime.
If your output is not realtime, this is likely an artefact of Komodo Edit. Run your script outside of Komodo.
And Python, as any programming language, starts from scratch when you start it. How would it otherwise work?
If you want a interpreter-like situation you can use import pdb;pdb.set_trace() in your script. That will give you an interpreter prompt for debugging.

How can I save / copy classes & functions I've written in the python interpreter?

How can you save functions/ classes you've writing in a python interactive session to a file? Specifically, is there a way in pydev / eclipse's interactive session (on a mac) to do this?
I just started learning python - and am enjoying using the interpreter's interactive session for testing and playing with modules I've written. However, I find myself writing functions in the interpreter, which I think, oh it would be cool to save that to my script files. How do I do this?
I tried:
import pickle
pickle.dump(my_function, open("output.p", "w"))
But it seems to be more of a binary serialization, or at least nothing that I could copy and paste into my code...
Are there ways to see the code behind classes & functions I've defined in the interpreter? And then copy them out of the interpreter?
Update:
Ok, here's what I've learned so far:
I missed the easiest of all - PyDev's interactive session in eclipse allows you to right click and save your session. Still have to remove >>>'s, but gets the job done.
IPython is apparently the way to go to do this.
How to save a Python interactive session? has more details.
The best environment for interactive coding sessions has to be IPython, in my opinion. It's built on and extends the basic Python interpreter with a lot of magic, including history. For example, you can issue the command %logstart to dump all subsequent input to a file, which still needs to be edited afterward before it will be a script, but gives you a lot to work with.
When installing IPython, don't forget pyreadline.
In general, however, it is best to write code in an IDE and then run it. IPython helps here as well. If you write and save the script, then use the IPython "run" command to run it, the entire global namespace of the script will be available for inspection in your IPython session. Additionally, you can use the -d argument to run to trigger the pdb debugger immediately on any unhandled exception.
If you're more of a straightlaced IDE and debugger kind of guy, then the easiest and best lightweight environment has to be PyScripter.
I think the answer is to change your workflow.
What I do is write my functions in an editor (emacs), and then press a key combination (Ctrl-c Ctrl-e) to send the region of text to the (i)python interpreter.
That way I can save the function if I want, and also play with it in an interpreter.
Emacs is central to how I do it, but I'm sure there must be similar approaches with many editors (vim, gedit, etc) and IDEs.
PS. Finding a good editor is crucial when working with Python. The editor must be able to move blocks of code to the left and right easily, or the whitespace issue becomes too onerous.
I dislike typing blocks of code in the python interpreter because it doesn't allow me to shift blocks easily. You'll like Python even more when you find the right editor.
You can setup a python history file which stores everything you type into the interpreter.
Here's how:
http://docs.python.org/tutorial/interactive.html
I think it can't be done.
Python can perform instrospection with the inspect module, but the inspect.getsource function won't work without a source file.

How to develop a Python module/package without having to restart the interpreter after every change?

I am developing a Python package using a text editor and IPython. Each time I change any of the module code I have to restart the interpreter to test this. This is a pain since the classes I am developing rely on a context that needs to be re-established on each reload.
I am aware of the reload() function, but this appears to be frowned upon (also since it has been relegated from a built-in in Python 3.0) and moreover it rarely works since the modules almost always have multiple references.
My question is - what is the best/accepted way to develop a Python module/package so that I don't have to go through the pain of constantly re-establishing my interpreter context?
One idea I did think of was using the if __name__ == '__main__': trick to run a module directly so the code is not imported. However this leaves a bunch of contextual cruft (specific to my setup) at the bottom of my module files.
Ideas?
A different approach may be to formalise your test driven development, and instead of using the interpreter to test your module, save your tests and run them directly.
You probably know of the various ways to do this with python, I imagine the simplest way to start in this direction is to copy and paste what you do in the interpreter into the docstring as a doctest and add the following to the bottom of your module:
if __name__ == "__main__":
import doctest
doctest.testmod()
Your informal test will then be repeated every time the module is called directly. This has a number of other benefits. See the doctest docs for more info on writing doctests.
Ipython does allow reloads see the magic function %run iPython doc
or if modules under the one have changed the recursive dreloadd() function
If you have a complex context is it possible to create it in another module? or assign it to a global variable which will stay around as the interpreter is not restarted
How about using nose with nosey to run your tests automatically in a separate terminal every time you save your edits to disk? Set up all the state you need in your unit tests.
you could create a python script that sets up your context and run it with
python -i context-setup.py
-i When a script is passed as first argument or the -c option is
used, enter interactive mode after executing the script or the
command. It does not read the $PYTHONSTARTUP file. This can be
useful to inspect global variables or a stack trace when a
script raises an exception.

Categories

Resources