使用烧瓶Restplus时烧瓶运行不起作用

时间:2019-11-19 11:13:46

标签: python python-3.x flask flask-restplus

我是python和flask的新手。我想使用flask-restplus。使用flask restplus时,无法使用“ flask run”命令启动我的应用程序。但是,当我将其作为模块“ python3 -m flask run”运行时,它确实会启动。我的第一个问题是,将其作为模块与正常方式启动有什么区别? 其次,在这种情况下,它不是以“烧瓶运行”开始的原因是什么。我在这里想念什么吗?

这是一个带有烧瓶restplus的简单示例应用程序:

2019-11-19 11:58:55.375 INFO [     parallel-3] i.a.m.websocket.MyWebSocketHandler  : websocket toJson 4.09
2019-11-19 11:58:55.375 INFO [     parallel-1] i.a.m.notifier.Notifier : notifier sendNotification 4.86
2019-11-19 11:58:57.366 INFO [     parallel-1] i.a.m.notifier.Notifier : notifier sendNotification 4.24
2019-11-19 11:58:57.374 INFO [     parallel-3] i.a.m.websocket.MyWebSocketHandler  : websocket toJson 4.11
2019-11-19 11:58:59.365 INFO [     parallel-1] i.a.m.notifier.Notifier : notifier sendNotification 4.61
2019-11-19 11:58:59.374 INFO [     parallel-3] i.a.m.websocket.MyWebSocketHandler  : websocket toJson 4.03
2019-11-19 11:59:01.365 INFO [     parallel-1] i.a.m.notifier.Notifier : notifier sendNotification 4.88
2019-11-19 11:59:01.375 INFO [     parallel-3] i.a.m.websocket.MyWebSocketHandler  : websocket toJson 4.29
2019-11-19 11:59:03.364 INFO [     parallel-1] i.a.m.notifier.Notifier : notifier sendNotification 4.37

以下是“ flask run”命令的输出:

from flask import Flask
from flask_restplus import Resource, Api

app = Flask(__name__)                  #  Create a Flask WSGI application
api = Api(app)                         #  Create a Flask-RESTPlus API

@api.route('/hello')                   #  Create a URL route to this resource
class HelloWorld(Resource):            #  Create a RESTful resource
    def get(self):                     #  Create GET endpoint
        return {'hello': 'world'}

if __name__ == '__main__':
    app.run()

以及使用“ python3 -m flask run”命令的输出:

(venv) ~/C/e/s/testpython ❯❯❯ flask run
 * Serving Flask app "app.py"
 * Environment: local
 * Debug mode: off
Usage: flask run [OPTIONS]

Error: While importing "app", an ImportError was raised:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 235, in locate_app
    __import__(module_name)
  File "/Users/rajatarora/Code/eon/sales-plus/testpython/app.py", line 2, in <module>
    from flask_restplus import Resource, Api
ModuleNotFoundError: No module named 'flask_restplus'
当然会安装

flask resplus。这是点冻结的输出

(venv) ~/C/e/s/testpython ❯❯❯ python3 -m flask run                                  ✘ 2
 * Serving Flask app "app.py"
 * Environment: local
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

2 个答案:

答案 0 :(得分:2)

未安装#pragma once #include <winrt/Windows.Foundation.Collections.h> #include <winrt/Windows.System.UserProfile.h> #include <iostream> #pragma comment(lib, "windowsapp") using namespace winrt; using namespace Windows::Foundation; using namespace Windows::System::UserProfile; int main() { winrt::init_apartment(); for (const auto& lang : GlobalizationPreferences::Languages()) { std::wcout << lang.c_str() << std::endl; } } 模块。您可以从virtualenv环境安装它,也可以在本地安装(不推荐,“但是您可以选择”)。

#include <roapi.h>
#include <wrl.h>
#include <Windows.System.UserProfile.h>
#include <iostream>
#include <stdint.h>
#pragma comment(lib, "runtimeobject.lib")

using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
using namespace ABI::Windows::Foundation::Collections;
using namespace ABI::Windows::System::UserProfile;

int main()
{
    RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
    if (FAILED(initialize)) {
        std::cerr << "RoInitialize failed" << std::endl;
        return 1;
    }

    ComPtr<IGlobalizationPreferencesStatics> gps;
    HRESULT hr = RoGetActivationFactory(
        HStringReference(
            RuntimeClass_Windows_System_UserProfile_GlobalizationPreferences)
            .Get(),
        IID_PPV_ARGS(&gps));
    if (FAILED(hr)) {
        std::cerr << "RoGetActivationFactory failed" << std::endl;
        return 1;
    }

    ComPtr<IVectorView<HSTRING>> langs;
    hr = gps->get_Languages(&langs);
    if (FAILED(hr)) {
        std::cerr << "Could not get Languages" << std::endl;
        return 1;
    }

    uint32_t size;
    hr = langs->get_Size(&size);
    if (FAILED(hr)) {
        std::cerr << "Could not get Size" << std::endl;
        return 1;
    }
    for (uint32_t i = 0; i < size; ++i) {
        HString lang;
        hr = langs->GetAt(i, lang.GetAddressOf());
        if (FAILED(hr)) {
            std::cerr << "Could not get Languages[" << i << "]" << std::endl;
            continue;
        }
        std::wcout << lang.GetRawBuffer(nullptr) << std::endl;
    }
}

包含有关flask-restplus的信息的链接是

pip install flask-restplus

# To see if module is installed
pip freeze

答案 1 :(得分:0)

在命令行上

:尝试在Windows:set FLASK_APP=app.py上运行,然后运行flask run命令。