How do I print a greeting message when I initialize a python interpreter? For example, if I were to initialize a python interpreter with custom pre-defined variables, how might I advertise those variables to the user?
初始化python解释器时如何打印问候语?例如,如果我使用自定义预定义变量初始化python解释器,我该如何将这些变量通告给用户?
2 个解决方案
#1
There is an environment variable called PYTHONSTARTUP
that describes a path to a Python file to be executed on the invocation of Python shell. The script can contain normal Python code that is executed on invocation, therefore variables, prints or whatever else you want. It can be set in your ~/.bashrc
有一个名为PYTHONSTARTUP的环境变量,它描述了在Python shell调用时要执行的Python文件的路径。该脚本可以包含在调用时执行的普通Python代码,因此可以包含变量,打印或其他任何您想要的代码。它可以在你的〜/ .bashrc中设置
export PYTHONSTARTUP="$HOME/.pythonrc"
and then creating the file itself
然后创建文件本身
cat > ~/.pythonrc << EOF
print 'Hello World!'
EOF
The output when starting python then looks somewhat like this
启动python时的输出看起来有点像这样
Python 2.7.8 (default, Oct 19 2014, 16:02:00)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Hello World!
>>>
Because it is a normal Python file, setting the variables and showing them/announcing availability can be done like this:
因为它是一个普通的Python文件,所以设置变量并显示它们/宣布可用性可以这样做:
foo = 'Hello'
bar = 12.4123
print 'The following variables are available for use\nfoo: {}\nbar: {}'.format(foo, bar)
Output when invoking a Python repl and printing variable foo
:
调用Python repl和打印变量foo时的输出:
Python 2.7.8 (default, Oct 19 2014, 16:02:00)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
The following variables are available for use
foo: Hello
bar: 12.4123
>>> print foo
Hello
iPython acts different in a sense that it does not execute your PYTHONSTARTUP file, but has it's own mechanism called profiles. Default profile can be modified in ~/.ipython/profile_default/startup/
, where each *.py
and *.ipy
file is executed (see ~/.ipython/profile_default/startup/README
).
iPython在某种意义上表现不同,它不会执行您的PYTHONSTARTUP文件,而是拥有自己的称为配置文件的机制。可以在〜/ .ipython / profile_default / startup /中修改默认配置文件,其中执行每个* .py和* .ipy文件(请参阅〜/ .ipython / profile_default / startup / README)。
#2
You can embed a console from a script using the built-in console or IPython's console.
您可以使用内置控制台或IPython控制台从脚本嵌入控制台。
If you want to use Python's built-in console, pass the banner
argument. Assuming you have a dictionary of variables to inject:
如果要使用Python的内置控制台,请传递banner参数。假设你有一个要注入的变量字典:
from code import interact
vars = {'hello': 'world'}
message = 'Extra vars: {}'.format(', '.join(vars))
interact(banner=message, local={'hello': 'world'})
With IPython's console, pass banner1
.
使用IPython的控制台,传递banner1。
from IPython import embed
embed(banner1=message, user_ns={'hello': 'world'})
#1
There is an environment variable called PYTHONSTARTUP
that describes a path to a Python file to be executed on the invocation of Python shell. The script can contain normal Python code that is executed on invocation, therefore variables, prints or whatever else you want. It can be set in your ~/.bashrc
有一个名为PYTHONSTARTUP的环境变量,它描述了在Python shell调用时要执行的Python文件的路径。该脚本可以包含在调用时执行的普通Python代码,因此可以包含变量,打印或其他任何您想要的代码。它可以在你的〜/ .bashrc中设置
export PYTHONSTARTUP="$HOME/.pythonrc"
and then creating the file itself
然后创建文件本身
cat > ~/.pythonrc << EOF
print 'Hello World!'
EOF
The output when starting python then looks somewhat like this
启动python时的输出看起来有点像这样
Python 2.7.8 (default, Oct 19 2014, 16:02:00)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Hello World!
>>>
Because it is a normal Python file, setting the variables and showing them/announcing availability can be done like this:
因为它是一个普通的Python文件,所以设置变量并显示它们/宣布可用性可以这样做:
foo = 'Hello'
bar = 12.4123
print 'The following variables are available for use\nfoo: {}\nbar: {}'.format(foo, bar)
Output when invoking a Python repl and printing variable foo
:
调用Python repl和打印变量foo时的输出:
Python 2.7.8 (default, Oct 19 2014, 16:02:00)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
The following variables are available for use
foo: Hello
bar: 12.4123
>>> print foo
Hello
iPython acts different in a sense that it does not execute your PYTHONSTARTUP file, but has it's own mechanism called profiles. Default profile can be modified in ~/.ipython/profile_default/startup/
, where each *.py
and *.ipy
file is executed (see ~/.ipython/profile_default/startup/README
).
iPython在某种意义上表现不同,它不会执行您的PYTHONSTARTUP文件,而是拥有自己的称为配置文件的机制。可以在〜/ .ipython / profile_default / startup /中修改默认配置文件,其中执行每个* .py和* .ipy文件(请参阅〜/ .ipython / profile_default / startup / README)。
#2
You can embed a console from a script using the built-in console or IPython's console.
您可以使用内置控制台或IPython控制台从脚本嵌入控制台。
If you want to use Python's built-in console, pass the banner
argument. Assuming you have a dictionary of variables to inject:
如果要使用Python的内置控制台,请传递banner参数。假设你有一个要注入的变量字典:
from code import interact
vars = {'hello': 'world'}
message = 'Extra vars: {}'.format(', '.join(vars))
interact(banner=message, local={'hello': 'world'})
With IPython's console, pass banner1
.
使用IPython的控制台,传递banner1。
from IPython import embed
embed(banner1=message, user_ns={'hello': 'world'})