Jupyter Import Module AND use function. How can I do that? - python

I need to import a function classify(par1, par2, par3)
from a module called _Y03_Labeling. Importing does work, but using its functions with more than one extra parameter doesnt work.
Question : How can I import functions with more than one Parameter?
.
What I already tried ( Without Success):
I successfully can run the whole notebook2 from notebook1 with the following code:
import _Y03_Labeling
Labeling =_Y03_Labeling
(Why I know if it is successful? Because its comments are printed out). Whenever I try to run:
X,y = classify(a,b,c)
I get the following error: "TypeError: 'module' object is not callable"
I tried many variations from the import line, including:
import _Y03_Labeling
Labeling =_Y03_Labeling
X,y = Labeling.classify(a,b,c)
# or:
from _Y03_Labeling import classify
# or:
import _Y03_Labeling
X,y = _Y03_Labeling .classify(a,b,c)
Sadly none of them worked for me.
Things i also did so far:
shutting down the _Y03_Labeling notebook before I run the main Notebook
putting the function in the second notebook into a class, importing the class from the notebook and calling the function. (works only if function needs 1 parameter)
also i didnt forgot "self" i the function declaration with the class try.
I am glad, this forum exists and thankful for every possible help.

Related

A function in a module i imported returns with not defined

import mafia
game.add_faction(Town())
game.add_faction(Town())
game.add_faction(Mafia("Crypto Mafia"))
game.add_player("Alice", Cop(town))
game.add_player("Bob", Doctor(town))
game.add_player("Eve", Goon(mafia))
Hello I'm trying to write a python program with a module called mafia (It's from github: https://github.com/calder/mafia/) But when i tried to test the module it gives me "Game" is not defined, I installed the mafia, checked it with dir command, it works. I'm the faulty one here so please enlighten me.
(Btw don't bash me i'm new to python)
Edit: changed import mafia to
from mafia import *
now gives 'mafia.game' has no attribute 'add_faction' error.
You need to add following line.
from mafia import *
g = Game()
Then you can append your code
game.add_faction(Town())
game.add_faction(Mafia("Crypto Mafia"))
game.add_player("Alice", Cop(town))
game.add_player("Bob", Doctor(town))
game.add_player("Eve", Goon(mafia))

python not able to import

Importing inside the function, Python is not able to import this. Outside the function, it works fine. This is actually a Django server and view_page function gets called from outside the file.
So, basically doing this throws up an error "Error : No module named plotting"
My code:
def view_page():
from bokeh.plotting import figure # This throws up an error
pass
This works absolutely fine;
from bokeh.plotting import figure
def view_page():
pass

'module' object has no attribute 'cmap' in cubehelix python

I'm trying to use cubehelix in python however I've been getting simple problems which I don't think should be showing up. The code I'm using is the following:
import matplotlib.pyplot as plt
import numpy as np
import cubehelix
cx1 = cubehelix.cmap(reverse=True)
alplot = plt.imshow(rtotal_rotated,vmax=1100,extent[-21,19,23,-17],cmap=cx1)
However the following error comes up when I run the code:
AttributeError: 'module' object has no attribute 'cmap'
I know this can't be right since I'm simply following the code from this tutorial
http://www.ifweassume.com/2014/04/cubehelix-colormap-for-python.html
So I'm not sure why it's breaking.
Very like that the script imports itself, i.e. your script is named cubehelix.py. So, if you have file named cubehelix.py in your current working directory, rename it to my_cubehelix.py and try again.

Access part of module from pytest

I have an issue accessing part of imported module from the pytest.
Here is branch with code referenced below: https://github.com/asvc/snapshotr/tree/develop
In particular, when running this test, it works as expected for test_correct_installation() but test_script_name_checking() fails with AttributeError.
import main as ss
import os
class TestInit:
def test_correct_installation(self):
assert os.path.exists(ss.snapr_path)
assert os.path.isfile(ss.snapr_path + "/main/markup.py")
assert os.path.isfile(ss.snapr_path + "/main/scandir.py")
def test_script_name_checking(self):
assert ss.ssPanel.check_script('blah') is None # Here it fails
Link to the main which is being tested
What I'm trying to do is to "extract" isolated piece of code, run it with known data and compare result to some reference. Seems like extraction part doesn't work quite well, best practises for such cases would be greatly appreciated.
Traceback:
AttributeError: 'module' object has no attribute 'ssPanel'
I have tried a small hack in the test_init.py:
class dummy():
pass
nuke = dummy()
nuke.GUI = True
But it (obviously) doesn't work as nuke.GUI is being redefined in __init__.py upon every launch.
This is a quite complex situation. When you import main in test_init.py, it will import main/__init__.py and execute all the code. This will cause nuke being imported and also, if nuke.GUI is False, there will not be ssPanel, as you can see.
The problem is that, you can't fake a dummy nuke in the test script. It won't work. Because before the test is running, the real nuke was already imported.
My suggestion would be seperate ssPanel into another python file. Then in __init__.py we can do:
if nuke.GUI:
from sspanel import ssPanel
And in test scripts, we can also easily import it using:
from main.sspanel import ssPanel

can import numpy module but cannot use functionalities

i had succesfully installed numpy (numpy-1.6.2-win32-superpack-python2.7.exe). But, whenever i try to call any functions i am getting following the error below. Thanks in advance for help.
import numpy as np
if __name__ == "__main__":
k = np.arange(10)
AttributeError: 'module' object has no attribute 'arange'
Echoing one of the comments above (as I just had this problem, over 4 years later):
You probably named your file numpy.py. When trying to load a module, I believe the path checks the current directory first, and thus it isn't found.
For sanity, to check that it really is this issue, you should run the Python REPL (python) and type:
import numpy as np, followed by dir(np)
And you should see all of the actual functions as output.
This might also happen because you probably named your program file numpy.py (i made the same mistake)
try the following:
for x in dir(np):
print x
this should list all methods etc of your import, that way you can see if arange() is available.
you could also try
from numpy import *
and then just try:
print arange(10)
Can't think of much else. Odd that the import does not produce an error if arange is not there.

Categories

Resources