有什么方法可以通过 flask app 安装 python 包吗?
Is there any way to install python packages through flask app?
我有一个 flask 应用程序,它使用 exec(script_name, globals()) 执行脚本,并且 运行 在 Google Cloud 运行 中使用 docker.我所有的脚本都在 Google 云存储中。所以我使用 gcsfs 模块从 GCS 读取脚本并执行。
例如:
exec(gcs_file_system.open(<script_from_cloud>).read(), globals())
但我面临的问题是,每当有新包要导入时,我需要先使用 exec() 函数通过我的 flask 应用程序安装该包。到目前为止,我已经尝试使用
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 云 运行)。
可以通过定义另一个仅用于安装包的路由函数来安装包。
这是通过在单独的路由函数中提供以下语句而不是指定在路由函数内的另一个 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>
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>
我有一个 flask 应用程序,它使用 exec(script_name, globals()) 执行脚本,并且 运行 在 Google Cloud 运行 中使用 docker.我所有的脚本都在 Google 云存储中。所以我使用 gcsfs 模块从 GCS 读取脚本并执行。
例如:
exec(gcs_file_system.open(<script_from_cloud>).read(), globals())
但我面临的问题是,每当有新包要导入时,我需要先使用 exec() 函数通过我的 flask 应用程序安装该包。到目前为止,我已经尝试使用
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 云 运行)。
可以通过定义另一个仅用于安装包的路由函数来安装包。
这是通过在单独的路由函数中提供以下语句而不是指定在路由函数内的另一个 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>
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>