2017-05-22 57 views
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' 

什么是错误?

+1

您忘记顶级'__init __。py'了吗? –

+0

尝试在[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

回答

0

test无法看到service

3解决方案:

1 - 添加service到您的PYTHONPATH变量

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

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