0
嗯,我想运行一个从github克隆的python3程序,但是我遇到了一些关于导入的问题。 它的结构是:Python3导入错误
$ tree .
.
├── readme.md
├── requirements.txt
├── service
│ ├── api.py
│ ├── decorator.py
│ ├── __init__.py
│ └── spider.py
├── tests
│ ├── __init__.py
│ └── test_grade_api.py
└── wsgi.py
在tests
的__ INIT __.py在于:
from service import app
from .test_grade_api import test_grade_api
if __name__ == '__main__':
test_grade_api(app)
在service
的__ INIT __.py在于:
import base64
import asyncio
from aiohttp import web
from aiohttp_session import setup, get_session, session_middleware
from aiohttp_session.cookie_storage import EncryptedCookieStorage
from cryptography import fernet
def create_app():
app = web.Application()
fernet_key = fernet.Fernet.generate_key()
secret_key = base64.urlsafe_b64decode(fernet_key)
# ====== app set ======
setup(app, EncryptedCookieStorage(secret_key))
# =====================
# ====== url map ======
# =====================
# ====== sub app ======
from .api import api
app.add_subapp('/api/', api)
# =====================
return app
app = create_app()
loop = asyncio.get_event_loop()
但是当我在test
,它告诉我:
Traceback (most recent call last):
File "__init__.py", line 1, in <module>
from service import app
ImportError: No module named 'service'
什么是错误?
您忘记顶级'__init __。py'了吗? –
尝试在[Python路径](https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH)中添加顶级目录(包含'service'和'test'的目录)(另请参阅[sys.path](https://docs.python.org/3/library/sys.html#sys.path))。 – Tryph