Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

“RuntimeError: dictionary keys changed during iteration” when attempting to call add_attachment


I am attempting to just add a CSV file to my issues as a test, but I keep receiving the error:

RuntimeError: dictionary keys changed during iteration

Here is the code (I've removed the parameters for server, username and password:
from jira import JIRA

options = {"server": "serverlinkgoeshere"}
jira = JIRA(options, basic_auth=('username', 'password'))
issuesList = jira.search_issues(jql_str='', startAt=0, maxResults=100)
for issue in issuesList:
with open("./csv/Adobe.csv",'rb') as f:
jira.add_attachment(issue=issue, attachment=f)
I'm at a loss, I'm not changing any dictionary keys in my code. Here is the full error message:

Traceback (most recent call last):
File "C:/Users/USER/PycharmProjects/extractor/main/", line 8, in <module>
jira.add_attachment(issue=issue, attachment=f)
File "C:\Users\USER\AppData\Roaming\Python\Python38\site-packages\jira\", line 126, in wrapper
result = func(*arg_list, **kwargs)
File "C:\Users\USER\AppData\Roaming\Python\Python38\site-packages\jira\", line 787, in add_attachment
url, data=m, headers=CaseInsensitiveDict({'content-type': m.content_type, 'X-Atlassian-Token': 'nocheck'}), retry_data=file_stream)
File "C:\Users\USER\AppData\Roaming\Python\Python38\site-packages\jira\utils\", line 41, in __init__
for key, value in super(CaseInsensitiveDict, self).items():
RuntimeError: dictionary keys changed during iteration

Jira add_attachment example:

add_attachment source code:

3 answers

The bug is in the jira library.  From .../jira/utils/

    for key, value in super(CaseInsensitiveDict, self).items():
if key != key.lower():
self[key.lower()] = value
self.pop(key, None)

Should be something like:

    for key in [x for x in super(CaseInsensitiveDict, self).keys() if x != x.lower()]:
        self[key.lower()] = self.pop(key, None)

(Iterate over a copy of the keys that need to be modified instead of over the dict to be modified.)

Modifying a dict while iterating over it has always been a bug, but Python 3.8 enforces this.

Any timeline on implementing suggested fix?  Being able to use 3.8 would solve some dependency hell I'm currently facing.

Still looking to see if there is a fix as of end of May 2020

The fix is to stop using code that does the wrong thing, as given December 2019

The official fix hasn't been merged yet but exists here

They essentially iterate over the dictionary looking for non lowercase key names, then in the next loop they recreate the key in lowercase and remove the one with uppercase. Two separate loops, rather than modifying the dict while also iterating it.

You need to modify the site-packages/jira/utils/ file and make the changes you see at the link above, until it gets merged and pushed out officially.

Like Ana Bennett likes this

Solution. Changed Python Interpreter from 3.8 to 3.7


Hope this helps someone else in the future. 

Helped me today, thank you!

What if i can't? (the code runs on a remote vm not under my management)

This can't be an accepted solution.. I proposed a monkey patch for someone in need.

I agree - the problem is not the version of Python (nor is it in the Jira API), the problem is in the code, and your patch corrects it.

0 votes

You'll need to rework your python code so that it does not alter the object while it is running over it.

This is a slightly different error, but is the same solution -

Guys, it's really quite weird especially in case Ubuntu 20.04 ('Python 3.8.2' version by default). I need to do custom migrations for some project, but I can't do this due to described problem with Python 3.8.2. So, it would be really helpful to get proper solution ASAP! Please, check Chuck_Phillips suggestion. Is it suitable? Thank you in advance!

@Nic Brough _Adaptavist_ can you please advise about this part:

from jira import JIRA
jira = JIRA(options, basic_auth=(user, apikey))
project = jira.create_project('TTTP', 'Temporary Toast Test Project')

After its execution I get an error also:

'RuntimeError: dictionary keys changed during iteration' 

Your code is altering a collection while the collection is being iterated over.

  • Do not do this
  • See the link I gave to see why it's wrong and how to do it the right way

@Nic Brough _Adaptavist_  why are you telling people who are simply using an API provided to them, to stop using it? It is up to the developers to officially fix this problem.

The problem lies in the Jira module, not any persons individual code presented here. Its not their code, its the API code that is behaving badly.

You need to be clear about what you mean by API.

You need to be clear that there is nothing wrong with the Jira API.  The problem is with the Python module.  Until the Python (module) developers fix that, the only useful advice here is "don't use functions that are broken", or, better, "use the calls the right way"

Suggest an answer

Log in or Sign up to answer

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you