微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JupyterHub Oracle InstantClient 和 cx_Oracle 安装

如何解决JupyterHub Oracle InstantClient 和 cx_Oracle 安装

我使用 Docker 容器安装了 JupyterHub。

  1. 我创建了一个新的 Anaconda 环境“cx_oracle_env”,并在终端中安装了包 cx_Oracle:

    # Creates a new Anaconda Environment called "cx_oracle_env" using Python 3.7 in silent mode
    conda create -n cx_oracle_env python=3.7 -y
    # >>>> Returns no warnings / errors
    
    # Activates the Anaconda Environment "cx_oracle_env"
    conda activate cx_oracle_env
    # >>>> Returns no warnings / errors
    
    # Mamba is a reimplementation of the conda package manager in C++.
    # - parallel downloading of repository data and package files using multi-threading
    # - libsolv for much faster dependency solving,a state of the art library used in the 
    #   RPM package     manager of Red Hat,Fedora and OpenSUSE
    # - core parts of mamba are implemented in C++ for maximum efficiency
    # At the same time,mamba utilize the same command line parser,package installation and 
    # deinstallation code and transaction verification routines as conda to stay as compatible as 
    # possible.
    #
    conda install mamba -n base -c conda-forge -y
    # >>>> Returns no warnings / errors
    
    # Installs ipykernel in currently active Anaconda Environment
    mamba install ipykernel -y
    # >>>> Returns no warnings / errors
    
    # Installs cx_Oracle
    python -m pip install cx_Oracle --upgrade
    # >>>> Returns no warnings / errors
    
    # Binds ipykernel "cx_oracle_env" to Anaconda Environment "cx_oracle_env"
    python -m ipykernel install --user --name cx_oracle_env --display-name="cx_oracle_env"
    # >>>> Returns no warnings / errors
    
  2. 我将 ORACLE InstantClient instantclient-basic-linux.x64-21.1.0.0.0.ziphttps://www.oracle.com/uk/database/technologies/instant-client/linux-x86-64-downloads.html 下载到我的本地计算机,并将 zip 文件上传到我的 JupyterHub 工作目录。

  3. 我通过在 Launcher 面板的 Notebook 部分中选择“cx_oracle_env”打开了一个新的 Jupyter Notebook。

  4. 在 Jupyter Notebook 中,我使用以下命令解压了文件 Instantclient-basic-linux.x64-21.1.0.0.0.zip:

    from shutil import unpack_archive
    # Decompress ZIP-file to working directory (/home/jovyan/instantclient_21_1/)
    unpack_archive('instantclient-basic-linux.x64-21.1.0.0.0.zip','')
    >>>> Returns no warnings / errors
    
  5. 检查路径是否存在:

    import os.path
    from os import path
    path.exists("/home/jovyan/instantclient_21_1")
    # >>>> Returns True
    
  6. 设置 LD_LIBRARY_PATH:

    import os
    os.environ["LD_LIBRARY_PATH"] = "/home/jovyan/instantclient_21_1"
    !echo $LD_LIBRARY_PATH
    # >>>> Returns /home/jovyan/instantclient_21_1
    
  7. 设置 ORACLE_HOME:

    os.environ["ORACLE_HOME"] = "/home/jovyan/instantclient_21_1"
    !echo $ORACLE_HOME
    # >>>> Returns /home/jovyan/instantclient_21_1
    
  8. 安装 libaio:

    !mamba install libaio -y
    # >>>> Returns no warnings / errors
    
  9. 导入 cx_Oracle:

    import cx_Oracle
    # >>>> Returns no warnings / errors
    
  10. 调用 init_oracle_client 后,出现以下错误

    cx_Oracle.init_oracle_client(lib_dir=r"/home/jovyan/instantclient_21_1")
    

enter image description here

我错过了什么?

PS:不幸的是,我在 JupyterHub 终端中没有 sudo 权限......

enter image description here

解决方法

不幸的是,环境变量 LD_LIBRARY_PATH 必须 在启动进程之前设置,因此在您的 Python 脚本中更改它不起作用。

此外,由于当前即时客户端构建方式的限制,cx_Oracle.init_oracle_client() 不能在 Linux 上使用,除非事先在进程之外设置 LD_LIBRARY_PATH。希望这个限制很快就会被删除,但与此同时,这就是你必须做的。您还可以使用 ld.so.conf 配置使整个计算机的安装工作,在这种情况下,不再需要设置 LD_LIBRARY_PATH

,

我最终通过自定义 https://github.com/jupyter/docker-stacks/blob/master/minimal-notebook/Dockerfile 中的 Dockerfile 创建一个新的 JupyterHub 环境解决了这个问题,并将其作为新的“最小 Oracle 环境”嵌入到 JupyterHub 中。

自定义Dockerfile有如下内容(我只添加了“cx_Oracle安装开始/结束”部分):

# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
ARG BASE_CONTAINER=jupyter/base-notebook
FROM $BASE_CONTAINER

LABEL maintainer="Jupyter Project <jupyter@googlegroups.com>"

USER root

# Install all OS dependencies for fully functional notebook server
RUN apt-get update && apt-get install -yq --no-install-recommends \
    build-essential \
    vim-tiny \
    git \
    inkscape \
    libsm6 \
    libxext-dev \
    libxrender1 \
    lmodern \
    netcat \
    # ---- nbconvert dependencies ----
    texlive-xetex \
    texlive-fonts-recommended \
    texlive-plain-generic \
    # ----
    tzdata \
    unzip \
    nano-tiny \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# cx_Oracle installation begin
WORKDIR /opt/oracle
RUN apt-get update && apt-get install -y libaio1 wget
RUN wget https://download.oracle.com/otn_software/linux/instantclient/instantclient-basiclite-linuxx64.zip && \
    unzip instantclient-basiclite-linuxx64.zip && rm -f instantclient-basiclite-linuxx64.zip && \
    cd /opt/oracle/instantclient* && rm -f *jdbc* *occi* *mysql* *README *jar uidrvci genezi adrci && \
    echo /opt/oracle/instantclient* > /etc/ld.so.conf.d/oracle-instantclient.conf && ldconfig
RUN python -m pip install cx_Oracle
WORKDIR $HOME
# cx_Oracle installation end

# Create alternative for nano -> nano-tiny
RUN update-alternatives --install /usr/bin/nano nano /bin/nano-tiny 10

# Switch back to jovyan to avoid accidental container runs as root
USER $NB_UID

在本地构建自定义的Dockerfile,并将其作为“最小Oracle环境”嵌入到Kubernetes集群中后,我在新创建的JupyterHub环境中启动了一个新的Jupyter Notebook,并测试了ORACLE连接如下:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?