Python NameError when var IS most definitely defined

后端 未结 3 742
遇见更好的自我
遇见更好的自我 2021-01-15 01:51
def make_pdf(self):
        self.get_filez()
        self.get_client()
        file_name = self.client_id+\"_\"+self.client_name+\"_\"+self.batch_num
        style =         


        
相关标签:
3条回答
  • 2021-01-15 02:36

    Try to print file_name after each line, to see if somebody is removing the "file_name" variable from your namespace.

    In addition, to be more pythonic (and efficient), use

    file_name = "_".join((self.client_id, self.client_name, self.batch_num))
    

    to concatenate strings.

    0 讨论(0)
  • 2021-01-15 02:41

    If you're assigning file_name here:

    file_name = self.client_id+"_"+self.client_name+"_"+self.batch_num
    

    And you're getting a NameError reporting, that file_name is not defined, then try wrapping the operation in a try..except, to see what is going wrong:

    try:
        file_name = self.client_id+"_"+self.client_name+"_"+self.batch_num
    except NameError as err:
        print err, 'failed, here is some debug stuff:'
        print "CLIENT ID   =", self.client_id
        print "CLIENT NAME =", self.client_name
        print "BATCH NUM   =", self.batch_num
    

    If any of this is failing, this will set you on the course to finding out why and narrowing down the cause of it.

    0 讨论(0)
  • 2021-01-15 02:45

    Mysterious NameErrors may arise from your file containing invisible control characters. On unix machines, you can spot these errors by looking at the output of

    cat -A filename.py
    
    0 讨论(0)
提交回复
热议问题