【问题标题】:Is there any way to install python packages through flask app?有没有办法通过flask app安装python包?
【发布时间】:2021-07-15 13:00:53
【问题描述】:

我有一个烧瓶应用程序,它使用 exec(script_name, globals()) 执行脚本,并使用 docker 在 Google Cloud Run 中运行。我所有的脚本都在谷歌云存储中。所以我使用 gcsfs 模块从 GCS 读取脚本并执行。

例如:

exec(gcs_file_system.open(<script_from_cloud>).read(), globals())

但我面临的问题是,每当要导入新包时,我需要首先使用 exec() 函数通过我的烧瓶应用程序安装该包。到目前为止,我已经尝试过使用

1. exec("os.system('pip install package_name')", globals())
2. exec("subprocess.check_call([sys.executable, '-m', 'pip', 'install', package_name])", globals())
3. import pip
   pip.main(['install', package_name])
4. import pip
   exec("pip.main(['install', package_name])", globals())
5. os.system('pip install package_name')
6. subprocess.check_call([sys.executable, '-m', 'pip', 'install', package_name])

所有这些都尝试在我调用的脚本 script.py 中执行

exec(gcs_file_system.open('bucket_name..../script.py').read())

每次我尝试其中任何一个时,我都会收到上游断开错误或脚本失败。我真的需要一些关于如何通过在云中运行的烧瓶应用程序安装包的帮助或建议(Google Cloud Run)。

【问题讨论】:

  • 它可以在您的工作站上运行吗?你试过你的容器了吗?
  • 它在我的本地机器上工作..但在容器中不起作用
  • 它在本地机器上的容器中不起作用,对吧?这并不奇怪,因为您将容器加载到内存中,使用 python 运行时,并且您可以即时更新 python 运行时。有些东西闻起来像是一个错误的想法/设计。
  • 我通过在单独的路由函数中给出 exec(os.system('pip install package'), globals()) 而不是在另一个 exec() 脚本中给出它来修复它。它有效。
  • 有趣。将其发布为答案!

标签: python google-cloud-platform google-cloud-run


【解决方案1】:

可以通过定义另一个仅用于安装包的路由函数来安装包。

这是通过在单独的路由函数中提供以下语句来完成的,而不是指定将包安装在路由函数内的另一个 exec(script) 函数中。

exec("os.system('pip install " + str(packages) + "')", globals())

服务器.py

@app.route('/add_package', methods=['GET', 'POST'])
def add_package():
    return render_template('add_package.html')


@app.route('/add_package_success', methods=['GET', 'POST'])
def add_package_success():
    code_content = request.form.get("code_editor", "").split("\n")
    for empties in range(code_content.count("")):
        code_content.remove("")
    code_content = [packages.strip().replace("\n", "") for packages in code_content]
    for packages in code_content:
        exec("os.system('pip install " + str(packages) + "')", globals())
    return render_template('add_package_success.html')

add_package.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Add Packages</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='css/codemirror.css') }}">
    <script type="text/javascript"
         src="{{ url_for('static', filename='codemirror.js') }}"></script>
     <script type="text/javascript"
         src="{{ url_for('static', filename='python.js') }}"></script>
    <style>
        .center {
            width: 15%;
            border: 2.5px solid red;
        }
        .header{
            font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
            font-weight: bold;
            position: relative;
            left: 10%;
        }
        .body_text{
            font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
        }
        .info_div{
            height: 180px;
            width: 50%;
            position: absolute;
            left: 50px;
            display: none;
            z-index:100;
        }
        .info{
            height: 15px;
            width: 15px;
            background-color: yellow;
            position: relative;
            left: 30px;
            border: solid red;
            text-align: center;
            font-weight bold;
            font-family: Arial, Helvetica, sans-serif;
        }
        .info:hover{
            cursor: help;
        }
        .info:hover + .info_div{
            display: block;
        }
        .submit_btn_2 {
            color: white;
            border: solid;
            position: relative;
            background-color: #003280;
            width: 170px;
            height: 30px;
        }
        .submit_btn_2:hover {
            background-color: #575558;
            cursor: pointer;
        }
    </style>
</head>
<body>
<form action="/add_package_success" method="post">
    <div class="center">
        <p class='header'>Add Packages</p><input class='info' value='?' readonly<br/><br/>
    </div><br/>
    <div>
        <a class="body_text" style="border: 2px #020d36 solid;color: #3a18a5;text-align: left;">Enter the Packages name (one in each line): </a><br/><br/>
        <textarea name="code_editor" id="code_editor"></textarea><br/><br/>
        <button type="submit">Add</button><br/><br/>
        <button formaction="/" style="left: 0px; top: 10px; width: 100px; height: 30px;" class = "submit_btn_2" type="submit"> Home </button>
    </div>
</form>
</body>
<script>
      var editor = CodeMirror.fromTextArea(document.getElementById("code_editor"), {
        mode: {name: "python",
               version: 3,
               singleLineStringErrors: false},
        lineNumbers: true,
        indentUnit: 4,
        matchBrackets: true
    });
    </script>
</html>

add_package_success.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Code Edited</title>
</head>
<body onload="load_function()">
</body>
<script>
    function load_function(){
        if(alert("Package Installed.\n\nPress OK to go HOME.")){
            window.location.href = "{{ url_for('index') }}";
        }
        window.location.href = "{{ url_for('index') }}";
    }
</script>
</html>

index.html

<form method='post' action='/'>
    <button class = "submit_btn_2" formaction="/add_package" id='add_package' style="position: absolute; top: 210px; left:1230px; height: 30px;" name="add_package">Install Packages</button>
</form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 2011-10-01
    相关资源
    最近更新 更多