python - 从命令行运行python脚本时找不到模块
问题描述
两者ISO8583.py
都ISO8583Errors.py
位于ISO8583_payment
文件夹内。另外,我创建了虚拟__init.py__
文件ISO8583_Payment
夹。从命令行运行 ISO8583.py 时,它会抛出一个错误。但是当我从 pyCharm IDE 运行时它可以工作。
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
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(
当我从命令提示符运行时,出现以下错误。
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'
解决方案
尝试以下操作:
import os
import sys
sys.path.append(os.path.realpath(os.path.dirname(os.path.dirname(__file__))))
是文件名ISO8583Errors.py
还是ISOErrors.py
?
这取决于你必须使用
from ISO8583_Payment.ISO8583Errors import ...
或者
from ISO8583_Payment.ISOErrors import ...
推荐阅读
- json - 错误“提取中位置 0 处 JSON 中的意外令牌 P”
- docker - 如何在docker中安装conan并使用
- java - Junit 参数化数组
- css - CSS Flexbox 第一个垂直列表,然后在垂直空间用完时启动多个列
- math - SVG 背后的数学原理是什么
筛选? - python-3.x - 如何在python中按点标签一一添加颜色和图例?
- google-sheets - 如何根据另一个工作表更新 Google 工作表中的数据?
- java - 如何拆分值并保存到 Map java?
- google-maps - 似乎无法向我的地图添加额外的 GLTF 标记(WebGL)
- python - MPI (mpi4py) - 如果第一个 test() 返回 false,则 irecv() 和 test() 不会在循环中工作