I'm using imported methods from a .py file in a jupyter notebook and I wish to edit the .py file and use the updated function in the notebook. Currently, when I change a function or class in the .py file I have to close this file in VSCode and restart the notebook kernel to use the methods with the change I just made in the .py file, as if VSCode uses a cached version of the .py file if I don't restart the jupyter kernel. Is there anyway to change this setting? I used to work like this in PyCharm and it worked fine.
You need to enable the auto reload magic in IPython. Try running the following before your code:
from IPython import get_ipython
ip = get_ipython()
ip.magic("reload_ext autoreload") # these will enable module autoreloading
ip.magic("autoreload 2")
In jupyter notebook, because the way of parsing files is based on JSON, the file format saved by default is not .py is .ipynb。 And .ipynb files cannot be simply imported to .py or .ipynb file, which brings great inconvenience to the development. Because in Jupiter notebook, it must be in the default .ipynb can support a series of features, such as automatic completion, console waiting, and so on .py files can only be modified through a text editor, which is very inconvenient.
Because .ipynb can import .py module, so one of the solutions is to convert the .ipynb module to .py file. Create a new cell at the end of the .ipynb file. Adding the following codes:
try:
!jupyter nbconvert --to python file_name.ipynb
except:
pass
# file_ name.ipynb is the file name of the current module
Then a file with the same name will be generated in the current .py file, this module can be used in other .ipynb.
Related
I know that .py is a regular python file. Also, .ipynb is a python notebook that contains the notebook code, and IPython is supposed to be an interactive shell for Python.
But when I want to write a Jupiter notebook code and run it in VSC, what extension should I use for my code file? .ipython or .ipynb?
It should be .ipynb. Python extension does not support .ipython file, and I could not find an extension that supports this file extension.
I am trying to bring in a .py file that I have created and hosted in my Jupyter environment to a notebook. I have tried the following method, but it gives me an error.
import sys
sys.path.insert(0, '/path/to/file.py')
import file.py
I have several functions saved in this file that I want to be able to call, but when I run the above I get an error that the file.py was not able to be loaded in. Is there a special way within notebooks to load in created .py files?
We ran some regression models in python .py and .ipynb, however, we'd like to do convert this to .do file for users who only use Stata.
Is there a way to do this?
For the notebook file, this can be programmatically achieved with nbconvert, specifically by developing an Exporter that ports or closes the notebook source to Stata source. Alternatively, nbconvert has existing exporters that can convert a ipynb file to a .py file, whereby PyStata maybe can be used.
I have written some Jupyter notebooks using original Jupyter notebook web interface. All notebooks are synced nicely in this way.
But now, I would like to edit my notebooks in the VSCode. But I cannot configure syncing notebook file with its python script.
I tried this using jupytext:
created file jupytext in the folder ~/.config
put the next code into this file:
# Always pair ipynb notebooks to py:percent files
default_jupytext_formats = "ipynb,py:percent"
But no effect!
(Update) Can this be achieved, as a first solution, using VSCode Tasks (I am not used tasks yet)?
May be it possible to run the task with jupytext command if the notebook file is opened/saved/modified?
Currently, VSCode does not support such a function. The Jupyter function in VSCode is provided by a Python extension, which supports us to convert between .ipynb files and .py files in VSCode.
.ipynb files to .py files : Export as python script.
.py files to .ipynb files : Right click, "Export Current Python File as Jupyter Notebook"
I have submitted the requirement you described, and we look forward to the realization of this feature. Giuhub link: How to synchronize the jupyter file and python file of VSCode.
Every time when I try to run a file in the JupiterLab console I get the following message:
ERROR:root:File 'thing.py' not found.
In this case, my file is called thing.py and I try to run it with the trivial run thing.py command in the console. The code is running and it gives me correct results when executed in the console, but I wanted to have it saved, so I put it in a JupiterLab text file and changed the extension to .py instead of .txt. But I get the aforementioned message regardless of which file I try to run. I am new to JupiterLab and admit that I might have missed something important. Every help is much appreciated.
If you're running Jupyterlab you should be able:
to create a new file & paste in your commands
Rename that file to "thing.py"
And then open a console in the same Jupyterlab instance and run that file. Notice that you can see "thing.py" in the file explorer on the left:
Alternatively, you can use the %load magic command in a notebook to dynamically load the code into a notebook's cell.
You might want to understand exactly what a Jupyter Lab file is, and what a Jupyter Lab file is not. The Jupyter Notebooks have the extension, .ipynb.
So anyway, the Jupyter Notebooks are not saved or formatted with python extensions. There are no Jupyter Notebooks or Jupyter Labs ending with the .py extension. That means Jupyter will not recognize an extension having .py, or .txt or .R or etc.... Jupyter opens, reads, and saves files having the .ipynb extension.
Jupyter Notebooks are an open document format based on JSON.
Jupyter can export in a number of different formats. Under the File tab, is the Export feature. The last time I looked there were about 20 different export formats. But there isn't a python or .py export format. A Jupyter file can also be Downloaded. Also under the File tab is the Download feature. This will download a standard text formatted JSON file. JSON files are mostly unreadable unless you've spent years coding JSON.
So there's not much purpose in downloading the Jupyter file unless you are working on a remote server and cannot save your work at that site. And it makes much more sense to save and copy the Jupyter file in its native Jupyter format - that means having the extension, .ipynb . Then just open and use that file on another PC.
Hopefully this should clarify why Jupyter won't open any .py or .txt files.