Python3中的导入错误

时间:2017-05-22 12:36:13

标签: python python-3.x

好吧,我想运行一个从github克隆的python3程序,但我遇到了一些关于import的问题。 它的结构是:

$ 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()

但是当我在python3 __init__.pytest时,它会告诉我:

Traceback (most recent call last):
  File "__init__.py", line 1, in <module>
    from service import app
ImportError: No module named 'service'

错误是什么?

1 个答案:

答案 0 :(得分:0)

您无法在service内看到test

3个解决方案:

1 - 将service添加到PYTHONPATH变量

2 - 执行根目录中的代码(即cd test/;cd ../

3 - 来自service的{​​{1}}的符号链接,即test