Import os restricted and module not found errors

kiosk1dge
kiosk1dge Newcomer
edited September 1 in Refinitiv Data Platform

Hi! I am trying to run the code described in this article to retrieve earnings calls from the Python Codebook in LSEG Workspace: https://developers.lseg.com/en/article-catalog/article/Uncovering_Tariff_Exposure_from_Transcripts . However when I try to import os and pysftp I get the following errors:

RefiImportError: Import 'os' is restricted.
ModuleNotFoundError: No module named 'pysftp'

Could you please advise how to solve the following errors?

Thank you in advance!

Answers

  • Jirapongse
    Jirapongse ✭✭✭✭✭
    edited September 1

    @kiosk1dge

    Thank you for reaching out to us.

    You can't use the os module on Codebook and the pysftp module is not available on Codebook.

    Therefore, you can't run that sample code on Codebook.

    You may need to setup your Jupyter Environment to run that example.

  • Thank you very much for your reply. I am now trying to run the example on my Jupyter Notebook. When I try to run this part of code:

    if download_from_sftp:
    print("Connecting to SFTP server...")
    
    cnopts = pysftp.CnOpts()
    cnopts.hostkeys = None
    
    with pysftp.Connection(host=hostname, username=username, password=password, cnopts=cnopts) as sftp:
        print("Connection successfully established!")
    
        sftp.cwd(directory)
    
        files = sftp.listdir_attr()
    
        x_days_ago = datetime.now() - timedelta(days=lookback_days)
        recent_files = [(file.filename, datetime.fromtimestamp(file.st_mtime)) for file in files if datetime.fromtimestamp(file.st_mtime) > x_days_ago]
    
        df_recent_files = pd.DataFrame(recent_files, columns=['Filename', 'CreationDate'])
    
        for index, row in df_recent_files.head(max_files).iterrows():
            remote_file = row['Filename']
            local_file = os.path.join(local_directory, remote_file)
            sftp.get(remote_file, local_file)
    
        print(f"Downloaded the most recent files into the '{local_directory}' directory.")
    

    I get the following error message:

    Connecting to SFTP server...
    
    C:\Miniforge3\Lib\site-packages\pysftp\__init__.py:61: UserWarning: Failed to load HostKeys from C:\Users\user\.ssh\known_hosts.  You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None).  
    warnings.warn(wmsg, UserWarning)
    
    ---------------------------------------------------------------------------
    gaierror                                  Traceback (most recent call last)
    File C:\Miniforge3\Lib\site-packages\pysftp\__init__.py:176, in Connection._start_transport(self, host, port)
        175 try:
    --> 176     self._transport = paramiko.Transport((host, port)) 
       177     # Set security ciphers if set
    File C:\Miniforge3\Lib\site-packages\paramiko\transport.py:484, in Transport.__init__(self, sock, default_window_size, default_max_packet_size, gss_kex, gss_deleg_creds, disabled_algorithms, server_sig_algs, strict_kex, packetizer_class)
    483 reason = "No suitable address family"
    --> 484 addrinfos = socket.getaddrinfo(
    485 hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM
    486 )
    487 for family, socktype, proto, canonname, sockaddr in addrinfos:

    File C:\Miniforge3\Lib\socket.py:978, in getaddrinfo(host, port, family, type, proto, flags)
    977 addrlist = []
    --> 978 for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
    979 af, socktype, proto, canonname, sa = res

    gaierror: [Errno 11001] getaddrinfo failed

    During handling of the above exception, another exception occurred:

    ConnectionException Traceback (most recent call last)
    Cell In[5], line 11
    8 cnopts.hostkeys = None
    10 # Establish the connection
    ---> 11 with pysftp.Connection(host=hostname, username=username, password=password, cnopts=cnopts) as sftp:
    12 print("Connection successfully established!")
    14 # Change to the target directory

    File C:\Miniforge3\Lib\site-packages\pysftp\__init__.py:140, in Connection.__init__(self, host, username, private_key, password, port, private_key_pass, ciphers, log, cnopts, default_path)
    138 # Begin the SSH transport.
    139 self._transport = None
    --> 140 self._start_transport(host, port)
    141 self._transport.use_compression(self._cnopts.compression)
    142 self._set_authentication(password, private_key, private_key_pass)

    File C:\Miniforge3\Lib\site-packages\pysftp\__init__.py:183, in Connection._start_transport(self, host, port)
    180 self._transport.get_security_options().ciphers = ciphers
    181 except (AttributeError, socket.gaierror):
    182 # couldn't connect
    --> 183 raise ConnectionException(host, port)

    ConnectionException: ('ftp-setranscripts.lseg.com', 22)

    Could you please suggest a way to solve the issue or if I should write a different hostname than the one in the example? The hostname in the example is "ftp-setranscripts.lseg.com".
    Thank you very much!

  • Jirapongse
    Jirapongse ✭✭✭✭✭

    It looks like to be a connection issue.

    The library can't establish a connection to the ftp-setranscripts.lseg.com server.

    You can use other ssh tools or contact your IT support team to verify the connection.