Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing terminal commands in Jupyter notebook

Tags:

I am trying to run the following in Jupyter notebook (with Python 2 if it makes a difference):

!head xyz.txt

and I get the following error:

'head' is not recognized as an internal or external command, operable program or batch file.

Is there anything I need to import to be able to do this?

like image 755
luckyfool Avatar asked Aug 01 '16 08:08

luckyfool


People also ask

Can you run command line in Jupyter Notebook?

You can run anything in your system shell with a terminal, including programs such as vim or emacs. The terminals run on the system where the Jupyter server is running, with the privileges of your user. Thus, if JupyterLab is installed on your local machine, the JupyterLab terminals will run there.

Can we run Linux commands in Jupyter Notebook?

Linux commands and exercises are designed to be executed from a Linux terminal. However, there are several different ways that most of this functionality can be achieved from the Jupyter notebooks with the attendent advantage of built-in documentation capabilities.


2 Answers

Might be useful for others. Use ! followed by terminal command you want to execute. To run a shell command. E.g.,

! pip install some_package

to install the some_package.

like image 72
Krishna Avatar answered Oct 03 '22 01:10

Krishna


An easier way to invoke terminal using jupyter-notebooks is to use magic function %%bash and use the jupyter cell as a terminal:

%%bash head xyz.txt pip install keras git add model.h5.dvc data.dvc metrics.json git commit -m "Second model, trained with 2000 images" 
like image 27
Kurian Benoy Avatar answered Oct 02 '22 23:10

Kurian Benoy