Calling a .py script which uses "import()" from Jupyter Lab throws UnicodeDecodeError

I have a class (IBAsset) implemented in a script (assets.py). The property IBAsset.daily_returns uses python’s import() module to ask for user input. As long as I create the IBAsset instance from an IPython console, it all works fine and I get prompted for input via the console. However, once I create the IBAsset instance from within Jupyter Lab, I get the following UnicodeDecodeError:

---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-4-56b6b5771106> in <module>
      1 from paulfolio.assets import IBAsset
      2 asset = IBAsset(symbol='IEMG', secType='STK', exchange='NYSE')
----> 3 asset.daily_returns

~/paulfolio/paulfolio/assets.py in daily_returns(self)
    206         if self._historical_data is None:
    207             if input(
--> 208                 f"Historical data for {self.identifier} has not been fetched - Fetch now?n(y/n): "
    209             ).lower() in ['yes','y']:
    210                 self._fetch_data()

/usr/local/lib/python3.7/site-packages/ipykernel/kernelbase.py in raw_input(self, prompt)
    858             self._parent_ident,
    859             self._parent_header,
--> 860             password=False,
    861         )
    862 

/usr/local/lib/python3.7/site-packages/ipykernel/kernelbase.py in _input_request(self, prompt, ident, parent, password)
    869         while True:
    870             try:
--> 871                 self.stdin_socket.recv_multipart(zmq.NOBLOCK)
    872             except zmq.ZMQError as e:
    873                 if e.errno == zmq.EAGAIN:

/usr/local/lib/python3.7/site-packages/zmq/sugar/socket.py in recv_multipart(self, flags, copy, track)
    564             for any of the reasons :func:`~Socket.recv` might fail
    565         """
--> 566         parts = [self.recv(flags, copy=copy, track=track)]
    567         # have first part already, only loop while more to receive
    568         while self.getsockopt(zmq.RCVMORE):

zmq/backend/cython/socket.pyx in zmq.backend.cython.socket.Socket.recv()

zmq/backend/cython/socket.pyx in zmq.backend.cython.socket.Socket.recv()

zmq/backend/cython/socket.pyx in zmq.backend.cython.socket._recv_copy()

zmq/backend/cython/socket.pyx in zmq.backend.cython.socket._recv_copy()

/usr/local/lib/python3.7/site-packages/zmq/backend/cython/checkrc.pxd in zmq.backend.cython.checkrc._check_rc()

/usr/local/lib/python3.7/site-packages/zmq/error.py in __init__(self, errno, msg)
    100     def __init__(self, errno='ignored', msg='ignored'):
    101         from zmq import EAGAIN
--> 102         super(Again, self).__init__(EAGAIN)
    103 
    104 

/usr/local/lib/python3.7/site-packages/zmq/error.py in __init__(self, errno, msg)
     41             self.errno = errno
     42             if msg is None:
---> 43                 self.strerror = strerror(errno)
     44             else:
     45                 self.strerror = msg

zmq/backend/cython/error.pyx in zmq.backend.cython.error.strerror()

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfc in position 37: invalid start byte

assets.py:

@property
def daily_returns(self) -> pd.Series:
    if self._historical_data is None:
        if input(
            f"Historical data for {self.identifier} has not been fetched - Fetch now?n(y/n): "
        ).lower() in ['yes','y']:
            self._fetch_data()
        else:
            raise Exception('Historical data has not been fetched!')
        
    return self._compute_asset_prices_and_returns(
        historical_data=self._historical_data,
        nb_offset_days=1
    )[1]

Jupyter Notebook (Jupyter Lab):

from assets import IBAsset
asset = IBAsset(symbol='IEMG', secType='STK', exchange='NYSE')
asset.daily_returns

I’m working on a dockerized Ubuntu 18.04 container with python 3.7.9 and the following Jupyter dependencies:

jupyter core     : 4.7.0
jupyter-notebook : 6.1.5
qtconsole        : not installed
ipython          : 7.19.0
ipykernel        : 5.4.2
jupyter client   : 6.1.7
jupyter lab      : 2.2.9
nbconvert        : 6.0.7
ipywidgets       : 7.5.1
nbformat         : 5.0.8
traitlets        : 5.0.5

Any help is very much appreciated!

Cheers

Source: Docker Questions

LEAVE A COMMENT