Search code examples
python-3.xglobal-variables

Global variable 'not defined' when decleared in another function


I'm trying to run a package developed by others and it is giving me NameError: name 'hg38' is not defined error when I run. I try to summarize the issue here:

There is a python file with two functions. The first one has a global variable as

#global_var_test.py

def print_sth(a='ath'):
    global hg38
    hg38 = 555
    <do something here>
    

def process_del(gen):
    print(gen) 

I call this file from another script as

import global_var_test

global_var_test.print_sth()
global_var_test.process_del(hg38)

which returns the error

NameError: name 'hg38' is not defined

What I'm trying summarize here is actually from this repository: https://github.com/jzhoulab/orca/tree/main

running the line (given in README) outputs = process_dup('chr17', 70845859, 71884859, hg38, file='./chr17_70845859_71884859_RevSex_dup_maximum_Sox9', show_genes=True) returns the same error. Variable is defined in this function: https://github.com/jzhoulab/orca/blob/87beec0e52f9366043fda0d18a639d54c7a3bf04/orca_predict.py#LL37C25-L37C25 It is called here: https://github.com/jzhoulab/orca/blob/87beec0e52f9366043fda0d18a639d54c7a3bf04/orca_predict.py#L1163

Any ideas? I'd appreciate it if you can show me where I do wrong.


Solution

  • As @jsbueno commented, the second script should actually be

    import global_var_test
    
    global_var_test.print_sth()
    global_var_test.process_del(global_var_test.hg38)