是否可以启动带有预先导入的某些模块的 Paster shell?
是否可以运行“paster shell blah.ini”(或其变体)并让它自动加载某些库?
我讨厌总是在每个 Paster shell 中输入“from foo.bar import mystuff”作为第一个命令,并且希望计算机为我做这件事。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以尝试的一个选项是创建一个 sitecustomize.py 脚本。 如果它与 Paster shell 位于同一文件夹中,则 python 解释器应该在启动时加载它。
让我澄清一下,sitecustomize.py(如果找到)总是在解释器启动时加载。 因此,如果您将其放在可以找到的地方,最好是仅在 Paster shell 启动时才能找到的地方,那么您应该能够将导入添加到其中并让它们准备好。
这可能是您最好的选择。 如果粘贴 shell 是一个打包的应用程序(la py2exe),它应该仍然可以工作。
另请参阅:
http://www.rexx。 com/~dkuhlman/pylons_quick_site.html#using-an-ipython-embedded-shell
http://pylonshq.com/project/pylonshq/ticket/428
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
如果将环境变量 PYTHONSTARTUP 设置为文件名,它将在打开交互式提示符时执行该文件。
我对 Paster shell 一无所知,但我认为它的工作原理类似。
或者,您可以研究 iPython,它具有更强大的功能(特别是与 readline 库一起安装时)。 例如 %run 允许您在当前命名空间中运行脚本,或者您可以使用历史记录完成。
编辑:
好的。 经过更多研究后,我相当确定 Paster shell 只是执行一组有用的导入,并且可以使用简短的脚本和 ipython 轻松复制,然后
%run myscript.py
编辑:
查看源代码后,这将很难做到(我对默认导入的看法是错误的。它也会解析你的配置文件),但是如果你同时安装了 Pylons 和 iPython,那么 Paster shell 应该自动使用 iPython。 仔细检查两者是否安装正确,并仔细检查 Paster shell 是否尚未使用 iPython(它可能看起来像正常的 python 提示符)。
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).