如何在ipython中访问shell变量

时间:2016-11-21 20:26:41

标签: shell environment-variables ipython ipython-magic

使用!魔法我可以访问env类型的环境变量,但不能访问终端中定义的环境变量,或.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]:

1 个答案:

答案 0 :(得分:2)

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

foo=value

仅在当前流程中显示(此处:bash

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

export foo

或设置&amp;出口:

export foo=value

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

顺便说一句,获得环境。 python中的变量,真正的python方式是:

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