有没有办法通过flask应用程序安装python软件包?

有没有办法通过flask应用程序安装python软件包?,python,google-cloud-platform,google-cloud-run,Python,Google Cloud Platform,Google Cloud Run,我有一个flask应用程序,它使用exec(script_name,globals())执行脚本,并使用docker在Google Cloud中运行。我所有的脚本都在谷歌云存储中。所以我使用gcsfs模块从GCS读取脚本并执行 例如: exec(gcs_file_system.open(<script_from_cloud>).read(), globals()) 所有这些都尝试在script.py中执行,我使用 exec(gcs_file_system.open('bucket_

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

例如:

exec(gcs_file_system.open(<script_from_cloud>).read(), globals())
所有这些都尝试在script.py中执行,我使用

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

每次我尝试上述任何一种方法时,要么会出现上游断开连接错误,要么脚本就失败了。我真的需要一些帮助或建议,告诉我如何通过在云中运行的flask应用程序(Google cloud Run)安装软件包。

安装软件包可以通过定义另一个路由函数来完成,只需安装软件包

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

exec("os.system('pip install " + str(packages) + "')", globals())
Server.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>

添加包
.中心{
宽度:15%;
边框:2.5px纯红;
}
.标题{
字体系列:“Helvetica Neue”,Helvetica,Arial,无衬线;
字体大小:粗体;
位置:相对位置;
左:10%;
}
.正文{
字体系列:“Helvetica Neue”,Helvetica,Arial,无衬线;
}
.info_div{
高度:180像素;
宽度:50%;
位置:绝对位置;
左:50px;
显示:无;
z指数:100;
}
.info{
高度:15px;
宽度:15px;
背景颜色:黄色;
位置:相对位置;
左:30px;
边框:纯红;
文本对齐:居中;
字体粗体;
字体系列:Arial、Helvetica、无衬线字体;
}
.info:悬停{
光标:帮助;
}
.info:hover+.info\u div{
显示:块;
}
.提交{
颜色:白色;
边框:实心;
位置:相对位置;
背景色:#003280;
宽度:170px;
高度:30px;
}
.submit_btn_2:悬停{
背景色:#575558;
光标:指针;
}

添加软件包

在您的工作站上工作吗?你试过你的容器吗?它在我本地的机器上工作。。但是不能在容器中工作它不能在本地机器上的容器中工作,对吗?这并不奇怪,因为您在内存中加载了容器,使用python运行时,您可以动态更新python运行时。有些东西闻起来像是一个错误的想法/设计。我通过在单独的路由函数中提供exec(os.system('pip install package')、globals()来修复它,而不是在另一个exec()脚本中提供它。很有意思。将其作为答案发布!
<!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>
<!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>

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