如何在IPython 3.2.0上使用〜/ .bashrc别名? [英] How to use ~/.bashrc aliases on IPython 3.2.0?

查看:235
本文介绍了如何在IPython 3.2.0上使用〜/ .bashrc别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在IPython上使用来自〜/ .bashrc 的别名。

I need to use my aliases from ~/.bashrc on IPython.

首先我试过但它不起作用

First I've tried but it didn't work

%%bash 
source ~/.bashrc

根据这篇文章我们应该这样做

%%bash 
. ~/.bashrc
f2py3 -v

在Jupiter和I上运行需要20秒获取:

It takes 20 sec to run on Jupiter and I get:


bash:第2行:f2py3:未找到命令

bash: line 2: f2py3: command not found

我的〜/ .bashrc 文件看起来像

alias f2py3='$HOME/python/bin/f2py'




bash:第2行:输入:f2py3:not found

bash: line 2: type: f2py3: not found

别名,来源和%rehashx%都不工作

Neither alias, source, nor %rehashx% work

%%bash
alias f2py3='$HOME/python/bin/f2py'

我实际上发现问题是Python,无法用sh执行 alias 命令也不是bash。

I actually found that the problem is Python, who can't execute alias command neither with sh nor bash.

我可以在IPython魔法中使用别名吗?

Can I use alias with IPython magics?

推荐答案

你可以在ipython配置中解析你的bashrc文件并添加你定义的任何自定义别名:

You can parse your bashrc file in the ipython config and add any custom aliases you have defined:

import re
import os.path

c = get_config()

with open(os.path.expanduser('~/.bashrc')) as bashrc:
    aliases = []
    for line in bashrc:
        if line.startswith('alias'):
            parts = re.match(r"""^alias (\w+)=(['"]?)(.+)\2$""", line.strip())
            if parts:
                source, _, target = parts.groups()
                aliases.append((source, target))

    c.AliasManager.user_aliases = aliases

这应放在〜/ .ipython / profile_default / ipython_config.py

% rehashx 使别名表中的系统命令可用,因此如果你想使用ipytho也非常有用n作为shell。

%rehashx makes system commands available in the alias table so is also very useful if you want to use ipython as a shell.

这篇关于如何在IPython 3.2.0上使用〜/ .bashrc别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆