NameError: name 'ls' is not defined

Hello,

I am a newbie on the forum so please forgive me if I make some mistakes in the process and the informations given.

The context :
I’m migrating python scripts from a RHEL4.x server to a RHEL8.10 server.

The issue :
On my RHEL8.X server, I have an error “NameError: name ‘ls’ is not defined” when launching the python script on RHEL8.10 server.
The script was executed successfully on RHEL4.x server

The script is launched with this command :
source /home/mcdo/.bash_profile;/usr/local/bin/python3 /apps/mcdo_batchcorpo/prod/bin/moteur_adapter002.py >> /data/log/mcdo/nie_moteur.log

Informations on python3 on the RHEL 4.x server :

$ /usr/local/bin/python3 --version
Python 3.1.1

Informations on python3 on the RHEL 8.x server :

$ /usr/local/bin/python3 --version
Python 3.1

Content of /home/mcdo/.bash_profile :

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi



# User specific environment and startup programs
DB2INSTANCE=db2user
ANT_HOME=/data/softs/ant
#JAVA_HOME=/data/softs/sun/java:/data/softs/sun/jdk1.5.0_17
JAVA_HOME=/data/softs/sun/java
#PATH=.:/bin:/data/ibm/db2/V9.1/bin:/data/softs/ant/bin:$JAVA_HOME/bin:$PATH:$HOME/bin
PATH=.:/bin:/opt/ibm/db2/V9.1/bin:/data/softs/ant/bin:$JAVA_HOME/bin:$PATH:$HOME/bin
LANG=fr_FR@euro
DB2_PATH=$PATH

export JAVA_HOME PATH LANG DB2_PATH ANT_HOME

unset USERNAME

export LOG=/ord/var/log/VNIA1/
export IN=/data/reception/NIA/
export OUT=/data/transfert/NIA/VNIA1/

Content of the log file /data/log/mcdo/nie_moteur.log :

--Return--
> /usr/local/lib/python3.1/encodings/iso8859_15.py(15)decode()->('<string>', 8)
-> return codecs.charmap_decode(input,errors,decoding_table)
(Pdb) (Pdb) *** NameError: name 'ls' is not defined
(Pdb) Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /usr/local/lib/python3.1/cmd.py(127)cmdloop()
-> line = input(self.prompt)
(Pdb) *** NameError: name 'ls' is not defined
(Pdb) --Return--
> /usr/local/lib/python3.1/encodings/iso8859_15.py(15)decode()->('<string>', 8)
-> return codecs.charmap_decode(input,errors,decoding_table)
(Pdb) lot:['NIE001R.10107.20241021.3835125']
lock:NIE001R.10107.20241021.3835125
trait:NIE001R.10107.20241021.3835125
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /usr/local/lib/python3.1/encodings/iso8859_15.py(23)decode()
-> return codecs.charmap_decode(input,self.errors,decoding_table)[0]
(Pdb) /ord/var/archive/VNIE0/10107/20241021/3835125/
/ord/var/archive/VNIE0/10107/20241021/3835125/tmp/
/ord/var/archive/VNIE0/10107/20241021/3835125/tmp/envoimail.cfg
/ord/var/archive/VNIE0/10107/20241021/3835125/tmp/nie001.dat
/ord/var/archive/VNIE0/10107/20241021/3835125/tmp/nie002.dat
/ord/var/archive/VNIE0/10107/20241021/3835125/tmp/NIEP002_log_config.xml
/ord/var/archive/VNIE0/10107/20241021/3835125/tmp/niep002.properties
/ord/var/archive/VNIE0/10107/20241021/3835125/tmp/NIEP005_log_config.xml
/ord/var/archive/VNIE0/10107/20241021/3835125/tmp/niep005.properties
/ord/var/archive/VNIE0/10107/20241021/3835125/log/
/ord/var/archive/VNIE0/10107/20241021/3835125/log/NIE002.log
/ord/var/archive/VNIE0/10107/20241021/3835125/svlog/
lot:['NIE001R.10107.20241021.3835125']
lock:NIE001R.10107.20241021.3835125
trait:NIE001R.10107.20241021.3835125
unlock:NIE001R.10107.20241021.3835125
lot:[]
/ord/var/archive/VNIE0/10107/20241021/3835125/
/ord/var/archive/VNIE0/10107/20241021/3835125/tmp/
/ord/var/archive/VNIE0/10107/20241021/3835125/tmp/envoimail.cfg
/ord/var/archive/VNIE0/10107/20241021/3835125/tmp/nie001.dat
/ord/var/archive/VNIE0/10107/20241021/3835125/tmp/nie002.dat
/ord/var/archive/VNIE0/10107/20241021/3835125/tmp/NIEP002_log_config.xml
/ord/var/archive/VNIE0/10107/20241021/3835125/tmp/niep002.properties
/ord/var/archive/VNIE0/10107/20241021/3835125/tmp/NIEP005_log_config.xml
/ord/var/archive/VNIE0/10107/20241021/3835125/tmp/niep005.properties
/ord/var/archive/VNIE0/10107/20241021/3835125/log/
/ord/var/archive/VNIE0/10107/20241021/3835125/log/NIE002.log
/ord/var/archive/VNIE0/10107/20241021/3835125/svlog/
lot:['NIE001R.10107.20241021.3835125']
lock:NIE001R.10107.20241021.3835125
trait:NIE001R.10107.20241021.3835125
unlock:NIE001R.10107.20241021.3835125

Content of the moteur_adapter002.py script :

'''
Created on Apr 21, 2010

@author: wangl
'''

import time
import os
from utils import util
from conf.common import *
from utils import listfile, util
from moteurlot import MoteurLot
from moteur_adapter import MoteurAdapter

class MoteurAdapter002(MoteurAdapter):

    def __init__(self):
        '''
        Constructor
        '''
        MoteurAdapter.__init__(self)

         #override
    def getFlux(self):

        t_time = time.strftime("%Y-%m-%d %H:%M:%S")

        file = getFirstFile(PATH_RECEPTION)
        if file==None:
            return None

        if util.isLock(PATH_LOCK+file)==True:
            self.logger.info("File Name is lock.")
            return None

        if util.canReadFile(PATH_RECEPTION+file)==False:
            return None

        name,codeBanque,period,idset=file.split(".")

        if name.find('NIE001') > -1:
            conf="NIE002."+codeBanque+"."+period
        elif name.find('NIE002') > -1:
            print()
        else:
            return None

        file_conf = listfile.getFirstFileByName(PATH_RECEPTION,conf)



        return none


if __name__ == '__main__':
    file_lock_traitement=PATH_LOCK+"taitement_en_cour"
    if util.isLock(file_lock_traitement)==False:
        util.lock(file_lock_traitement)
        motorAdapter = MoteurAdapter()
        motorAdapter.run()
        util.unlock(open(file_lock_traitement,"w"),file_lock_traitement)

Content of the called script /usr/local/lib/python3.1/encodings/iso8859_15.py :

""" Python Character Mapping Codec iso8859_15 generated from 'MAPPINGS/ISO8859/8859-15.TXT' with gencodec.py.

"""#"

import codecs

### Codec APIs

class Codec(codecs.Codec):

    def encode(self,input,errors='strict'):
        return codecs.charmap_encode(input,errors,encoding_table)

    def decode(self,input,errors='strict'):
        return codecs.charmap_decode(input,errors,decoding_table)

class IncrementalEncoder(codecs.IncrementalEncoder):
    def encode(self, input, final=False):
        return codecs.charmap_encode(input,self.errors,encoding_table)[0]

class IncrementalDecoder(codecs.IncrementalDecoder):
    def decode(self, input, final=False):
        return codecs.charmap_decode(input,self.errors,decoding_table)[0]

class StreamWriter(Codec,codecs.StreamWriter):
    pass

class StreamReader(Codec,codecs.StreamReader):
    pass

### encodings module API

def getregentry():
    return codecs.CodecInfo(
        name='iso8859-15',
        encode=Codec().encode,
        decode=Codec().decode,
        incrementalencoder=IncrementalEncoder,
        incrementaldecoder=IncrementalDecoder,
        streamreader=StreamReader,
        streamwriter=StreamWriter,
    )


### Decoding Table

decoding_table = (
    '\x00'     #  0x00 -> NULL
    '\x01'     #  0x01 -> START OF HEADING
    '\x02'     #  0x02 -> START OF TEXT
    '\x03'     #  0x03 -> END OF TEXT
    '\x04'     #  0x04 -> END OF TRANSMISSION
    '\x05'     #  0x05 -> ENQUIRY
    '\x06'     #  0x06 -> ACKNOWLEDGE
    '\x07'     #  0x07 -> BELL
    '\x08'     #  0x08 -> BACKSPACE
    '\t'       #  0x09 -> HORIZONTAL TABULATION
    '\n'       #  0x0A -> LINE FEED
    '\x0b'     #  0x0B -> VERTICAL TABULATION
    '\x0c'     #  0x0C -> FORM FEED
    '\r'       #  0x0D -> CARRIAGE RETURN
    '\x0e'     #  0x0E -> SHIFT OUT
    '\x0f'     #  0x0F -> SHIFT IN
    '\x10'     #  0x10 -> DATA LINK ESCAPE
    '\x11'     #  0x11 -> DEVICE CONTROL ONE
    '\x12'     #  0x12 -> DEVICE CONTROL TWO

I remain at your disposal to answer any remarks or questions you may have
Thanks in advance
Best regards.

Please share all of the traceback so that we can see the context of the error.

What do you mean ?
I have shared all the traces I have for the moment in my initial message (cf. /data/log/mcdo/nie_moteur.log content)

The first step will be finding the line that this error actually happens on, which doesn’t appear to be anywhere in the traceback or the code provided. Where is "ls" coming from?

The traceback is not in the log file, because the launch command

only redirects stdout, not stderr, where the traceback is printed. The traceback would be printed to the console, or discarded if the script is not run from a console.

Change the launch script to

source /home/mcdo/.bash_profile;/usr/local/bin/python3 /apps/mcdo_batchcorpo/prod/bin/moteur_adapter002.py >> /data/log/mcdo/nie_moteur.log 2>&1

to also log the traceback.

2 Likes

Hello,

Thanks all for your feedbacks.
I have resolved the issue myself.
In fact, the script is working on the old RHEL4.x server which is using Python 3.1.1 and I had installed python 3.1 before launching the script on the new server. After reinstalling python to 3.1.1 version, these “NameError” messages disappeared.

Now, I’m encountering others errors but I’ll open a new topic if needed.
I’am also noting the tips to enable tracebook in python scripts.

Best regards.