如何在ipython中访问shell变量 [英] How to access shell variable in ipython

查看:234
本文介绍了如何在ipython中访问shell变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用魔法我可以访问 env 输入环境变量,但不能在我的终端中定义,或者 .bashrc

Using the ! magic I can access env type environment variables, but not ones defined in my terminal, or .bashrc.

$:/<path>/balter/chip-seq-analysis/chipseq$ echo $hg38
/<path>/genomes/hg38/release-85/Homo_sapiens.GRCh38.dna.primary_assembly.fa
balter@exalab3:/<path>/balter/chip-seq-analysis/chipseq$ ipython
Python 2.7.12 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:42:40)
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: !echo $hg38


In [2]: !echo $SHELL
/usr/bin/bash

In [3]:


推荐答案

如果你的变量不是 export ed,那么没有子进程可以访问它们,python与否。

if your variables are not exported, no subprocess can access them, python or not.

foo=value

仅在当前流程中看到(此处: bash

is only seen by the current process (here: bash)

但如果你这样做(在你的 .bashrc 中) :

but if you do (in your .bashrc):

export foo

或设置& export:

or set & export:

export foo=value

然后子过程变量可见变量。这是你的修复。

then the variable becomes visible by subprocesses. That's your fix.

BTW,获得环境。 python中的变量,真正的python方式是:

BTW, to get an env. variable in python, the real python way is:

import os
print(os.getenv("foo"))

这篇关于如何在ipython中访问shell变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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