无法让python导入位于不同文件夹中的nba_py

时间:2018-02-21 01:51:21

标签: python module python-import

我在这个项目中使用Python2.7和Google App Engine。我是一个巨大的NBA粉丝,我想使用nba_py api来检索要在我的网站上显示的其他信息,但似乎无法正确导入它。我想在我的blogfront.py中导入它但是当我刷新浏览器时出现错误。这是我的目录结构。

multi-user-blog
      - libraries
            - nba_py
                 -nba_py
                     - __init__.py
      - handlers
           -blogfront.py
           - __init__.py
      - mainblog.py

这是我的blogfront.py

from bloghandler import BlogHandler
from models.post import Post

from libraries.nba_py import nba_py


# Render Home Page
class BlogFront(BlogHandler):
    def get(self):
        # type: () -> object
        posts = Post.all().order('-created')
        title = "Blog/Scores"
        standing = get_standing()
        self.render('front.html', posts=posts, title=title)


    def get_standing():
        scoreboard = nba_py.Scoreboard()
        print scoreboard.west_conf_standings_by_day()

这是我的mainblog.py

import sys
import os
import re
import random
import hashlib
import hmac
import webapp2
import jinja2
import time


from handlers.bloghandler import BlogHandler
from handlers.blogfront import BlogFront
from handlers.deletecomment import DeleteComment
from handlers.deletepost import DeletePost
from handlers.likepost import Likes
from handlers.login import Login
from handlers.logout import Logout
from handlers.addcomment import AddComment
from handlers.newpost import NewPost
from handlers.postpage import PostPage
from handlers.signup import Signup
from handlers.signup import Register
from handlers.editcomment import EditComment
from handlers.editpost import EditPost


from models.comment import Comment
from models.user import User
from models.post import Post

from helpers import *

from string import letters
from google.appengine.ext import db


app = webapp2.WSGIApplication([('/', BlogFront),
                               ('/blog/?', BlogFront),
                               ('/postpage/([0-9]+)', PostPage),
                               ('/edit/([0-9]+)', EditPost),
                               ('/delete/([0-9]+)', DeletePost),
                               ('/addcomment/([0-9]+)', AddComment),
                               ('/blog/([0-9]+)/editcomment/([0-9]+)',
                                EditComment),
                               ('/blog/([0-9]+)/deletecomment/([0-9]+)',
                                DeleteComment),
                               ('/blog/like/([0-9]+)', Likes),
                               ('/blog/newpost', NewPost),
                               ('/signup', Register),
                               ('/login', Login),
                               ('/logout', Logout),
                               ],
                              debug=True)

错误是:ImportError:没有名为libraries.nba_py

的模块

3 个答案:

答案 0 :(得分:0)

您是如何安装nba_py的?

如果您使用pip安装它,则可以执行import nba_py。 您无需libraries结构。

答案 1 :(得分:0)

目录链中的每一步都有__init__.py吗?那总是让我

multi-user-blog
      - libraries
            - __init__.py       <-------
            - nba_py
                 - __init__.py  <-------
                 -nba_py
                     - __init__.py

答案 2 :(得分:0)

根据您的描述,您不清楚是否已按照https://cloud.google.com/appengine/docs/standard/python/tools/using-libraries-python-27中的建议并使用nba_pypip install -t安装到您的项目中,以及添加了appengine_config.py来执行vendor.add()。如果没有这些步骤,您的应用程序将会很难过。

另外,由于nba_py想要使用pandas,你也想要pip install -t它,并且由于pandas想要numpy,你在{{1}需要以下内容}}:

app.yaml