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