简体   繁体   English

如何使脚本在python shell启动之后以及customuer输入之前执行?

[英]How to make a script execute just after python shell starts and before customuer's input?

In Pythonxy I can edit some python scripts in an pre-defined folder so that they will be executed (or import? I don't know). 在Pythonxy中,我可以在预定义的文件夹中编辑一些python脚本,以便执行(或导入?我不知道)。 For example, if I put a script in that folder: 例如,如果我将脚本放在该文件夹中:

import numpy as np
import scipy as sp

Then after I enter python shell, I can conveniently use np and sp as quick references. 然后,在进入python shell之后,我可以方便地使用np和sp作为快速参考。

Now the problem is, how can I use such technique without Pythonxy's support in standard python shell? 现在的问题是,如果没有标准python shell中的Pythonxy支持,如何使用这种技术? Can I write a script to handle pre import? 我可以编写脚本来处理预导入吗? I think it will be very useful. 我认为这将非常有用。

You can do this by setting up a python startup file, and set PYTHONSTARTUP variable to that file. 您可以通过设置python启动文件并将PYTHONSTARTUP变量设置为该文件来执行此操作。 In the startup file, just write 在启动文件中,只需编写

import numpy as np
import scipy as sp

Then, every time you enter python shell, it will automatically import the above modules. 然后,每次您输入python shell时,它将自动导入上述模块。

Here is an example. 这是一个例子。 Installing pythonstartup file 安装pythonstartup文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM