Import module from one python install into another

I am under Ubuntu 16.04 LTS.

I have two python installations. I am actually using them via pvpython, but this is likely irrelevant for the present question. The versions are:

  1. Python 2.7.12, installed with apt-get, residing in system dirs.
  2. Python 2.7.11, residing in ~/apps/ParaView-5.4.1-Qt5-OpenGL2-MPI-Linux-64bit, simply expanded from a tar file. To get the python prompt I run ~/apps/ParaView-5.4.1-Qt5-OpenGL2-MPI-Linux-64bit/bin/pvpython.

I mean to use readline from version 1 in version 2 (since it does not have its own, strange as it may be). To do this:

  1. Find where is readline in version 1:

    >>> import readline
    >>> readline.__file__
    '/usr/lib/python2.7/lib-dynload/readline.x86_64-linux-gnu.so'
    
  2. Use it in version 2, following this. I placed needed stuff in a directory dir1 which is an element of sys.path (I tried with both /home/santiago/apps/ParaView-5.4.1-Qt5-OpenGL2-MPI-Linux-64bit/lib/python2.7 and /home/santiago/apps/ParaView-5.4.1-Qt5-OpenGL2-MPI-Linux-64bit/lib/python2.7/lib-dynload).

    2.1. Get the .so file.

    $ cd dir1
    $ ln -s /usr/lib/python2.7/lib-dynload/readline.x86_64-linux-gnu.so
    

    2.2. Create readline.py

    $ nano readline.py
    

    with contents (as per ref above):

    def __bootstrap():
    global __bootstrap, __loader__, __file__
    import sys, pkg_resources, imp
    __file__ = pkg_resources.resource_filename(__name__,'readline.x86_64-linux-gnu.so')
    __loader__ = None; del __bootstrap, __loader__
    imp.load_dynamic(__name__,__file__)
    __bootstrap()
    

Now when I use version 2 with ~/apps/ParaView-5.4.1-Qt5-OpenGL2-MPI-Linux-64bit/bin/pvpython, I still get the error (that I wanted to get rid of)

ImportError: No module named readline

from an import in my ~/.pythonrc.

How can I import readline from version 1 into version 2?

Upvotes: 1

Views: 152

Answers (1)

I managed to solve this. The key was to link with the name readline.so instead of the original name. The rest was irrelevant.

In 2.1 of the OP:

$ cd ~/apps/ParaView-5.4.1-Qt5-OpenGL2-MPI-Linux-64bit/lib/python2.7/lib-dynload
$ ln -s /usr/lib/python2.7/lib-dynload/readline.x86_64-linux-gnu.so readline.so

That is it. It turns out that readline.py with __bootstrap (item 2.2) was not needed.

Upvotes: 1

Related Questions