如何在IPython启动时将目录添加到sys.path?

7

环境:

  • Windows 7 英文版 32位
  • Python 2.7.3
  • iPython 0.13.1

我尝试进行配置:

ipython -i -c "import sys; sys.path.append('path_name')"

但是它似乎不起作用。
那么正确的解决方案是什么呢?

或者如何在ipython启动时将当前目录添加到sys.path中?
谢谢。


抱歉我的粗心大意,它可以工作。问题来自于Eclipse插件EasyShell。我仍然无法处理Easyshell。 - Honghe.Wu
4个回答

10

只是对Honghe.Wu的回答做一个简短的跟进。

可能还需要补充一点:

c.InteractiveShellApp.exec_lines = [
'import sys; sys.path.append("/absolute/path/")']

将任意目录添加到ipython_config.py中。

另外,如果您是ipython的新手(就像我一样),您需要首先创建标准配置文件,以便实际存在ipython_config.py。因此,您需要执行以下步骤:

ipython profile create

你可以通过以下方式找到配置目录:

ipython locate

最好的S


6

在@cartman的帮助下,我目前使用以下配置在文件 ipython_config.py 中将当前目录添加到sys.path中:

c.InteractiveShellApp.exec_lines = [
'import sys,os; sys.path.append(os.getcwd())'
]

直到有更好的解决方案。

如果找不到配置文件,只需运行 ipython profile create - diabloneo

1

但我希望path_name是动态的。 - Honghe.Wu

0
c = get_config()
c.InteractiveShellApp.exec_lines = ['import sys,os; sys.path.append os.get_exec_path())']

将此代码放入 ipython_config.py 文件中。
感谢 stevosn 的帮助。

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