Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
进入python解释器时打印问候语消息_Python_Ipython_Interpreter_Python Interactive - Fatal编程技术网

进入python解释器时打印问候语消息

进入python解释器时打印问候语消息,python,ipython,interpreter,python-interactive,Python,Ipython,Interpreter,Python Interactive,初始化python解释器时如何打印问候语?例如,如果是,我如何向用户公布这些变量?有一个名为PYTHONSTARTUP的环境变量,它描述了调用pythonshell时要执行的Python文件的路径。脚本可以包含在调用时执行的普通Python代码,因此可以包含变量、打印或任何您想要的内容。它可以在~/.bashrc中设置 export PYTHONSTARTUP="$HOME/.pythonrc" 然后创建文件本身 cat > ~/.pythonrc << EOF print

初始化python解释器时如何打印问候语?例如,如果是,我如何向用户公布这些变量?

有一个名为
PYTHONSTARTUP
的环境变量,它描述了调用pythonshell时要执行的Python文件的路径。脚本可以包含在调用时执行的普通Python代码,因此可以包含变量、打印或任何您想要的内容。它可以在~/.bashrc中设置

export PYTHONSTARTUP="$HOME/.pythonrc"
然后创建文件本身

cat > ~/.pythonrc << EOF
print 'Hello World!'
EOF
因为它是一个普通的Python文件,所以可以这样设置变量并显示它们/宣布可用性:

foo = 'Hello'
bar = 12.4123

print 'The following variables are available for use\nfoo: {}\nbar: {}'.format(foo, bar)
调用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的作用不同,它不执行PYTHONSTARTUP文件,但有自己的机制,称为profiles。可以在
~/.ipython/profile\u Default/startup/
中修改默认配置文件,其中执行每个
*.py
*.ipy
文件(请参见
~/.ipython/profile\u Default/startup/README

可以使用内置控制台或ipython的控制台从脚本嵌入控制台

如果要使用Python的内置函数,请传递
banner
参数。假设您有一个要注入的变量字典:

from code import interact
vars = {'hello': 'world'}
message = 'Extra vars: {}'.format(', '.join(vars))
interact(banner=message, local={'hello': 'world'})
使用IPython,传递
banner1

from IPython import embed
embed(banner1=message, user_ns={'hello': 'world'})

您能举个例子吗?使用PYTHONSTARTUP环境变量指定一个文件。python-h给出了细节
from IPython import embed
embed(banner1=message, user_ns={'hello': 'world'})