启动 ipython 运行脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3323230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Start ipython running a script
提问by johannix
My use case is I want to initialize some functions in a file and then start up ipython with those functions defined. Is there any way to do something like
我的用例是我想初始化文件中的一些函数,然后使用定义的这些函数启动 ipython。有没有办法做类似的事情
ipython --run_script=myscript.py?
采纳答案by Alex Martelli
Per the docs, it's trivial:
根据文档,这很简单:
You start IPython with the command:
您可以使用以下命令启动 IPython:
$ ipython [options] files
If invoked with no options, it executes all the files listed in sequence and drops you into the interpreter while still acknowledging any options you may have set in your
ipythonrcfile. This behavior is different from standard Python, which when called aspython -iwill only execute one file and ignore your configuration setup.
如果不带任何选项调用,它会执行按顺序列出的所有文件,并将您放入解释器,同时仍然确认您可能在
ipythonrc文件中设置的任何选项 。此行为与标准 Python 不同,后者在调用 aspython -i时只会执行一个文件并忽略您的配置设置。
So, just use ipython myfile.py... and there you are!-)
所以,只需使用ipython myfile.py...就可以了!-)
回答by Maarten
In recent versions of ipython you do need to add the -ioption to get into the interactive environment afterwards. Without the -iit just runs the code in myfile.py and returns to the prompt.
在最新版本的 ipython 中,您确实需要添加-i选项以随后进入交互式环境。如果没有-i它,它只会运行 myfile.py 中的代码并返回到提示。
$ ipython -i myfile.py
回答by partofthething
Nowadays, you can use the startup folder of ipython, which is located in your home directory (C:\users\[username]\.ipython on Windows). Go into the default profile and you'll see a startup folder with a README file. Just put any Python scripts in there, or if you want ipython commands, put them in a file with an .ipy extension.
现在,您可以使用 ipython 的启动文件夹,该文件夹位于您的主目录中(Windows 上为 C:\users\[用户名]\.ipython)。进入默认配置文件,您将看到一个包含 README 文件的启动文件夹。只需将任何 Python 脚本放入其中,或者如果您需要 ipython 命令,请将它们放入一个扩展名为 .ipy 的文件中。
回答by mpjan
You seem to be looking for ipyhton's %run magic command.
您似乎在寻找 ipyhton 的 %run 魔法命令。
By typing in ipython:
通过输入 ipython:
%run hello_world.py
you'll run the hello.py program saved in your home directory. The functions and variables defined in that script will be accessible to you too.
您将运行保存在主目录中的 hello.py 程序。您也可以访问该脚本中定义的函数和变量。
回答by Caleb
You can use ipython profiles to define startup scripts that will run every time you start ipython. A full description of profiles, is given here. You can create multiple profiles with different startup files.
您可以使用 ipython 配置文件来定义每次启动 ipython 时都会运行的启动脚本。此处给出了配置文件的完整说明。您可以使用不同的启动文件创建多个配置文件。
Assuming you only need one profile, and always want the same startup files every time you start ipython, you can simply modify the default profile. To do this, first find out where your ipython configuration directory is in an ipython session.:
假设您只需要一个配置文件,并且每次启动 ipython 时总是想要相同的启动文件,您可以简单地修改默认配置文件。为此,首先要找出您的 ipython 配置目录在 ipython 会话中的位置。:
In [1]: import IPython
In [2]: IPython.paths.get_ipython_dir() # As of IPython v4.0
In [3]: print(ipython_config_dir)
/home/johndoe/.config/ipython
For this example, I am using Ubuntu Linux, and the config directory is in /home/johndoe/.config/ipython, where johndoeis the username.
在这个例子中,我使用的是 Ubuntu Linux,并且 config 目录在 中/home/johndoe/.config/ipython,johndoe用户名在哪里。
The default_profile is in the profile_defaultsubdirectory. Put any starting scripts in profile_default/startup. In the example here, the full path would be /home/johndoe/.config/ipython/profile_default/startup.
default_profile 位于profile_default子目录中。将任何启动脚本放在profile_default/startup. 在此处的示例中,完整路径为/home/johndoe/.config/ipython/profile_default/startup.
回答by Thej Kiran
Update to @Caleb's answer for Python 3.5 in Ubuntu 14.04: Made this answer self contained by copying relevant parts of @Caleb's answer.
更新@Caleb's answer for Python 3.5 in Ubuntu 14.04:通过复制@Caleb's answer的相关部分使这个答案自包含。
You can use ipython profiles to define startup scripts that will run every time you start ipython. A full description of profiles, is given here. You can create multiple profiles with different startup files.
您可以使用 ipython 配置文件来定义每次启动 ipython 时都会运行的启动脚本。此处给出了配置文件的完整说明。您可以使用不同的启动文件创建多个配置文件。
Assuming you only need one profile, and always want the same startup files every time you start ipython, you can simply modify the default profile. To do this, first find out where your ipython configuration directory is in an ipython session.:
假设您只需要一个配置文件,并且每次启动 ipython 时总是想要相同的启动文件,您可以简单地修改默认配置文件。为此,首先要找出您的 ipython 配置目录在 ipython 会话中的位置。:
Input:
输入:
import IPython
ipython_config_dir = IPython.paths.get_ipython_dir()
print(ipython_cofig_dir)
Output:
输出:
/home/johndoe/.ipython
For this example johndoeis the username.
对于此示例johndoe是用户名。
Inside the /.ipythonfolder, the default_profile is in the profile_defaultsubdirectory. Put any starting scripts in profile_default/startup. In the example here, the full path would be
在/.ipython文件夹内,default_profile 位于profile_default子目录中。将任何启动脚本放在 profile_default/startup 中。在此处的示例中,完整路径为
/home/johndoe/.ipython/profile_default/startup
回答by Mark
The following is for the case when you want your startup scripts to automatically be run whenever you use ipython (instead of having a script that you must specify each time you run ipython).
以下是您希望每次使用 ipython 时自动运行启动脚本的情况(而不是每次运行 ipython 时都必须指定一个脚本)。
For recent versions (i.e. 5.1.0) of ipython, place one or more python scripts you wish to have executed in the IPYTHON_CONFIG_DIR/profile_PROFILENAME/startupfolder.
对于 ipython 的最新版本(即 5.1.0),请将您希望执行的一个或多个 Python 脚本放在该IPYTHON_CONFIG_DIR/profile_PROFILENAME/startup文件夹中。
On linux, for example, you could put your python startup code into a file named ~/.ipython/profile_default/startup/10-mystartupstuff.pyif you want it to run when no ipython profile is specified.
例如,在 linux 上,您可以将您的 python 启动代码放入一个名为的文件中,~/.ipython/profile_default/startup/10-mystartupstuff.py如果您希望它在未指定 ipython 配置文件时运行。
Information on creating and using ipython profiles is available here.

