Python: cannot import name x for importing module - python

** EDIT: Copy-pasting my actual file to ease confusion. The code snippet below is in a file named train_fm.py:
def eval_fm(x,b,w,V):
# evaluate a degree 2 FM. x is p X B
# V is p x k
# some python code that computes yhat
return(yhat);
Now in my main file: I say the following
from train_fm import eval_fm
and I get the error:
ImportError: cannot import name f1
When I type
from train_fm import train_fm
I do not get an error.
OLD QUESTION BELOW :
def train_fm(x,y,lb,lw,lv,k,a,b,w,V):
# some code
yhat = eval_fm(x,b,w,V);
# OUTPUTS
return(b,w,V);
I have a file called f2.py, where I define 2 functions (note that one of the functions has the same name as the file)
def f1():
some stuff;
return(stuff)
def f2():
more stuff;
y = f1();
return(y)
In my main file, I do
from aaa import f1
from aaa import f2
but when I run the first of the 2 commands above, I get
ImportError: cannot import name f1
Any idea what is causing this? The second function gets imported fine.

Related

How to import a cached numba function without a Python definition

Consider the following function in numba, which just serves as an example:
import numba as nb
import numpy as np
#nb.njit('float64(float64[::1])', cache=True)
def total (x):
''' Sum the elements of an array. '''
total = 0
for i in range(x.shape[0]):
total += x[i]
return total
x = np.arange(100,dtype=np.float64)
print(total(x))
Since I have specified the cache=True option, two files are created in the __pycache__ folder, one .nbc file and one .nbi file. I assume that these files contain all (compiled) information about the function. Let's say I delete the Python file that defines the function (i..e, the above code).
Can I still use compiled/cached functions? In other words, can I import them without having the original .py file that defined them?
#Michael Szczesny's comment about ahead of time compilation is exactly what I wanted. To make it work, I have adapted the code as follows:
from numba.pycc import CC
cc = CC('my_module')
#cc.export('total', 'float64(float64[::1])')
def total (x):
''' Sum the elements of an array. '''
total = 0
for i in range(x.shape[0]):
total += x[i]
return total
if __name__ == "__main__":
cc.compile()
After running this file, a binary module file (.pyd) is saved in the directory and you can use it as follows:
import numpy as np
from my_module import total
x = np.arange(100,dtype=np.float64)
print(total(x))

using functions with variables from other python files at notebook's directory

For example, in my folder, I have my ipython notebook "program.ipynb" and a python file "functions.py" which has some functions in it, for example, "func"
from numpy import sqrt
def func(x):
return N + sqrt(x)
that is going to be used in "program.ipynb" which looks like that
from functions import func
N = 5
func(2)
--> name 'N' is not defined
To fix the bug i need to define the variable N in my functions.py file but isn't there a way around? I want to define all my global variables in my main programm (program.ipynb).
You can't access a variable like that, the best way would be:
functions.py
from numpy import sqrt
def func(x, N):
return N + sqrt(x)
program.ipynb
from functions import func
N = 5
func(2, N)

Python 3.6 Statistics module - NameError: name 'statistics' is not defined

While learning to use "statistics module" in python 3.6 I am facing the following error:
NameError: name 'statistics' is not defined
I am just testing statistics basic functions which should return mean, median, mode, stdev, variance.
I am new to Python and I can't find where the error is.
Code:
from statistics import *
example_list = [5,2,5,6,1,2,6,7,2,6,3,5,5]
x = statistics.mean(example_list)
print(x)
y = statistics.median(example_list)
print(y)
z = statistics.mode(example_list)
print(z)
a = statistics.stdev(example_list)
print(a)
b = statistics.variance(example_list)
print(b)
What am I doing wrong?
If I do this in IDLE, all works as expected.
>>> from statistics import *
>>> example_list = [5,2,5,6,1,2,6,7,2,6,3,5,5]
>>> x = mean(example_list)
>>> x
4.230769230769231
So I don't get the error you report at x = mean(example_list).
You haven't reported your stack trace (why not?) so it's not possible for me to tell, but I suspect you have named your test program statistics.py, and that is hiding the real statistics module.
"from" module "import" * brings in all the names defined in __all__ if that exists and all names except for those starting with an underscore if __all__ doesn't exist.
You don't need to qualify the names imported (that is, prefix them with statistics). Just used them directly, median, mode, stdev, variance.

Python Updating Source Code wrt Output of a Function

As example, my first module is
from sympy import *
x,y=symbols('x y')
def A():
equation=2*x+y
return equation
print(A())
the output is
2*x + y
my second module is
def B(x,y):
equation=2*x + y
return equation
I have to copy past the output of first module to second one each time from the terminal. Is there a way to pass this output automatically to the source code of B(x,y)?
Thank you for your attention

Python dynamic import and changing a variable

Lets say I have the following config file:
config.py:
x = 2
y = x * 2
I would like to import this in the file main.py, preferably using load_source command, but I also want to be able to change the value of x at the time of import such that the change in x propagates to the other variables in the config.py. For example, I want the following code, prints 6 and not 4.
main.py:
import imp
config = imp.load_source('', 'config.py')
config.x = 3
print config.y
What is the best way to do that? I know I can write functions in config.py to do this for me, but I prefer the config to be simple variable definitions only.
Put the code into a class:
class Config(object):
def __init__(self, x=2):
self.x = x
self.y = x * 2
Then, in your main program:
c = Config(3)
print c.y

Categories