在 Google Cloud Compute Engine 上运行 python 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47455680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:13:04  来源:igfitidea点击:

Running a python script on Google Cloud Compute Engine

pythongoogle-cloud-platform

提问by jorism1993

For a machine learning task at school I wrote my own MLP network. The data set is quite big, and training takes forever. I was alerted to the option of running my script on the Google Cloud Compute Engine. I tried to set this up, but did not succeed (yet).

对于学校的机器学习任务,我编写了自己的 MLP 网络。数据集相当大,训练需要很长时间。我被告知可以在 Google Cloud Compute Engine 上运行我的脚本的选项。我试图设置它,但没有成功(还)。

The steps I undertook where:

我采取的步骤:

  1. Create an account
  2. Create a VM
  3. Open the VM via the browser
  1. 创建一个帐户
  2. 创建虚拟机
  3. 通过浏览器打开虚拟机

Can anyone help me with importing and running my python script into the Google Cloud. Or does anyone have clear a tutorial on how to solve this? I tried finding these myself, but had no success so far.

任何人都可以帮助我将我的 python 脚本导入并运行到 Google Cloud 中。或者有没有人有关于如何解决这个问题的明确教程?我试图自己找到这些,但到目前为止没有成功。

回答by EsotericVoid

I finally figured this out so I'll post the same answer on my own post that worked for me here. Using Debian Stretch on my VM. I'm assuming you already uploaded your file(s) to the VM and that you are in the same directory of your script.

我终于想通了,所以我会在我自己的帖子上发布相同的答案,这里对我有用。在我的 VM 上使用 Debian Stretch。我假设您已经将文件上传到 VM,并且您位于脚本的同一目录中。

  1. Make sure to add the following in your Python File as the first line to make it executable
  1. 确保在您的 Python 文件中添加以下内容作为第一行以使其可执行

' #!/usr/bin/python3 '

' #!/usr/bin/python3 '

  1. Make your script an executable

    chmod +x myscript.py
    
  2. Run the nohupcommand to execute the script in the background. The &option ensures that the process stays alive after exiting. I've added the shebang line to my python script so there's no need to call pythonhere

    nohup /path/to/script/myscript.py &
    
  3. Logout from the shell if you want

    logout
    
  1. 使您的脚本成为可执行文件

    chmod +x myscript.py
    
  2. 运行nohup命令在后台执行脚本。该&选项可确保进程在退出后保持活动状态。我已将shebang行添加到我的python脚本中,因此无需在python此处调用

    nohup /path/to/script/myscript.py &
    
  3. 如果需要,请从 shell 注销

    logout
    

Done! Now your script is up and running. You can login back and make sure that your process is still alive by checking the output of this command:

完毕!现在您的脚本已启动并正在运行。您可以通过检查以下命令的输出重新登录并确保您的进程仍然存在:

ps -e | grep myscript.py

If anything went wrong, you can check out the nohup.outfile to see the output of your script:

如果出现任何问题,您可以查看nohup.out文件以查看脚本的输出:

cat nohup.out

回答by codeslord

You can use Google Cloud Platform tutorials itself and is very simple to follow. Links are given below

您可以使用 Google Cloud Platform 教程本身,并且非常易于遵循。链接如下

Setting up Python

设置 Python

https://cloud.google.com/python/setup

https://cloud.google.com/python/setup

Getting started

入门

https://cloud.google.com/python/getting-started/hello-world

https://cloud.google.com/python/getting-started/hello-world

Please note that you don't have any free tier to run Python 3.x, Standard environment with free tier only supports Python 2.x.

请注意,您没有任何免费层来运行 Python 3.x,具有免费层的标准环境仅支持 Python 2.x。

Edit: As per the latest update Python 3.x is default in standard environment

编辑:根据最新更新 Python 3.x 是标准环境中的默认值

回答by yasir khatri

Just navigate to the directory where the script is placed.

只需导航到放置脚本的目录即可。

python thenameofscript.py

回答by Oded BD

I used Way Scriptwhich is great and have a free plan to run every hour

我使用了很棒的Way Script并且有一个每小时运行一次的免费计划

You can check this videoto see explanation

您可以查看此视频以查看说明