2017-05-22 4 views
0

Nun, ich möchte ein Python3-Programm ausführen, das von Github geklont wurde, aber ich habe eine Frage zum Import. Die Struktur davon ist, dass:Ein Importfehler in Python3

$ tree . 
. 
├── readme.md 
├── requirements.txt 
├── service 
│   ├── api.py 
│   ├── decorator.py 
│   ├── __init__.py 
│   └── spider.py 
├── tests 
│   ├── __init__.py 
│   └── test_grade_api.py 
└── wsgi.py 

Die __ init __.py in tests das heißt:

from service import app 
from .test_grade_api import test_grade_api 

if __name__ == '__main__': 
    test_grade_api(app) 

Die __ init __.py in service ist, dass:

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

Aber wenn ich python3 __init__.py in test, Es sagt mir, dass:

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

Was ist der Fehler?

+1

Haben Sie die oberste Ebene "__init __. Py" vergessen? –

+0

Versuchen Sie, Ihr Top-Level-Verzeichnis (das mit 'service' und' test') im [Python-Pfad] (https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH) hinzuzufügen (siehe auch [sys.path] (https://docs.python.org/3/library/sys.html#sys.path)). – Tryph

Antwort

0

Sie haben keine Sichtbarkeit der service von innerhalb test.

3 Lösungen:

1 - Hinzufügen service auf Ihrem PYTHONPATH Variable

2 - Code aus dem Stammverzeichnis ausführen (dh cd test/;cd ../)

3 - Symbolischer Link zum service von test, dh ln -s service ../service/