How to import ipython file in jupyter notebook - ipython

I have gone through various ways to import a ipython file in jupyter. I am new to it so i have no idea how to import the file.
Code
import SupplyDelay.ipynb as we do in python
Error:
ModuleNotFoundError Traceback (most recent call last)
in ()
3 import numpy as np
4 from sklearn.model_selection import train_test_split
----> 5 import SupplyDelay.ipynb
ModuleNotFoundError: No module named 'SupplyDelay'
File Hierarchy:
Folder abc:
SupplyDelay
Forecast
Forecast should import SupplyDelay

Have you tried using the %run magic designed to run a notebook from within a notebook? Use like so:
%run SupplyDelay.ipynb
If that doesn't work or had undesired side effects, you can also follow the instructions in the docs: http://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/Importing%20Notebooks.html

Here i have created two .ipynb file
1. Master.ipynb
2. Child.ipynb
The Master.ipynb looks something like this
Here i have defined few functions.
Now i have created the Child.ipynb in the same directory as Master.ipynb, the following snip shows how to call Master.ipynb from the Child.ipynb
Hope, this solves your problem.

Related

VSCode Jupyter project deletes lines of python code when saving the new .py file

(1) Create a new .py file
(2) Add code
(3) Ctrl + s
It deletes lines of python code in the file. Why?
import sys, numpy, pandas as pd, matplotlib, seaborn
import matplotlib.pyplot as pyplot
seaborn.set(style="darkgrid")
When ctrl+s, it deletes lines and only the following remain:
import seaborn
seaborn.set(style="darkgrid")

import ImageTk for google earth engine

I want to import ee.mapclient
but when I do this the following error is returned:
Traceback (most recent call last):
File "<ipython-input-16-5f312fd5c732>", line 1, in <module>
import ee.mapclient
File "C:\Users\Stefano\Anaconda2_2\lib\site-packages\ee\mapclient.py", line 43, in <module>
import ImageTk # pylint: disable=g-import-not-at-top
ImportError: No module named ImageTk
I am using anaconda and have installed pillow and PIL. Within my PIL folder in site-packages there is a file caled ImageTk.
When I do this:
from PIL import Image, ImageTk
everything imports fine, but for some reason ee.mapclient is not recognizing this.
A link to the python code used in mapclient.py can be found here:
https://github.com/google/earthengine-api/blob/master/python/ee/mapclient.py
Edit mapclient to put "from PIL import Image,ImageTk " in try block ,

Import a python file into another python file on pythonanywhere

I am trying to built a web app on pythonanywhere.
I have a file abc.py and another file xyz.py . In the file xyz.py I want to use a function of abc.py so how do I import abc.py into xyz.py given that they are in the same directory. What will be the import statement? I was trying:
import "absolute/path/to/the/file.py" which didnt work.
Also did
from . import abc.py in xyz.py which also didnt work
PythonAnywhere dev here: if they're in the same directory, then you need to use
from abc import functionname
This part of the official Python tutorial explains how import works.

Scipy and CX_freeze - Error importing scipy: you cannot import scipy while being in scipy source directory

I'm having trouble compiling an exe while using cx_freeze and scipy. In particular, my script uses
from scipy.interpolate import griddata
The build process seems to complete successfully, however when I try to run the compiled exe, I get the following message.
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
exec(code, m.__dict__)
File "gis_helper.py", line 13, in <module>
File "C:\Python27\lib\site-packages\scipy\__init__.py", line 103, in <module>
raise ImportError(msg)
ImportError: Error importing scipy: you cannot import scipy while
being in scipy source directory; please exit the scipy source
tree first, and relaunch your python intepreter.
After looking at scipy\ _init__.py file, there is the following:
if __SCIPY_SETUP__:
import sys as _sys
_sys.stderr.write('Running from scipy source directory.\n')
del _sys
else:
try:
from scipy.__config__ import show as show_config
except ImportError:
msg = """Error importing scipy: you cannot import scipy while
being in scipy source directory; please exit the scipy source
tree first, and relaunch your python intepreter."""
raise ImportError(msg)
I'm not entirely sure what is the problem here however although it seems that the erros is being thrown because there is a problem with the scipy config file. Possibly not being included in the build process. I'm quite a novice and hoping someone more experienced with generating build using cxfreeze can shed some light on this.
Incidentally, the scipy in use was installed from binaries here.
i have had the same issue. I added this code to the setup.py generated by cx_freeze:
import scipy
includefiles_list=[]
scipy_path = dirname(scipy.__file__)
includefiles_list.append(scipy_path)
Then, used includefiles_list as part of the build_exe parameter:
build_options = dict(packages=[], include_files=includefiles_list)
setup(name="foo", options=dict(build_exe=build_options))
I add the same problem and solved it using fepzzz method and including some missing packages:
additional_mods = ['numpy.matlib', 'multiprocessing.process']
includefiles = [(r'C:\Anaconda3\Lib\site-packages\scipy')]
setup(xxx, options={'build_exe': {'includes': additional_mods, 'include_files': includefiles}})
And using version 5.0.2 of cx-Freeze package, which solved an error when importing multiprocessing.process

Using ipython magics from inside python modules to be loaded in notebook

I am pretty new to ipython and still learning and liking its functionality. Trying to write a module with common tasks so users of a notebook can load my custom module and save typing common code.
Here is a sample mymodule.ipy:
import mymodule as mymod
mymod.func():
wd=%pwd
return wd
When I test it in the notebook:
import mymodule as my
---------------
ImportError Traceback (most recent call last)
<ipython-input-1-1036e4702cf4> in <module>()
----> 1 import mymodule as my
ImportError: No module named mymodule
But when I rename the module file as mymodule.py, the module load fails with error message denoting it cannot recognize the pwd magic:
File "mymodule.py", line 2
wd=%pwd
^
SyntaxError: invalid syntax
My question is how do I write a module with ipython magics and other ipython functionality in it which I can load from notebooks? I had the impression that I can put ipython code into a file with .ipy extension and can load it from ipython. Am I missing something?
"%run mymodule.ipy" as pointed in my comment serves my need for now. Posting it as answer to close this question. Feel free to post more if you have a "module" approach.