gpt4 book ai didi

python - 尽管有 __init__.py,但在非包错误中尝试相对导入

转载 作者:太空狗 更新时间:2023-10-30 00:56:22 25 4
gpt4 key购买 nike

我有一个包 cclogger。该目录有一个 __init__.py 文件,其中包含一些用于加载配置的代码。当我尝试使用以下命令在该目录中运行文件 api_main.py 时...

python -m cclogger.api_main

我得到以下错误:-

config loaded
Instantiating DB with: cclogger/test123@localhost:x
Instantiated ParseCentral
Register parser called by : CitiIndia
Registered parser for email: CitiAlert.India@citicorp.com
Instantiated SmsParseCentral
Register parser called by : Citi Bank
Registered sms parser for address: lm-citibk
Register parser called by : HDFC Bank
Registered sms parser for address: am-hdfcbk
Traceback (most recent call last):
File "/Users/applegrew/Dropbox/Credit Expense/cclogger/cclogger/api_main.py", line 4, in <module>
from .bottle import run, default_app, debug, get
ValueError: Attempted relative import in non-package

错误上方显示的消息来自同一包中的模块,这些模块由 __init__.py 导入。

api_main.py 中的代码是:-

import re
import os

from .bottle import run, default_app, debug, get
from .common_util import date_str_to_datetime, UTCOffset, date_filter

#app = Bottle()

default_app().router.add_filter('date', date_filter)

from . import api, dev

@get('/index')
def index():
return "CCLogger API main live and kicking."

if dev:
debug(True)
run(reloader=True, port=9000)
else:
os.chdir(os.path.dirname(__file__))
application = default_app()

我有 python 2.7.1。

我做错了什么?您可以在 https://github.com/applegrew/cclogger/tree/master/cclogger 查看完整代码

最佳答案

你不能直接将 python 模块作为脚本运行(我真的不知道为什么)。

编辑:原因在PEP338中解释。这是 "-m" 选项的规范。

The release of 2.5b1 showed a surprising (although obvious in retrospect) interaction between this PEP and PEP 328 - explicit relative imports don't work from a main module. This is due to the fact that relative imports rely on __name__ to determine the current module's position in the package hierarchy. In a main module, the value of __name__ is always __main__ , so explicit relative imports will always fail (as they only work for a module inside a package).

For the 2.5 release, the recommendation is to always use absolute imports in any module that is intended to be used as a main module

要测试您的应用程序,请将 api_main 封装在一个函数中并创建一个将运行主循环的顶层 main.py 文件:

cclogger/api_main.py :

import re
import os


from .bottle import run, default_app, debug, get
from .common_util import date_str_to_datetime, UTCOffset, date_filter

#app = Bottle()


def main():
default_app().router.add_filter('date', date_filter)

from . import api, dev

@get('/index')
def index():
return "CCLogger API main live and kicking."

if dev:
debug(True)
run(reloader=True, port=9000)
else:
os.chdir(os.path.dirname(__file__))
application = default_app()

/main.py:

from cclogger import api_main


if __name__ == '__main__':
api_main.main()

您可以通过键入 python main.pypython -m mainpython -c "import cclogger.api_main; api_main.main( )” .

PS:感谢您链接完整的源代码,它总是比随问题提供的 stub 更有帮助。

关于python - 尽管有 __init__.py,但在非包错误中尝试相对导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18888198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com