How can I get Jupyter Notebook to import a python file? - python-3.x

I have a very simple .py file, displayD3caller.py:
def caller():
print("Here is a print statement from displayD3caller.py")
I can import and use the function defined in other files, and from the python shell. However, when I try to import the function in a Jupyter Notebook, it can't find the module:
ImportError: No module named 'displayD3caller'
What can I do to fix this?

Before Jupyter Notebook uses any change to the source code of related files, the kernal needs to be restarted. This includes creating a new file.
If you make a new module after Jupyter is already running, you need to restart the kernal before it will find it. From the top menu, select Kernal > Restart. You will need to re-execute any cells whose outputs you depend on.

Related

Use variables from a custom name script file provided by user

I'm currently building an user friendly program in Python. Currently, the user is able to modify the input values provided in an init script that we can call init.py. At this moment the user can open in Spyder, the main.py and run/execute the whole process or just typing the classical command:
python3 main.py
The main.py file import all the variables needed from the init.py and run normally. What I would like to do now is to add a feature which allows the user to change the name of the init.py file. For example to be able to build initcustom.py.
And use the following command :
python3 main.py initcustom.py
How can I be able to import variables in main.py from a script which can change name (that should be provided by the user in the command line)?
And in the case where nothing is specified we keep the classical init.py
What such feature will induce as changes in the case where someone just want to do F5 using Spyder without precising input names?
Thank you in advance for your help
You can probably do something with exec
import sys
exec(‘import’+’sys.argv[2]’)

Jupyter does not see changes in the imported module

I have a file called "Notebook.ipynb". I want to import my own module into it patterns.py. Let's say in patterns.py I have a variable a=1. If I print it in Jupyter after importing it, I expect to get 1. But if I change the value of the variable to patterns.py Jupyter will continue to think that the variable a is equal to one. At the same time, of course, I restart the cell in which the variable is imported from patterns.py
What do I need to do to make Jupyter understand that the value of the variable has changed?
Suppose we have a file at folder_name/filename.py and call it in some .ipynb
def some_func():
print("test")
which was modified like this
def some_func():
print("test2")
In this case, second call of the following code in .ipynb
import folder_name.filename as module_name
module_name.some_func()
returns test
To update the function, you need to reload the module:
importlib.reload(module_name)
# you can reload "folder_name.filename" if you do not use molude name alias
module_name.some_func()
This code will return test2
Don't forget to import importlib before you run reload
This has happened to me.
You need to restart the python kernel so jupyter can read the new state of the python scripts located in the working directory.
Then the variable a should print the new assigned value.
There's actually a nifty IPython extension for this called autoreload. This answer shows how to use it in an IPython shell, but you can do the same in a Jupyter notebook. Just add:
%load_ext autoreload
%autoreload 2
before you import the module whose changes you want to have tracked, and it'll be re-imported before every cell you execute.
Just in addition to #ilia post:
Let's say the module's name is main.py
That's the code I put in the Jupiter's notebook cell, just before calling functions from the main module
import importlib
imported_module = importlib.import_module("main")
importlib.reload(imported_module)
from main import *

Jupyter Notebook No module named 'Grid' when Importing from Subdirectory

Before reading my question I would like to clarify that running the program within the python shell will run without any issues. I need to run some code through jupyter notebook and only then does importing within jupyter notebook will result in a No Module found error. This issue is described further below.
I have a project with the following hierarchy:
-ProjectFolder
-test.ipynb
-modules
-Game.py
-Grid.py
I was able to successfully import both Game.py and Grid.py into my jupyter notebook test file by adding the two lines to import each python file like below:
test.ipynb
from modules import Game
from modules import Grid
Unfortunately for this project there is a hard requirement. I must import the Grid Class within the Game.py file. Because both Game.py and Grid.py reside in the same folder I thought I could simply add the line import Grid at the top of Game.py without any issues. I did this by adding the line importGrid:
Game.py
import Grid
...
class Grid(object):
// grid structure logic
With that line, my test.ipynb now imports only the Game class from Game.py like so:
test.ipynb
from modules import Game
...
class Game(object):
// game logic
When running test in jupyter I unfortunately get the error No module named 'Grid'. Could anyone please tell me what I am doing wrong in this case? I have tried several things like relative imports trying from .Grid import Grid but this didnt work either and had a module not found error as well"

NameError: name 'log10' is not defined in function called in script

Why log10() is failing to be recognized when called within a function definition in another script? I'm running Python3 in Anaconda (Jupyter and Spyder).
I've had success with log10() in Jupyter (oddly without even calling "import math"). I've had success with defining functions in a .py file and calling those functions within a separate script. I should be able to perform a simple log10.
I created a new function (in Spyder) and saved it in a file "test_log10.py":
def test_log10(input):
import math
return math.log10(input)
In a separate script (Jupyter notebook) I run :
import test_log10
test_log10.test_log10(10)
I get the following error:
"NameError: name 'log10' is not defined"
What am I missing?
Since I'm not using the environment of Jupyther and alike, I don't know how to correct it in these system, perhaps there is some configuration file over there,check the documentation.
But exactly on the issue, when this happens its because python has not "linked" well something at the import, so I suggest a workaround with the libs in the next way:
import numpy as np
import math
and when you are using functions from math, simply add the np. before, i.e.:
return math.log10(input)
to
return np.math.log10(input)
Exactly I don't know why the mismatch, but this worked for me.

Run a function in py file on import

I've written an interpreter in python, saved it to a .py file. To use it in my jupyter notebook I type-
import galickgun #galickgun is the interpreter name
galickgun.read_eval_printloop()
#GalickGun>
this function asks for an input and I enter my new language syntax in the input and get and an output. Everything is fine there.
I'd like to run the read_eval_printloop function as soon as import the .py file for the interpreter in my python environment. As in -
import galickgun
#GalickGun>
Are there better ways to do this? What are some ways I can interact with this new "language".

Resources