在python3.6中导入错误

时间:2018-02-20 20:17:57

标签: python

enter image description here

from flask import Flask, request
from flask_restful import Resource, Api
from flask_jwt import JWT, jwt_required
from .security import authenticate, identity


app = Flask(__name__)
app.secret_key = "jose"
api = Api(app)  # Can easily add resources to this "app"

jwt = JWT(app, authenticate, identity)

items = []

# every resource has to be a class
class Item(Resource):
    @jwt_required()
    def get(self, name):

        item = next(filter(lambda x: x["name"] == name, items), None)

        return {"item": item}, 200 if item else 404

    def post(self, name):

        if next(filter(lambda x: x["name"] == name, items), None) is not None:
            return {"Message": "An Item with the name {} already exit".format(name)}, 400

        data = request.get_json()

        item = {"name": name, "price": data["price"]}
        items.append(item)
        return item, 201 # 201 is status code for created


class ItemList(Resource):

    def get(self):
        return {"items": items}



api.add_resource(Item, "/item/<string:name>")
api.add_resource(ItemList, "/items")

app.run(port=5000, debug=True)


from werkzeug.security import safe_str_cmp
from .user import User

users = [
    User(1, "bob", "asdf")
]

username_mapping = {u.username: u for u in users}

userid_mapping = {u.id: u for u in users}


def authenticate(username, password):
    user = username_mapping.get(username, None)
    if user and safe_str_cmp(user.password, password):

        return user


def identity(payload):
    user_id = payload["identity"]
    return userid_mapping.get(user_id, None)

我正在尝试从security.py导入身份验证和身份到我的app.py.它们都在同一目录中。

linter没有给我任何错误但是当我运行app.py时它给了我这个错误:

  

ModuleNotFoundError:没有名为' main .security'的模块; ' main '不是包

我不确定这是从哪里来的。是不是在同一目录中识别出security.py存在?

1 个答案:

答案 0 :(得分:0)

问题似乎是python没有到达正确的文件夹。 我的代码位于名为 Code 的文件夹中,而Code位于我的根文件夹 Section4 中,我的根文件夹中也是venv文件夹。我最终将文件从 Code 移动到 Section4 。我运行了app.py,它开始顺利运行应用程序。