1

My company does not (yet) allow us to install or upgrade python3 neither install modules from pip on their servers. Even if I could, the machine is not connected to internet. But we can execute the python2 binary

Goal

Use the cx_Oracle module without using pip and internet

Workaround tentative

I got the idea to install cx_Oracle package on my computer and then copy the module files installed from my computer to the server.

So server dev folder looks like this (only listing interesting directories and files, omitting __pychache__, *.pyc and other useless *.py files):

|-- test_cx_oracle_in_local.py
\-- sqlalchemy/
    |-- connectors
    |-- databases
    |-- dialects
    |   |-- firebird
    |   |-- mysql
    |   |-- mssql
    |   |-- oracle
    |   |   |-- base.py
    |   |   |-- cx_oracle.py   <--- This is the interesting file
    |   |   |-- __init__.py
    |   |   \-- zxjdbc.py
    |   |-- postgresql
    |   |-- sqlite
    |   |-- sybase
    |-- engine
    |-- event
    |-- ext
    |   \-- declarative
    |-- orm
    |-- pool
    |-- sql
    |-- testing
    |   |-- plugin
    |   \-- suite
    \-- util

TEST 1

import cx_Oracle

if __name__ == "__main__":
    cx_Oracle.connect(user="xxxxxxxx", password="xxxxxxxx", dns="xxxxxxxx")

Output:

Traceback (most recent call last):
  File "test_cx_oracle_in_local.py", line xx, in <module>
    import cx_oracle
ImportError: No module named cx_Oracle

TEST 2

from sqlalchemy.dialects.oracle.cx_oracle import cx_Oracle

if __name__ == "__main__":
    cx_Oracle.connect(user="xxxxxxxx", password="xxxxxxxx", dns="xxxxxxxx")

Output:

Traceback (most recent call last):
  File "test_cx_oracle_in_local.py", line 36, in __init__
    self.connection = cx_oracle.connect(user="xxxxxxxx", password="xxxxxxxx", dns="xxxxxxxx")
AttributeError: 'module' object has no attribute 'connect'

TEST 3

Using STACKOVERFLOW - Import a module from a relative path

import os, sys, inspect
sys.path.insert(0, xxxxxxxx) # <--- see link above to see sorin's answer
import cx_oracle

if __name__ == "__main__":
    print("SYS_PATH = " + str(sys.path))
    cx_Oracle.connect(user="xxxxxxxx", password="xxxxxxxx", dns="xxxxxxxx")

Output:

SYS_PATH = ['/xxxxxxxx/sqlalchemy/dialects/oracle', '/xxxxxxxx/sqlalchemy/dialects', '/xxxxxxxx/sqlalchemy', 'xxxxxxxx', ...]
  File "test_cx_oracle_in_local.py", line xx, in <module>
    import cx_oracle
  File "/xxxxxxxx/sqlalchemy/dialects/oracle/cx_oracle.py", line 286, in <module>
    from . import base as oracle
ValueError: Attempted relative import in non-package

Please notice

  • The server OS is UNIX family (not a Windows/MAC)
  • I do not have edition neither execution rights to /bin, /usr, /opt, etc.
  • I can't use pip, pip is not even installed on the server
  • I can download files from my pro computer to the server
  • From server side, pinging or getting internet is impossible
  • I am currently running with python 2 but I am interested for python 3 solution if you have

Question

How can I fully use the module cx_Oracle without internet and without using pip neither having +wx access to system folder?

Christopher Jones
  • 9,449
  • 3
  • 24
  • 48
user12642493
  • 89
  • 1
  • 9
  • 1
    If your company has that strict of a policy regarding software installations, you should make sure you have the permission of the system admin and security admin to be doing what you're doing. This feels a lot like trying to circumvent an "Acceptable Use" user policy; people get fired for less... – pmdba Aug 05 '21 at 13:13
  • Oh it could xD But not. in fact it is more an issue for "upgrading" the server because there are currently other emergencies and things planned. Upgrading python is (almost) the last priority. In fact we have it on other servers, but not on this one :( Also this is why I am interested in Python3 answer because this machine should be upgraded "by soon" (meaning in 1 or 2 years....) – user12642493 Aug 05 '21 at 13:16
  • 1
    [install using github](https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html#install-using-github) clone the repo in ur system, and in server, then install by python setup.py as mention in link – sahasrara62 Aug 05 '21 at 13:22
  • Mac runs Unix. What Unix? Solaris, HP-UX, AIX? – Michael-O Aug 05 '21 at 13:27
  • @sahasrara62 : ```Traceback (most recent call last): File "setup.py", line 6, in import pkg_resources ImportError: No module named pkg_resources``` :( – user12642493 Aug 05 '21 at 13:44
  • You can download the package manually, then run package's setup.py and then this setup has also some option like 'install --user' where the final python module is then installed in your home directory. So basically you can manually duplicate all steps `pip` does. (Maybe it would be easier with some older version of cx_Oracle) – ibre5041 Aug 05 '21 at 13:49
  • After you solve the problem of `cx_Oracle` absence the next step will be to get Oracle client, that [is required for cx_Oracle](https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html#oracle-client-and-oracle-database-interoperability). Also you may read the documentation of this module, where they describe all [possible ways](https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html#install-using-source-from-pypi) to install the module. To break the security policy you may also build portable version of Python 3 and instal regular cx_Oracle module here. – astentx Aug 05 '21 at 15:20
  • You could also install that environment on a container if you are allowed to. – Mauricio Aug 11 '21 at 02:12

1 Answers1

4

The cx_oracle.py file in the sqlalchemy folder is not actually the cx_Oracle library - it's just a sqlalchemy wrapper for the actual cx_Oracle library, which is a compiled binary (including the compiled ODPI-C library, written in C).

Easiest way I can think of:

  1. Download cx_Oracle-7.3.0-cp27-cp27mu-manylinux1_x86_64.whl - this is the Wheel for the Python 2.7 version of cx_Oracle 7.3, the most recent cx_Oracle to support Python 2.
  2. Extract it (it's just a zip file) and put cx_Oracle.so somewhere on your server. This is the binary cx_Oracle library file.
  3. Load it as a relative library - if it's in the same directory as your code, import cx_Oracle should work.
kfinity
  • 8,581
  • 1
  • 13
  • 20