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".
Related
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]’)
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 *
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.
I'm new to NiFi. I'm trying to execute a Python script using ExecuteScript processor. When I tried a simple script which has no import commands it ran fine and showed output in nifi.StdOut. When I tried to run a script which includes import commands like import pandas. It showing the below error:
Import Error: No module named Pandas
I tried providing the path of the pkgs in the Module directory in properties. But it doesn't workout. Any help would be appreciated!
I believe the issue is that pandas is a natively-compiled module (it is written in C) rather than being pure Python. The reason this is a problem is that due to the JSR-223 engine, the Apache NiFi ExecuteScript processor uses Jython rather than actual Python. So Python code is fine to run, but it can't depend on modules that aren't pure Python.
The workaround is to use the ExecuteStreamCommand processor to invoke the Python script which depends on pandas via the command-line (i.e. python my_script_that_uses_pandas.py). The flowfile content will be streamed to STDIN and captured from STDOUT. Here's a related answer describing this in detail.
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.