如何在IPython中自动设置默认路径

3
我使用WingIDE进行开发和IPython运行我的脚本。我在以下几个方面遇到了一些不方便:
1. 每当我更新代码时,我都必须重新加载模块以便在IPython中更新它。为了解决这个问题,我按照Jomonsugi在以下链接中提供的答案操作:如何在IPython中自动重新加载模块?,这很有效。
2. 每次启动IPython时,我的路径都设置为我的exe文件所在位置,而我必须cd到我的脚本目录。我试图通过在ipython_config.py中设置参数c.InteractiveShell.ipython_dir = <mypath>来在启动IPython时自动更改目录,但这并没有起作用。
请问是否可以设置默认路径?在ipython_config.py中有特定的参数需要设置吗?
2个回答

3

一种方法是使用您的startup.py文件。它应该位于某个位置,例如:

C:/Users/yourname/.ipython/profile_default

如果还没有,请创建它。 然后使用。
import os
os.chdir('C:/Users/mypath')

这两行代码将在IPython启动时运行。

来源: IPython.org > 启动文件


3

我相信Brad Solomon的回答适用于他的IPython版本,但是我刚刚通过 pip install ipython 下载了IPython,并且我的启动文件在一个比他深一层的目录中。我的IPython版本是7.18.1,启动文件位于~/.ipython/profile_default/startup/。那里有一个README文件,其中说明

This is the IPython startup directory

.py and .ipy files in this directory will be run *prior* to any code
or files specified via the exec_lines or exec_files configurables
whenever you load this profile.

Files will be run in lexicographical order, so you can control the
execution order of files with a prefix, e.g.::

        00-first.py
        50-middle.py
        99-last.ipy

这相当自我描述,但我想要补充一点(对于从这里来的任何人),%load_ext autoreload是一个IPython命令,因此您可能需要创建一个名为startup.ipy的文件,并且其内容类似于:

%load_ext autoreload
%autoreload 2
print('IPython startup file - created 2020/9/28')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接