我正在用Python 3创建一个货币转换器程序,同时尝试清理一下我的代码,因此决定将常量保存在一个名为 settings.py 的单独文件中,然后导入设置无论在哪里使用常量。
我以前在以前的项目中已经做过,但是由于某种原因,它现在不想工作,并且很难找到原因。请记住,我不过是初学者。
包括回溯在内的错误是:
: [MSG] : Starting up...
Traceback (most recent call last):
File "main.py", line 1, in <module>
import settings
File "D:\Work\Programming\Python\Independent\CurrencyConverter\settings.py", line 3, in <module>
import main
File "D:\Work\Programming\Python\Independent\CurrencyConverter\main.py", line 82, in <module>
getData()
File "D:\Work\Programming\Python\Independent\CurrencyConverter\main.py", line 9, in getData
da.getFile()
File "D:\Work\Programming\Python\Independent\CurrencyConverter\dataAccquisition.py", line 22, in getFile
if isDataOld():
File "D:\Work\Programming\Python\Independent\CurrencyConverter\dataAccquisition.py", line 10, in isDataOld
if os.path.isfile(settings.FILE):
AttributeError: module 'settings' has no attribute 'FILE'
dataAccquisition.py
#Currency conversions - dataAccquisition.py
import settings
import requests
from xml.etree import cElementTree as ET
import os
import time
def isDataOld():
if os.path.isfile(settings.FILE):
fileTime = os.path.getmtime(settings.FILE)
if (time.time() - fileTime) / 3600 > 24*1:
print(' : [MSG] : Data is old.')
return True
else:
print(' : [MSG] : Data is up to date.')
return False
print(' : [MSG] : Data does not exist.')
return True
def getFile():
if isDataOld():
print(' : [MSG] : Finding and downloading data from URL.')
r = requests.get(settings.URL, allow_redirects=True)
open('exData.xml', 'wb').write(r.content)
def parseData():
print(' : [MSG] : Starting to parse data...')
tree = ET.ElementTree(file=settings.FILE)
root = tree.getroot()
datalist_currency = []
datalist_rates = []
print(' : [MSG] : Adding currency and rates to datalist=')
for child in root:
for subchild in child:
for subsubchild in subchild:
print(' : [MSG] : Adding currency',subsubchild.attrib['currency'])
datalist_currency.append(subsubchild.attrib['currency'])
print(' : [MSG] : with rate',subsubchild.attrib['rate'])
datalist_rates.append(subsubchild.attrib['rate'])
datalist_zip = dict(zip(datalist_currency, datalist_rates))
print(' : [MSG] : Done parsing data!')
return datalist_zip
settings.py
#Currency conversions - settings.py
import main
import os
URL = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"
DIR = os.path.dirname(os.path.realpath(__file__))
FILE = os.path.join(DIR, 'exData.xml')
OPTIONS = {'USD' : main.toUSD, 'JPY' : main.toJPY, 'BGN' : main.toBGN, 'CZK' : main.toCZK,
'DKK' : main.toDKK, 'GBP' : main.toGBP, 'HUF' : main.toHUF, 'PLN' : main.toPLN,
'RON' : main.toRON, 'SEK' : main.toSEK, 'CHF' : main.toCHF, 'ISK' : main.toISK,
'NOK' : main.toNOK
}
我的目录结构如下:
$ tree
.
├── dataAccquisition.py
├── main.py
├── settings.py
├── exData.xml
0 Directories, 4 files
main.py
import settings
import dataAccquisition as da
data = []
def getData():
global data
print(' : [MSG] : Starting up...')
da.getFile()
data = da.parseData()
print(' : [MSG] : Start up done, ready to be used.')
def getRate(currencyName):
return data[currencyName]
def menu():
def text():
print('''\n----------------MENU----------------
- 0. Convert currency -
- 1. Display all rates -
- 2. Retry to download data -
- 3. Quit -
------------------------------------''')
run = True
while run:
text()
choice = input('s: ')
if choice == '0':
for currency, rates in data.items():
print("'"+str(currency)+"'",":","to"+str(currency)+",")
elif choice == '1':
for currency, rates in data.items():
print(currency,':', rates)
elif choice == '2':
getData()
elif choice == '3':
run = False
# sys.quit()
else:
print(' : [MSG] : No such choice!')
choice = input('s: ')
def toUSD():
pass
def toJPY():
pass
def toBGN():
pass
def toCZK():
pass
def toDKK():
pass
def toGBP():
pass
def toHUF():
pass
def toPLN():
pass
def toRON():
pass
def toSEK():
pass
def toCHF():
pass
def toISK():
pass
def toNOK():
pass
getData()
menu()
答案 0 :(得分:0)
感谢user2357112对循环导入发表了评论。这有助于我弄清楚我做错了什么。
通过导入设置,导入设置又导入了main,然后将导致main重新运行,因此我们有一个循环导入导致错误。
我还没有足够的知识来解决该问题,因此我可以将OPTIONS保留在设置中,而仍然可以使用main中的功能。但是目前,我已经将OPTIONS保留在main.py中,以后可能会再次尝试学习如何操作。