3

How can I run .py files from jupyter lab? I have spent my all coding life using jupyter notebook and jupyter lab but replication codes of research papers are mostly in .py file format

For instance, this is a github repository for beta variational autoencoder. As you can see from the repository, these kinds of repositories are usually comprised of main.py, model.py, which looks a lot different from .ipynb format that I usually use.

Can someone share how to comfortably run these kinds of .py codeson jupyter lab? I would appreciate it a lot if someone tells me a video or an article explaining how to run these .py codes on jupyter lab comfortably.

Eiffelbear
  • 341
  • 4
  • 16
  • Please check this : https://stackoverflow.com/questions/38648286/in-jupyter-lab-execute-editor-code-in-python-console – J.K Jun 30 '19 at 15:00

3 Answers3

2

"...how to run these .py codes on jupyter lab comfortably."

Basically the Jupyter's IPython interface allows you to do magic commands that commands within a shell.

Here is the magic for python subprocess.

You can use:

%python -m /path/to/myfile

Then execute the cell and the command will run in the cell and the output cell is the standard output for this run. You can also make this run in tmux or other tools to make it as a managed background job.

mr_mo
  • 1,191
  • 5
  • 9
2

There is a rather thorough discussion of how to interact with .py files from Jupyter notebook here: How to load/edit/run/save text files (.py) into an IPython notebook cell?

(No terminal window is needed.)

Nau
  • 61
  • 4
1

Find File-> new launcher -> other -> terminal, then you use command line run your python file, like "python xxx.py"

  • I am thinking of running codes on .ipynb so that I can still work in the jupyter-like environment. If i have to use terminal to run .py file, then I think it would be better to use something like PyCharm! – Eiffelbear Jul 01 '19 at 03:14
  • This answer does not seem to quite fit your needs @Eiffelbear, indeed, but it actually solved my lack of knowledge for this very developing environment. It reminds me of my first years of learning computer science at college, when we had to open a terminal alongside the text editor in order to execute the code after edition; so it suits me well. Thanks. – Olivier Feb 13 '21 at 11:21