尝试在 python 个文件之间导入时出错
Error trying to import between python files
我在 Python 中构建了一个基本的解析器应用程序。我监视一个文件夹并在将文件放在那里时导入文件。我有一个 MongoDB,我正在尝试将导入保存到其中。几乎没有什么。当我尝试包含我的 class/mongo-document 文件之一时,就会出现问题。我确定这是一个我不明白的简单语法问题。我已经安装了我所有的要求,并且我在虚拟环境中 运行 这个。不过,这是我的第一个 python 应用程序,所以我可能没有看到它。
我的文件结构是
application.py
requirements.txt
__init__.py
-services
parser.py
__init__.py
-models
hl7message.py
__init__.py
这里是application.py
from mongoengine import connect
import os, os.path, time
from services import parser
db = connect('testdb')
dr = 'C:\Imports\Processed'
def processimports():
while True:
files = os.listdir(dr)
print(str(len(files)) + ' files found')
for f in files:
msg = open(dr + '\' + f).read().replace('\n', '\r')
parser.parse_message(msg)
print('waiting')
time.sleep(10)
processimports()
requirements.txt
mongoengine
hl7
parser.py
import hl7
from models import hl7message
def parse_message(message):
m = hl7.parse(str(message))
h = hl7message()
hl7message.py
from utilities import common
from application import db
import mongoengine
class Hl7message(db.Document):
message_type = db.StringField(db_field="m_typ")
created = db.IntField(db_field="cr")
message = db.StringField(db_field="m")
如果我不在 parser.py 中包含 hl7message class 它运行正常,但是一旦我包含它我就会收到错误,所以我确定它有一些东西处理那个文件。错误消息虽然没有帮助。我不知道我是否陷入了某种包含循环或其他问题。
抱歉,堆栈跟踪如下
Traceback (most recent call last):
File "C:/OneDrive/Dev/3/Importer/application.py", line 3, in <module>
from services import parser
File "C:\OneDrive\Dev\Importer\services\parser.py", line 2, in <module>
from models import hl7message
File "C:\OneDrive\Dev\Importer\models\hl7message.py", line 2, in <module>
from application import db
File "C:\OneDrive\Dev\Importer\application.py", line 23, in <module>
processimports()
File "C:\OneDrive\Dev\Importer\application.py", line 17, in processimports
parser.parse_message(msg)
AttributeError: module 'services.parser' has no attribute 'parse_message'
这是一个循环导入问题。 Application.py 导入解析器,导入 h17,导入 h17message,导入应用程序,其中 运行s processimports 在解析器模块的整个代码之前导入 运行。
在我看来,服务模块不应该导入应用程序。您可以创建一个包含行 db = connect('testdb')
的新模块 common.py 并在 application.py 和 h17message.
中从 common 导入数据库
我在 Python 中构建了一个基本的解析器应用程序。我监视一个文件夹并在将文件放在那里时导入文件。我有一个 MongoDB,我正在尝试将导入保存到其中。几乎没有什么。当我尝试包含我的 class/mongo-document 文件之一时,就会出现问题。我确定这是一个我不明白的简单语法问题。我已经安装了我所有的要求,并且我在虚拟环境中 运行 这个。不过,这是我的第一个 python 应用程序,所以我可能没有看到它。
我的文件结构是
application.py
requirements.txt
__init__.py
-services
parser.py
__init__.py
-models
hl7message.py
__init__.py
这里是application.py
from mongoengine import connect
import os, os.path, time
from services import parser
db = connect('testdb')
dr = 'C:\Imports\Processed'
def processimports():
while True:
files = os.listdir(dr)
print(str(len(files)) + ' files found')
for f in files:
msg = open(dr + '\' + f).read().replace('\n', '\r')
parser.parse_message(msg)
print('waiting')
time.sleep(10)
processimports()
requirements.txt
mongoengine
hl7
parser.py
import hl7
from models import hl7message
def parse_message(message):
m = hl7.parse(str(message))
h = hl7message()
hl7message.py
from utilities import common
from application import db
import mongoengine
class Hl7message(db.Document):
message_type = db.StringField(db_field="m_typ")
created = db.IntField(db_field="cr")
message = db.StringField(db_field="m")
如果我不在 parser.py 中包含 hl7message class 它运行正常,但是一旦我包含它我就会收到错误,所以我确定它有一些东西处理那个文件。错误消息虽然没有帮助。我不知道我是否陷入了某种包含循环或其他问题。
抱歉,堆栈跟踪如下
Traceback (most recent call last):
File "C:/OneDrive/Dev/3/Importer/application.py", line 3, in <module>
from services import parser
File "C:\OneDrive\Dev\Importer\services\parser.py", line 2, in <module>
from models import hl7message
File "C:\OneDrive\Dev\Importer\models\hl7message.py", line 2, in <module>
from application import db
File "C:\OneDrive\Dev\Importer\application.py", line 23, in <module>
processimports()
File "C:\OneDrive\Dev\Importer\application.py", line 17, in processimports
parser.parse_message(msg)
AttributeError: module 'services.parser' has no attribute 'parse_message'
这是一个循环导入问题。 Application.py 导入解析器,导入 h17,导入 h17message,导入应用程序,其中 运行s processimports 在解析器模块的整个代码之前导入 运行。
在我看来,服务模块不应该导入应用程序。您可以创建一个包含行 db = connect('testdb')
的新模块 common.py 并在 application.py 和 h17message.