简体   繁体   English

如何将.py 文件解释为 jupyter 笔记本

[英]How to interpret .py files as jupyter notebooks

I am using an online jupyter notebook that is somehow configured to read all.py files as jupyter notebook files:我正在使用一个在线 jupyter notebook,它以某种方式配置为将 all.py 文件作为 jupyter notebook 文件读取: n

I am a big fan of this setup and would like to use it everywhere.我是这个设置的忠实粉丝,并且想在任何地方使用它。 On my own jupyter installation however, .py files are just interpreted as test files and are not by default loaded into jupyter cells.然而,在我自己的 jupyter 安装中,.py 文件仅被解释为测试文件,默认情况下不会加载到 jupyter 单元中。 How can I achieve the same configuration for my jupyter notebook?如何为我的 jupyter notebook 实现相同的配置?

What you're looking for is jupytext .您正在寻找的是jupytext You just need to install it into python env from which you're running your jupyter notebooks:您只需将其安装到 python 环境中即可运行 jupyter 笔记本:

pip install jupytext --upgrade

And you get this:你得到这个:
在此处输入图像描述

That's not exactly what you asked, but you can achieve something close to that by using the magic %load FILE.py in a new jupyter notebook.这不完全是您所要求的,但您可以通过在新的 jupyter 笔记本中使用魔法%load FILE.py来实现与此接近的目标。

%load FILE.py will copy the contents of FILE.py in the current cell upon executing it. %load FILE.py将在执行时将FILE.py的内容复制到当前单元格中。

You use the python code in your Jupyter Notebook by just pasting the whole code in a cell OR:您只需将整个代码粘贴到单元格中,即可在 Jupyter Notebook 中使用 python 代码,或者:

%load pythonfile.py to load code from a file (not necessarily.py files) into a jupyter notebook cell; %load pythonfile.py将文件(不一定是 .py 文件)中的代码加载到 jupyter notebook 单元中;

%run pythonfile.py in order to execute the file instead of loading it (outputs whatever that file outputs). %run pythonfile.py以执行文件而不是加载它(输出该文件输出的任何内容)。

Also, pythonfile.py should exist in the cd or you can use its full path.此外,pythonfile.py 应该存在于cd中,或者您可以使用其完整路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM