将Tensorflow从usr / bin / python链接到conda环境python

时间:2017-01-07 17:33:14

标签: python-2.7 tensorflow conda

我的主要usr / bin / python上安装了Tensorflow和GPU,例如在AWS EC2计算机上,我想在conda环境中使用它。如果没有为每个环境重新安装张量流,这是否可行?

编辑:我发现了virtual-env --system-site-packages选项。也许相当于Conda?特别是对于像Tensorflow-gpu这样需要为特定硬件构建的软件包,并且不能直接在pip上使用。

1 个答案:

答案 0 :(得分:1)

如果系统中安装了tensorflow,那么它可能会出现在以下四个目录之一中:

/usr/local/lib/python2.7/dist-packages
/usr/local/lib/python2.7/site-packages
/usr/lib/python2.7/dist-packages
/usr/lib/python2.7/site-packages

要使系统包在conda中可用,您可以执行以下操作:

(root) ~/condaexpts $ cat <<EOF > $CONDA_PREFIX/lib/python2.7/site-packages/systempkgs.pth
/usr/local/lib/python2.7/dist-packages
/usr/local/lib/python2.7/site-packages
/usr/lib/python2.7/dist-packages
/usr/lib/python2.7/site-packages
EOF

然后验证是否已添加这些路径:

(root) ~/condaexpts $ python
Python 2.7.12 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:42:40) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import sys
>>> sys.path
['', '/home/ubuntu/condaexpts/m2/lib/python27.zip', '/home/ubuntu/condaexpts/m2/lib/python2.7', '/home/ubuntu/condaexpts/m2/lib/python2.7/plat-linux2', '/home/ubuntu/condaexpts/m2/lib/python2.7/lib-tk', '/home/ubuntu/condaexpts/m2/lib/python2.7/lib-old', '/home/ubuntu/condaexpts/m2/lib/python2.7/lib-dynload', '/home/ubuntu/condaexpts/m2/lib/python2.7/site-packages', '/home/ubuntu/condaexpts/m2/lib/python2.7/site-packages/setuptools-27.2.0-py2.7.egg', '/usr/local/lib/python2.7/dist-packages', '/usr/local/lib/python2.7/site-packages', '/usr/lib/python2.7/dist-packages']

现在您可以导入任何系统包。

如果您不希望此更改对于您的conda环境而言是永久性的,您始终可以将包含系统包的目录添加到PYTHONPATH环境变量中:

(root) ~/condaexpts $ PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/dist-packages/ python
Python 2.7.12 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:42:40) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import sys
>>> sys.path
['', '/home/ubuntu/condaexpts', '/usr/local/lib/python2.7/dist-packages', '/home/ubuntu/condaexpts/m2/lib/python27.zip', '/home/ubuntu/condaexpts/m2/lib/python2.7', '/home/ubuntu/condaexpts/m2/lib/python2.7/plat-linux2', '/home/ubuntu/condaexpts/m2/lib/python2.7/lib-tk', '/home/ubuntu/condaexpts/m2/lib/python2.7/lib-old', '/home/ubuntu/condaexpts/m2/lib/python2.7/lib-dynload', '/home/ubuntu/condaexpts/m2/lib/python2.7/site-packages', '/home/ubuntu/condaexpts/m2/lib/python2.7/site-packages/setuptools-27.2.0-py2.7.egg']