我的脚本在Pycharm上使用Python3.6很好,但是当我使用Python3.6从ubuntu 16运行时有一些问题
错误:
python3.6 AttributeInsertion.py加载库模块..。_unify_values sectiondict = self._sectionssection KeyError: test_configurations中的文件"/usr/lib/python3.6/configparser.py",第1138行 在处理上述异常期间,出现了另一个异常:AttributeInsertion.py文件,第49行,在jsonFeatureFile = readstringconfigfile('test_configurations','resourceName')文件"AttributeInsertion.py“中,第15行,recent文件字段值=parser.get(节,字段)文件”/usr/lib/python3.6/parconfigser.py“,第781行,get d=self._unify_values(节,vars)文件”/usr/python3.6/configparser.py“,第1141行,在"/usr/lib/python3/dist-packages/apport_python_hook.py",NoSectionError(节)中,configparser.NoSectionError: No节:sys.excepthook中的'test_configurations‘错误:回溯(最近一次调用):文件NoSectionError第63行,在apport.fileutils导入likely_packaged的apport_excepthook中,get_recent_crashes File likely_packaged第5行,在“从"/usr/lib/python3/dist-packages/apport/report.py",导入报告文件”"/usr/lib/python3/dist-packages/apport/fileutils.py",第30行、“导入apport.fileutils文件”第23行、“从apport.packaging_impl导入impl作为包装文件”"/usr/lib/python3/dist-packages/apport/packaging_impl.py",第23行中,在导入apt文件"/usr/lib/python3/dist-packages/apt/__init__.py",第23行中,在import apt_pkg ModuleNotFoundError: No模块中名为'apt_pkg‘的原始异常是: Traceback (最近一次调用):File "/usr/lib/python3.6/configparser.py",第1138行,在_unify_values sectiondict = self._sectionssection KeyError:'test_configurations’中处理上述异常,出现了另一个例外:AttributeInsertion.py文件,第49行,在jsonFeatureFile = readstringconfigfile('test_configurations','resourceName')文件"AttributeInsertion.py“中,第15行,readstringconfigfile字段值=parser.get(区段,字段) File "/usr/lib/python3.6/configparser.py",第781行,在get d=self._unify_values(节,vars)文件”/usr/lib/python3.6/configparser.py“中,第1141行,在_unify_values NoSectionError(节) configparser.NoSectionError: No节:'test_configurations‘
from configparser import ConfigParser
from time import sleep
import json
import datetime
import requests
print('Loading library modules...')
def readstringconfigfile(section, field):
try:
parser = ConfigParser()
configfilename = "..\\resources\\config.ini"
parser.read(configfilename)
fieldvalue = parser.get(section, field)
print(f'Read string config file {fieldvalue}.')
return fieldvalue[1:-1]
except FileNotFoundError:
print("Cannot find the config file.")
def replace_property_value(property, value):
try:
print('Old ' + property + ': ' + feature['properties'][property]) # print old value
feature['properties'][property] = value # override old value with new
print('New ' + property + ': ' + feature['properties'][property])
except Exception as e:
print(repr(e))
def replace_payload_value(value):
try:
print('Old payload: ' + feature['properties']['payload'][0])
feature['properties']['payload'][0] = value
print('New payload: ' + feature['properties']['payload'][0])
except Exception as e:
print(repr(e))
def replace_user_name(value):
try:
print(feature['properties']['user']['name'])
feature['properties']['user']['name'] = value
print('New payload: ' + feature['properties']['user']['name'])
except Exception as e:
print(repr(e))
jsonFeatureFile = readstringconfigfile('test_configurations', 'resourceName')
jsonFeatureFile = "{0}{1}".format("..\\resources\\", jsonFeatureFile)
print(f'Resource file name is {jsonFeatureFile}.')
xyzSitBearerToken = readstringconfigfile('sit_configurations', 'xyzSitBearerToken')
xyzSitBearerToken = "\"" + xyzSitBearerToken + "\""
print(f'XYZ Bearer Token is {xyzSitBearerToken}.')
# Read FeatureCollection from JSON file:
print('Reading FeatureCollection from JSON file.')
with open(jsonFeatureFile, 'r', encoding='utf-8') as f:
try:
featureJson = json.load(f)
print('Opening FeatureCollection.json', f)
#print(f'Raw feature from resource file is {json.dumps(rawFeatureJson, indent=4)}.')
except Exception as e:
print(repr(e))
try:
headers = {'Content-Type': 'application/json',
'Authorization': 'Bearer {}'.format(xyzSitBearerToken)}
e2eXyzSemiPerm = readstringconfigfile('sit_configurations', 'e2eXyzSemiPerm')
e2eXyzBaseUrl = readstringconfigfile('sit_configurations', 'e2eXyzBaseUrl')
xyzPutUrl = e2eXyzBaseUrl + e2eXyzSemiPerm + "/features"
print(f'PUT request to XYZ url - {xyzPutUrl}.')
for feature in featureJson['attributes']:
currentTime = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%SZ')
print(f'Current Time is {currentTime}.')
replace_property_value("creationTime", currentTime)
replace_property_value("startTime", currentTime)
replace_property_value("informationType", "gottlieb-sit-semi-permanent")
replace_payload_value("0xFF")
replace_user_name("Pradeep Thomas Thundiyil")
print(f'PUT Feature - {json.dumps(feature, indent=4)}.')
response = requests.put(xyzPutUrl, json=feature, headers=headers)
print(f'PUT feature to XYZ url - {response.url}.')
print(f'PUT request response to XYZ {response.text}.')
print(f'Response of PUT request to XYZ {response.status_code}.')
print(response.json)
print(f'Sleeping for 20 seconds.')
sleep(20)
except KeyError as e:
print(repr(e))发布于 2021-09-26 10:41:05
在这里,变量'configfilename‘中提供的路径没有指向config.ini文件的确切位置。默认目录可以根据环境/服务器/模块/等等而不同;这不是程序存在的目录。因此,您可以使代码指向文件存在的当前目录。
您可以尝试导入OS模块(导入os),并在变量“configfilename”中进行以下更改,该变量指向目录“config.ini”目录下的“资源”文件。
configfilename = (os.path.join(os.path.dirname(__file__),'resources','config.ini'))https://stackoverflow.com/questions/55145736
复制相似问题