Import Self Created Python Modules to Jupyter Notebook - python

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?

Related

How to make VSCode don't cache imported methods?

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.

Python sys.path.append

I am doing some multiprocessing work on python which isn't doable from a jupyter notebook. I have written the required functions in a .py file and I'm now importing it into my notebook.
How do I import this directly from the same folder without specifying the directory of where the file is in? This needs to be run-able on different PCs when sent to other people and thus I dont want to append the system path for my computer specifically.
import sys
#sys.path.append to get
sys.path.append()
from map_reducer import *
Thank you.
You can import them with:
from FileNameWithOutTheDotPy import *
And then just run them like normal:
FunctionThatDoesStuff(var1,var2)
Note: They do need to be in the same directory for this to work.

ModuleNotFoundError when working with multiple folders

I got a question/problem related to how to use init.py file for a project im working on. Im trying to run train_pipeline.py, which is located at
packages/cnn_modality_clf/cnn_modality_clf/train_pipeline.py
This file uses scripts that are located in the same folder, and also files that are within folders.
My imports (within train_pipeline.py
from cnn_modality_clf.pipeline import pipe
from cnn_modality_clf.config import config
from cnn_modality_clf.preprocessing import data_management as dm
from cnn_modality_clf.preprocessing import preprocessors as pp
However, when I run this train_pipeline file, I get a ModuleNotFoundError: No module named cnn_modality_clf and the error is on from cnn_modality_clf.pipeline import pipe
I have worked on previous projects (not that many) where using the init.py file will solved this issue. However, I have added this file into every folder. But that doesnt seem to work out.
My project structure looks like this:
I want to be able to use the python files that are within the same path, or that are within a folder (cnn.modality_clf.pipeline import pipe)
*Im using anaconda environment, and im using python 3.7.0
Any suggestions on how to overcome this problem?
Thanks in advance!

How to create modules in Jupyter notebook and import them? Python

I've created multiple python modules as .py files in a Python IDE called Pyzo in the following path:
'C:\Users\Michael\Anaconda3\Lib\site-packages' which I can then import like regular Python packages such as pandas and numpy into my Jupyter notebook or into Pyzo.
I'm a bit lost as to how to create a module in Jupyter notebook, containing a class with say a simple function, which I can then save and import into a new Jupyter notebook file.
The examples in this link below I found extremely vague and overly complicated. Any simpler examples would help, thanks!
http://nbviewer.jupyter.org/github/ipython/ipython/blob/master/examples/IPython%20Kernel/Importing%20Notebooks.ipynb
%run ./module_code.ipynb
keep this in import section- replace module_code with your file name and then you can access functions inside that file from the new notebook.
Suppose you want to import the contents of A.ipynb into B.ipynb.
Installation
pip install import-ipynb
How to use
Place both ipynb files in the same directory. Then, in the B.ipynb:
import import_ipynb
import A
Congratulations! You can now run any functions defined in A.ipynb from B.ipynb!
The easiest way to import user created modules on JupyterLab is to Export Notebook as an executable script from File menu as in screenshot. This will download .py file to your pc or mac. Just drag that downloaded file to your jupyterlab to import.
download notebook as .py file
Now you can import and use that module in other notebooks.
import example

How do I import module in jupyter notebook directory into notebooks in lower directories? [duplicate]

This question already has answers here:
Import local function from a module housed in another directory with relative imports in Jupyter Notebook using Python 3
(13 answers)
Closed 5 years ago.
I have used jupyter notebook for data analysis for quite sometime. I would like to develop a module in my jupyter notebook directory and be able to import that new module into notebooks. My jupyter notebook file directory can be represented as follows;
Jupyter notebooks\
notebook1.ipynb
new_module\
__init__.py
newfunction.py
currentnotebooks\
notebook2.ipynb
When use import new_module in notebook1.ipynb it works however when I try the same command in notebook2.ipynb I get the following ImportError: No module named 'new_module'. The two obvious solutions are A) move new_module into the currentnotebooks directory or B) move notebook2.ipynb up to the same level as new_module. I don't want to mess around with the file structure at all. Is this possible?
You need to make sure that the parent directory of new_module is on your python path. For a notebook that is one level below new_module, this code will do the trick:
import os
import sys
nb_dir = os.path.split(os.getcwd())[0]
if nb_dir not in sys.path:
sys.path.append(nb_dir)
If you're further down in the directory hierarchy, you will need to adjust the way nb_dir is set, but that's all. You should not run this code for a notebook in Jupyter notebooks, since it would add the parent of that directory to the python path, which is probably undesirable.
The reason the import works for notebook1 is that sys.path contains '' (the empty string), which is aways the current directory of the running interpreter (kernel, in this case). A google search for explain python path turns up several good explanations of how python uses PYTHONPATH (aka sys.path).

Categories

Resources