Google Colab:在%%shell之后使用%%python出现CalledProcessError

huangapple go评论182阅读模式
英文:

Google Colab: CalledProcessError with %%python after %%shell

问题

我在Google Colab中使用%shell启动了一个单元格来运行一些Shell代码,然后尝试通过%%python切换回来运行Python代码,但无论我尝试运行什么代码,我总是在%%python之前的最后一个Shell命令处收到CalledProcessError错误,如何解决这个问题?我需要它们在同一个单元格中。

英文:

I started a Google Colab cell with %shell to run some shell code, then tried to run python code by switching back with %%python, but no matter what code I tried, I always get CalledProcessError for the last shell command before %%python, how to solve this? I need them to be in the same cell.Google Colab:在%%shell之后使用%%python出现CalledProcessError

答案1

得分: 1

%%python 这一行被执行为一个 shell 命令,并导致初始错误。需要的是一种方法来退出 shell 并返回控制给单元格。使用 exit 会导致单元格完全退出。也许你可以像下面这样直接调用 python

  1. %%shell
  2. echo "hello from shell"
  3. python -c 'print("hello from Python")'

这将产生以下输出:

  1. hello from shell
  2. hello from Python
英文:

The %%python line is being executed as a shell command and leads to the initial error. What's needed is a way of logging out of the shell and returning control to the cell. Using exit causes the cell to exit completely. Perhaps you could call python directly as shown below.

  1. %%shell
  2. echo "hello from shell"
  3. python -c 'print("hello from Python")'

This gives the following.

  1. hello from shell
  2. hello from Python

huangapple
  • 本文由 发表于 2023年3月12日 13:15:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定