Code works everywhere except Jupyter

I have a fairly simple piece of code that works in the Python console, iPython, and in Bash. It doesn’t run in Jupyter.

When I run this anywhere except Jupyter it works as expected. I’m using the same version of Python everywhere. For some reason a variable is a “list” in Jupyter and a “dict” everywhere else. It’s expected to be a “dict”.

Here’s the error in Jupyter, expected results from everywhere else and the function -

Juypter -

Python version: 3.8.5
torrents_files: TYPE: <class 'dict'> LEN: 1942
files: TYPE: <class 'dict'> LEN: 1942

AttributeError                            Traceback (most recent call last)
<ipython-input-2-06f42731caef> in <module>
----> 1 torrent = t.find_torrent_by_filename('some_torrent_file.nfo')
      2 print(f"Torrent: {torrent}")

~/MediaManager/transmission.py in find_torrent_by_filename(self, filename)
     82             files = t[1]
     83             print(f"files: TYPE: {type(torrents_files)} LEN: {len(torrents_files)}")
---> 84             for k, v in files.items():
     85                 file_path = Path(v["name"])
     86                 file = file_path.name

AttributeError: 'list' object has no attribute 'items'

Python interpreter -

Python version: 3.8.5
torrents_files: TYPE: <class 'dict'> LEN: 1942
files: TYPE: <class 'dict'> LEN: 1942
v: TYPE: <class 'dict'> LEN: 5
Torrent: [{'id': 756, 'name': 'MyTorrent'}]

Bash -

$ python main.py 
Python version: 3.8.5
torrents_files: TYPE: <class 'dict'> LEN: 1942
files: TYPE: <class 'dict'> LEN: 1942
v: TYPE: <class 'dict'> LEN: 5
Torrent: [{'id': 756, 'name': 'MyTorrent'}]

iPython -

Python version: 3.8.5
torrents_files: TYPE: <class 'dict'> LEN: 1942
files: TYPE: <class 'dict'> LEN: 1942
v: TYPE: <class 'dict'> LEN: 5
Torrent: [{'id': 756, 'name': 'MyTorrent'}]

The function -

def find_torrent_by_filename(self, filename):
    from platform import python_version
    print(f"Python version: {python_version()}")
    torrents_files = self._transmission.get_files()
    print(f"torrents_files: TYPE: {type(torrents_files)} LEN: {len(torrents_files)}")
    torrents = []
    ts = []
    for t in torrents_files.items():
        torrent_id = t[0]
        files = t[1]
        print(f"files: TYPE: {type(torrents_files)} LEN: {len(torrents_files)}")
        for k, v in files.items():
            print(f"v: TYPE: {type(v)} LEN: {len(v)}")
            file_path = Path(v["name"])
            file = file_path.name
            if filename == str(file):
                ts.append(torrent_id)
    if ts:
        for t in ts:
            t = self._transmission.get_torrent(torrent_id=t)
            x = {"id": t.id, "name": t.name}
            torrents.append(x)
    return torrents

Wherever this function is defined is returning different object types in your working examples. I doubt it’s Jupyter related, though it might be the version of some dependency where you’re running the Jupyter kernel.

Python lists cannot be divided into separate lists based on characters that appear in the values of a list. This is unlike strings which values can be separated into a list. The AttributeError is an exception thrown when an object does not have the attribute you tried to access. The ‘list’ object has no attribute ‘split’ and you’re trying to call python split() function on the whole list of lines, and you can’t split a list of strings, only a string. So, you need to split each line, not the whole thing.

To solve the above problem, you need to iterate over the strings in the list to get individual strings; then, you can call the split() function.