繁体   English   中英

Python3中的导入错误

[英]An import mistake in Python3

好吧,我想运行一个从github克隆的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)

在__ INIT __.py service是:

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 python3 __init__.py时,它告诉我:

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

怎么了

test范围内,您无法看到该service

3个解决方案:

1-将service添加到您的PYTHONPATH变量

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

3-从testservice符号链接,即ln -s service ../service/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM