【问题标题】:Python import updated library from another directory when parent directory already have old version of that library当父目录已经有该库的旧版本时,Python 从另一个目录导入更新的库
【发布时间】:2017-02-17 18:57:53
【问题描述】:

我正在开发一个运行 Red Hat Enterprise Linus - 6+ 的强大集群。

我需要从另一个目录导入一个库(比如 Tensorflow),但主目录已经包含旧版本的 tensorflow。

结构 -

I Don't have Admin privileges for these libraries

Anaconda-directory 
 |--- tensorflow 0.7
 |--- other libraries which I need

Other Directory
 |--- tensorflow 0.11

My Home Directory (I have privileges here)
Local directory of anaconda
 |--- Keras

所以,我需要使用 Anaconda 目录,但我想使用 tensorflow (0.11) 的更新版本,我也想使用 Keras

我尝试了以下命令 -

METHOD - 1
# Load Anaconda module
module load apps/anaconda/4.1.1/gnu
# Has tensorflow 0.7 installed
jupyter notebook
--> Works, but with tensorflow 0.7 (Which is incompatible with keras)

METHOD - 2
# Load new version of tensorflow
# It automatically python module located at compiler/python/2.7.10/compilervars along with CUDAnn and others
module load apps/tensorflow/0.11/gnu

# Point to my local installation of anaconda where I have installed Keras
export PYTHONPATH=/home/-path-to-my-home-dir/anaconda2/lib/python2.7/site-packages/:$PYTHONPATH

echo $PYTHONPATH
/home/-path-to-my-home-dir/anaconda2/lib/python2.7/site-packages/:/home/apps/TENSOR_GPU/0.11/gnu/lib/python2.7/site-packages/

--> Works well in python shell, but I want to use IPython Notebook

jupyter notebook
--> Doesn't work in Ipython Notebook, which I guess is due to the reason that it uses jupyter notebook from my home dir (Not an Admin)


###  WHAT I WANT  ###
Anaconda Distribution --> From Server Module Directory (Not my local Directory)
Jypyter --> From above anaconda dist (Not my local anaconda dist)
Tensorflow --> From another module (Newer Version)
Keras --> Installed in my local anaconda distribution

# Any Other additional packages
# either not available on server (I can't install on server)
# or older versions available
# For such libraries, I should be able to use just that package 
# from my home directory (I can have anaconda installed on my home dir)

【问题讨论】:

    标签: python linux path server ipython


    【解决方案1】:

    我会试试这个。

    • 打开一个 Jupyter 笔记本。
    • import sys
    • sys.path[0:0] = ["/path/to/new/tensorflow"]
    • 导入 tensorflow,检查版本。
    • 从 Anaconda 导入库。

    第三步可以根据您的喜好进行扩展,包括与您的正常PYTHONPATH 不同的其他内容。

    【讨论】:

    • 刚试过sys.path[0:0] = ["/home/apps/TENSOR_GPU/0.11/gnu/lib/python2.7/site-packages/tensorflow"]但仍在使用旧的tensorflow版本tensorflow.__version__ '0.7.1'
    • >>> sys.path = ['/home/apps/TENSOR_GPU/0.11/gnu/lib/python2.7/site-packages/tensorflow', '', '/home/apps/TENSOR_GPU/lib/python27.zip', '/home/apps/TENSOR_GPU/lib/python2.7', '/home/apps/TENSOR_GPU/lib/python2.7/plat-linux2', '/home/apps/TENSOR_GPU/lib/python2.7/lib-tk', '/home/apps/TENSOR_GPU/lib/python2.7/lib-old', '/home/apps/TENSOR_GPU/lib/python2.7/lib-dynload', '/home/apps/TENSOR_GPU/lib/python2.7/site-packages', '/home/apps/TENSOR_GPU/lib/python2.7/site-packages/Sphinx-1.4.1-py2.7.egg', '/home/apps/TENSOR_GPU/lib/python2.7/site-packages/setuptools-23.0.0-py2.7.egg']
    • 我只能怀疑你的笔记本初始化已经导入了tensorflow。 sys.modules.keys() 是否在新打开的笔记本中包含 "tensorflow"?顺便说一句,试试reload("tensorflow");也许它会从新路径重新加载它。 (并不是说我喜欢这种方法。)
    猜你喜欢
    • 1970-01-01
    • 2020-03-21
    • 2021-09-16
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 2015-07-23
    相关资源
    最近更新 更多