views:

382

answers:

2

Is it possible to run "paster shell blah.ini" (or a variant thereof) and have it automatically load certain libraries?

I hate having to always type "from foo.bar import mystuff" as the first command in every paster shell, and would like the computer to do it for me.

A: 

If you set the environment variable PYTHONSTARTUP to the name of a file, it will execute that file on opening the interactive prompt.

I don't know anything about paster shell, but I assume it works similarly.

Alternatively you could look into iPython, which has much more powerful features (particularly when installed with the readline library). For example %run allows you to run a script in the current namespace, or you can use history completion.

Edit:

Okay. Having looked into it a bit more, I'm fairly certain that paster shell just does a set of useful imports, and could be easily replicated with a short script and ipython and then %run myscript.py

Edit:

Having looked at the source, it would be very hard to do (I was wrong about the default imports. It parses your config file as well), however if you have Pylons and iPython both installed, then paster shell should use iPython automagically. Double check that both are installed properly, and double check that paster shell isn't using iPython already (it might be looking like normal python prompt).

Singletoned
Sorry, PYTHONSTARTUP doesn't work with paster. As far as I'm aware, iPython (which I use everywhere else) doesn't either.
mike
What does paster shell actually do that you want to use it instead of ipython? I was kind of assuming that you could just use ipython instead and have a script to set up the handy environment that I assume paster shell creates for you.
Singletoned
http://wiki.pylonshq.com/pages/viewpage.action?pageId=9011323indicates that iPython does, in fact work with the paster shell.
Christopher
I'm having trouble replicating "paster shell" with a short script, but if you write one I'll give you the bounty.
mike
+2  A: 

An option to try would be to create a sitecustomize.py script. If you have this in the same folder as your paster shell, the python interpreter should load it up on startup.

Let me clarify, sitecustomize.py, if found, is always loaded on startup of the interpreter. So if you put it where it can be found, ideally somewhere that is only found when the paster shell starts, then you should be able to add your imports to it and have them be ready.

This is probably your best bet. If the paster shell is a packaged app (a la py2exe) it should still work.

See also:

http://www.rexx.com/~dkuhlman/pylons_quick_site.html#using-an-ipython-embedded-shell http://pylonshq.com/project/pylonshq/ticket/428

Christopher