英文:
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.
答案1
得分: 1
%%python
这一行被执行为一个 shell 命令,并导致初始错误。需要的是一种方法来退出 shell 并返回控制给单元格。使用 exit
会导致单元格完全退出。也许你可以像下面这样直接调用 python
。
%%shell
echo "hello from shell"
python -c 'print("hello from Python")'
这将产生以下输出:
hello from shell
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.
%%shell
echo "hello from shell"
python -c 'print("hello from Python")'
This gives the following.
hello from shell
hello from Python
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论