Merge_metaphlan_tables.py UnboundLocalError

There is an error when running merge_metaphlan_tables.py *.rel_ab_w_read_stats.clade_profiles.txt -o merged.rel_ab_w_read_stats.clade_profiles.txt. Same error whether I use -o or >. Output files are in rel_ab_w_read_stats format. I recently installed metaphlan via conda biobakery

Traceback (most recent call last):
  File "/home/amirza/.conda/envs/biobakery3/bin/merge_metaphlan_tables.py", line 10, in <module>
    sys.exit(main())
  File "/home/amirza/.conda/envs/biobakery3/lib/python3.7/site-packages/metaphlan/utils/merge_metaphlan_tables.py", line 81, in main
    merge(args.aistms, fout)
  File "/home/amirza/.conda/envs/biobakery3/lib/python3.7/site-packages/metaphlan/utils/merge_metaphlan_tables.py", line 46, in merge
    names = names,
UnboundLocalError: local variable 'names' referenced before assignment

Can you replace your merge_metaphlan_tables.py from the one present in the repository?
Another user reported the same problem here, I posted a solution a couple of weeks ago.

1 Like

An UnboundLocalError is raised when a local variable is referenced before it has been assigned. In most cases this will occur when trying to modify a local variable before it is actually assigned within the local scope. Python doesn’t have variable declarations, so it has to figure out the scope of variables itself. It does so by a simple rule: If there is an assignment to a variable inside a function, that variable is considered local.

Python has lexical scoping by default, which means that although an enclosed scope can access values in its enclosing scope, it cannot modify them (unless they’re declared global with the global keyword). A closure binds values in the enclosing environment to names in the local environment. The local environment can then use the bound value, and even reassign that name to something else, but it can’t modify the binding in the enclosing environment. UnboundLocalError happend because when python sees an assignment inside a function then it considers that variable as local variable and will not fetch its value from enclosing or global scope when we execute the function. However, to modify a global variable inside a function, you must use the global keyword.