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
  • 399
  • 1
  • 4
  • 23
  • 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

4 Answers4

3

"...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,401
  • 6
  • 10
  • depends on how you run your module. You can `python -m module` or `python module.py` – mr_mo Jul 01 '19 at 11:24
  • If you found my answer useful, please mark it so others can find it too. – mr_mo Jul 01 '19 at 11:25
  • Many Jupyter users finding this question and answer combination will be seeking `%run /path/to/myfile.py` that is highlighted among [the extensive listing on the page that Nau's answer links to](https://stackoverflow.com/a/30250285/8508004). The `%run` command has several features with how the output links to Jupyter and other abilities, such as the interactive `-i` flag running the script code in the current in the current notebook namsepace that make if very useful. – Wayne Apr 18 '23 at 14:48
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
1

Rather than opening a new Jupyter notebook and execute it in a cell, you can execute any kind of scripts with jupyterlab-executor

pip install jupyterlab-executor

It is an extension that you can run scripts from the jupyterlab file browser directly.

enter image description here

Gavin Chan
  • 55
  • 8