[英]module not found error when python script is run from command line
I have a python script which accepts the file.txt as an argument.我有一个 python 脚本,它接受 file.txt 作为参数。
When I run from the windows command line, it complains about the following error.当我从 windows 命令行运行时,它抱怨以下错误。
*C:\Projects\ATR220TA\ISO8583_Payment>python C:\Projects\ATR220TA\ISO8583_Payment\ISO8583.py C:\Projects\ISO8583.txt
Traceback (most recent call last):
File "C:\Projects\ATR220TA\ISO8583_Payment\ISO8583.py", line 19, in <module>
from ISO8583_Payment.ISOErrors import InvalidIso8583, ValueToLarge, InvalidValueType, InvalidBitType, BitInexistent, \
ModuleNotFoundError: No module named 'ISO8583_Payment'
C:\Projects\ATR220TA\ISO8583_Payment>pause*
Basically, ISO8583_Payment
is a subfolder of my main project, but for some reason, ISO8583_Payment
is assumed as a module and throws the "module not found" an error.基本上,
ISO8583_Payment
是我的主项目的子文件夹,但由于某种原因, ISO8583_Payment
被假定为一个模块,并引发“找不到模块”错误。
ISO8583.py ISO8583.py
def ParseRawMessage(ISO8583TextFile):
with open(ISO8583TextFile, 'rb') as in_file:
contents = in_file.read()
hex_bytes = binascii.hexlify(contents)
IsoStr = hex_bytes.decode("ascii")
Iso8583 = ISO8583()
try:
Iso8583.setIsoContent(IsoStr)
except InvalidMTI as error:
print("{0}".format(error))
except InvalidBitType as error:
print("{0}".format(error))
except ValueToLarge as error:
print("{0}".format(error))
except InvalidValueType as error:
print("{0}".format(error))
except BitInexistent as error:
print("{0}".format(error))
except BitNotSet as error:
print("{0}".format(error))
except InvalidIso8583 as error:
print("{0}".format(error))
bitsAndValuesDictionary = Iso8583.getBitsAndValues()
for v in bitsAndValuesDictionary:
print('%s (BIT-%s) = %s' % (v['name'], v['bit'], v['value']))
if __name__ == '__main__':
ParseRawMessage(sys.argv[1])
When you write当你写
from ISO8583_Payment.ISOErrors import InvalidIso8583, ValueToLarge, \
InvalidValueType, InvalidBitType, BitInexistent
You are telling Python that ISO8583_Payment
is a module.您告诉 Python
ISO8583_Payment
是一个模块。
So, you can add an empty __init__.py
file to that subdirectory or, if you want to leave it as a directory, use this code to add it to the system path so it will be searched:因此,您可以将一个空的
__init__.py
文件添加到该子目录,或者,如果您想将其保留为目录,请使用此代码将其添加到系统路径,以便对其进行搜索:
import sys
sys.path.append("/ISO8583_Payment")
@Mikah Barnett, Your suggestion did not work when I run from the command prompt. @Mikah Barnett,当我从命令提示符运行时,您的建议不起作用。 I have previously mentioned that it worked for me but, I just realized it only works when I run from the pyCharm IDE (sorry for the confusion).
我之前提到过它对我有用,但是我刚刚意识到它只有在我从 pyCharm IDE 运行时才有效(抱歉造成混淆)。 Both
ISO8583.py
and ISO8583Errors.py
are located inside the ISO8583_payment
folder. ISO8583.py
和ISO8583Errors.py
都位于ISO8583_payment
文件夹内。 Also, I created __init.py__
inside ISO8583_Payment
folder as per your suggestion.另外,我根据您的建议在
ISO8583_Payment
文件夹中创建了__init.py__
。
ISO8583.py ISO8583.py
import sys
sys.path.append("/ISO8583_Payment")
from ISO8583_Payment.ISOErrors import InvalidBitType,InvalidMTI,InvalidValueType,InvalidIso8583,ValueToLarge,BitInexistent,BitNotSet
''' I did not copy all the source code in here ''''
def ParseRawMessage(ISO8583TextFile):
with open(ISO8583TextFile, 'rb') as in_file:
contents = in_file.read()
hex_bytes = binascii.hexlify(contents)
IsoStr = hex_bytes.decode("ascii")
Iso8583 = ISO8583()
try:
Iso8583.setIsoContent(IsoStr)
except InvalidMTI as error:
print("{0}".format(error))
except InvalidBitType as error:
print("{0}".format(error))
except ValueToLarge as error:
print("{0}".format(error))
except InvalidValueType as error:
print("{0}".format(error))
except BitInexistent as error:
print("{0}".format(error))
except BitNotSet as error:
print("{0}".format(error))
except InvalidIso8583 as error:
print("{0}".format(error))
bitsAndValuesDictionary = Iso8583.getBitsAndValues()
for v in bitsAndValuesDictionary:
print('%s (BIT-%s) = %s' % (v['name'], v['bit'], v['value']))
if __name__ == '__main__':
ParseRawMessage(sys.argv[1])
ISOErrors.py ISOErrors.py
class ValueToLarge(Exception):
def __init__(self, value):
self.str = value
def __str__(self):
return repr(self.str)
class BitInexistent(Exception):
def __init__(self, value):
self.str = value
def __str__(self):
return repr(self.str)
class InvalidValueType(Exception):
def __init__(self, value):
self.str = value
def __str__(self):
return repr(self.str)
class InvalidBitType(Exception):
def __init__(self, value):
self.str = value
def __str__(self):
return repr(self.str)
class InvalidIso8583(Exception):
def __init__(self, value):
self.str = value
def __str__(self):
return repr(self.str)
class InvalidMTI(Exception):
def __init__(self, value):
self.str = value
def __str__(self):
return repr(self.str)
#Exception that indicate that bit is not there.
class BitNotSet(Exception):
def __init__(self, value):
self.str = value
def __str__(self):
return repr(
When I run form the command prompt, I still get following error.当我从命令提示符运行时,我仍然收到以下错误。
C:\Projects\ATR220TA_Work_On_Progress\ISO8583_Payment>python C:\Projects\ATR220TA_Work_On_Progress\ISO8583_Payment
C:\Users\gobiraaj.anandavel\AppData\Local\Programs\Python\Python37-32\python.exe: can't find '__main__' module in 'C:\\Projects\\ATR220TA_Work_On_Progress\\ISO8583_Payment'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.