I am trying to create my own module (mi_modulo.py) and move there all the functions that I have defined in my Jupyter Notebook script, so that it looks cleaner.
However, when I am trying to use these functions that I have already moved to the module, I am not able to use them all, and I get the following message:
module 'mi_modulo' has no attribute 'train4_data_import'
I have installed Anaconda 3.0 and I am running Python 3.7.0 through Jupyter Notebooks. (Forgive me if the expressions sound awkward, I know a bit of Python, but I am not really into all the installation, software, IDE, etc details.)
## mi_modulo.py ##
def train4_data_import(file_name):
df = pandas.read_excel(file_name)
force = df["Signal 1"].values[13:]
acceleration1 = df["Signal 2"].values[13:]
acceleration2 = df["Signal 3"].values[13:]
return force, acceleration1, acceleration2
def hola_mundo():
print("whatever")
## script ##
import pandas
import mi_modulo as mi
mi.hola_mundo()
mi.train4_data_import("Tren4.xlsx")
And this is what I get:
(I was going to show an image but I am not sure how to do that with this stackoverflow new form style)
whatever
AttributeError Traceback (most recent call last)
<ipython-input-18-69a38929f7e6> in <module>()
3 mi.hola_mundo()
4
----> 5 mi.train4_data_import()
AttributeError: module 'mi_modulo' has no attribute 'train4_data_import'
I don't understand why it is able to read one function but not the other.
----------------------------- EDIT 1 ----------------------------
Doing what U9-Forward suggests:
import pandas
from mi_modulo import *
hola_mundo()
train4_data_import("Tren4.xlsx")
I get now the following error:
whatever
NameError Traceback (most recent call last)
<ipython-input-25-e1885200beb7> in <module>()
3 hola_mundo()
4
----> 5 train4_data_import("Tren4.xlsx")
NameError: name 'train4_data_import' is not defined
In jupyter-notebook, sometimes you need to restart the kernel to import all the unsaved module you have. Also, you need to import all the dependency for the custom module within that module.
It is probably because you didn't press Ctrl+S or hit the save button on the file, it will probably work if you do that:
Ctrl+S
Or save button.
then run script.py and see it working :-)
Related
I'm new to python and I'm currently learning objects and graphics. I imported the graphics.py file successfully but for some reason it keeps giving me an attribute error whenever I try to run GraphWin. please see below:
import graphics
win = graphics.GraphWin()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'graphics' has no attribute 'GraphWin'
I'm using Zelle's "Python Programming: An Introduction to Computer Science" and it's been helpful.
please advise.
Is the graphics.py in the same as the file you’re getting the error in?
Also, try
from graphics import *
Instead of
import graphics
i checked out this article and it proved helpful. i just had to create an extension where i added the specific sub-module to the module folder. in this case it's:
from graphics._init_ import *
win = GraphWin()
this solves the attribute error. however, it only works with the from import * formula and not the import formula
I'm a rookie using Dask and I installed the new version 2.12.0 on my MacBook MacOS High Sierra 10.13.6. When I try to start the distributed mode with the code below:
from dask.distributed import Client
c = Client()
I got the following error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-1-67101dbde0b6> in <module>()
1 from dask.distributed import Client
----> 2 c = Client()
~/anaconda3/lib/python3.6/site-packages/distributed/client.py in __init__(self, address, loop, timeout, set_as_default, scheduler_file, security, asynchronous, name, heartbeat_interval, **kwargs)
554 if set_as_default:
555 self._previous_get = _globals.get('get')
--> 556 dask.set_options(get=self.get)
557 self._previous_shuffle = _globals.get('shuffle')
558 dask.set_options(shuffle='tasks')
AttributeError: module 'dask' has no attribute 'set_options'
Also, if I instantiate the c = Client(set_as_default=False) the cluster seems to successfully raise because I get the connection information for the dashboard (see this image). But as I navigate through the dashboard the following error is triggered.
According to the documentation lifting a single machine cluster seems to be trivial but I don't know what could be wrong. I would be very grateful if someone could guide me on how to solve this incident ;)
Your source file is named dask.py, which introduces a naming conflict between your module, dask.py, and the dask package, with your module taking precedence in later import statements. Therefore, when distributed/client.py attempts to call dask.set_options, this fails with AttributeError as your module does not provide this function.
After running this dask.py module, python caches the compiled python code. Therefore, even when you renamed your module to something else, the import name conflict still exists. To resolve this, you should delete the __pycache__ directory after which you should find that your module executes successfully.
This is my first time asking a question so please excuse me.
I am following a tutorial and using the code in this folder: https://github.com/ehmatthes/pcc_2e/tree/master/chapter_12/adding_ship_image
I literally replicated this folder in vs code with all the code and the names being used correctly.
For some reason I am getting this error:
Traceback (most recent call last):
File "/Users/sammyawad/Documents/projects/alieninvasion/alien_invasion.py", line 6, in <module>
from ship import Ship
ImportError: cannot import name 'Ship' from 'ship' (/Users/sammyawad/Documents/projects/alieninvasion/ship.py)
Also, I think I have an issue with linting because it is highlighting the pygame init functions for the class even though it works.
Change it to:
from .ship import Ship
import module as instance of class like this...
from ship import Ship as s1
I'd like to define a function in Python which itself uses a function from numpy, namely, the sine function 'sin'. To simplify the problem, I've made a test function "sine_test.py" as follows:
import numpy
def sine_test(theta):
return numpy.sin(theta)
I also want to run this function from a script, named "use_sine_test.py" and saved in the same directory, which reads as follows:
import numpy
from sine_test import sine_test
u=sine_test(1)
I would expect this to define u as the sine of 1 radian, about 0.84. Both "use_sine_test.py" and "sine_test.py" are in the same folder (D:/Python). However, if I run the "use_sine_test.py" Python script using the run button, I get a "NameError: global name 'sin' is not defined" error as shown below.
Any ideas what might be causing this?
Best regards,
Kurt
%run D:/Python/use_sine_test.py
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
C:\Program Files\Enthought Python 7.2.2\lib\site-packages\IPython\utils\py3compat.py in execfile(fname, glob, loc)
166 else:
167 filename = fname
--> 168 exec compile(scripttext, filename, 'exec') in glob, loc
169 else:
170 def execfile(fname, *where):
D:\Python\use_sine_test.py in <module>()
1 import numpy
2 from sine_test import sine_test
----> 3 u=sine_test(1)
D:\Python\sine_test.py in sine_test(theta)
1 import numpy
----> 2 def sine_test(theta):
3 return numpy.sin(theta)
NameError: global name 'sin' is not defined
more a comment than an answer (but i cant put code properly in a comment):
does it work if you put the next code in one 'something.py' file, and than just do python something.py in your terminal.
import numpy
def sine_test(theta):
return numpy.sin(theta)
u=sine_test(1)
Just to see if it is something related to a bad install, or if it is something related to ipython, or if it is something to do with how your files interact with each other. This way the problem can be narrowed down a bit. If this works as it should, than it is related to how your files work with each other or to your ipython. The next step would than be to just test it in different files like you do, but not in ipython, but just in terminal to see if it works in python, and if it is not a ipython problem. Than if that works too, than it is probably related to your ipython.
All,
Following BrenBarn's comment, I've tried to restart the Code Editor in Canopy and it seems to work now:
Welcome to EPD's interactive data-analysis environment!
with pylab-backend: wx
Type '?' for more information.
Welcome to pylab, a matplotlib-based Python environment [backend: WXAgg].
For more information, type 'help(pylab)'.
%run D:/Python/use_sine_test.py
u
Out[2]: 0.8414709848078965
Thanks for your comments!
I have a segmenting.py module in a package called processing.
I am trying to call a function in the module in my main. It is extremely simple.
In main.py
from processing import segmenting
segmenting.test()
In segmenting.py
def test():
print 'succeed'
However, I end up with errors as follows:
>>> from processing import segmenting
>>>
>>> segmenting.test()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'test'
>>>
Where went wrong?
The most likely cause is that you didn't restart your interactive interpreter after editing (and saving!) segmenting.py. Modules are imported only once and cached. If you edit the source code and then run the import statement again, the module is simply retrieved from the cache and doesn't pick up your changes. See also the reload() built-in.