首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从命令行运行python脚本时未找到错误的模块

从命令行运行python脚本时未找到错误的模块
EN

Stack Overflow用户
提问于 2019-10-22 13:45:10
回答 2查看 911关注 0票数 0

我有一个python脚本,它接受file.txt作为参数。

当我从windows命令行运行时,它会抱怨以下错误。

代码语言:javascript
复制
*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*

基本上,ISO8583_Payment是我的主要项目的一个子文件夹,但出于某种原因,ISO8583_Payment被假定为一个模块,并抛出“模块未找到”一个错误。

ISO8583.py 8583.py

代码语言:javascript
复制
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])
EN

Stack Overflow用户

发布于 2019-10-24 16:41:03

@Mikah,当我从命令提示符运行时,您的建议不起作用。我之前提到过它对我有用,但是,我只是意识到它只有在我从pyCharm IDE运行时才能工作(很抱歉造成了混乱)。ISO8583.pyISO8583Errors.py都位于ISO8583_payment文件夹中。此外,我按照您的建议在ISO8583_Payment文件夹中创建了ISO8583_Payment

ISO8583.py 8583.py

代码语言:javascript
复制
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

代码语言:javascript
复制
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(

当我从命令提示符中运行时,仍然会得到以下错误。

代码语言:javascript
复制
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'
票数 -1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58505737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档