I was trying to run this code but it says
NameError Traceback (most recent call last)
<ipython-input-5-49e303967177> in <module>
3
4 #Detector object created
----> 5 fd=FaceDetector(frontal_cascade_path)
NameError: name 'FaceDetector' is not defined
This is the code: -
#Frontal face of haar cascade loaded
frontal_cascade_path="../input/haarcascade-frontal-faces/haarcascade_frontalface_default.xml"
#Detector object created
fd=FaceDetector(frontal_cascade_path)
Read this. You need to import the FaceDetector into your code:
from face_detector import FaceDetector
Related
I can't load class I defined in another file. It is strange behavior that I CAN load function defined in the same file.
XXXX.py
def hoge():
print('hoge')
class YYYY:
def hoge(self):
print('hoge')
I try to import and run XXXX as follow:
import XXXX
XXXX.hoge()
XXXX.YYYY
Then, I encountered the error
hoge
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-76-cbd9d0cb0faf> in <module>
2 XXXX.hoge()
----> 3 XXXX.YYYY
AttributeError: module 'XXXX' has no attribute 'YYYY'
I use Python 3.6
I've solved this problem.
This error occurs on jupyter-notebook.
But it works on terminal.
I don't know why this happens on jupyter.
I'm using Python since few days and I'm trying to learn as much as possible from it. I'm using Jupyter notebook as well. I made a python file fibo.py where I code a function fib and I saved it. In the same folder I try to import that module and use the function fib
import pandas as pd
import numpy as np
import fibo
result = fibo.fib(10)
but I get the following error message :
AttributeError: module 'fibo' has no attribute 'fib'
Could you please give me some suggestions where the problem should be? Thank you in advance.
This is the code for fibo.py
# Fibonacci numbers module\n",
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
print()
The error I get is here:
AttributeError Traceback (most recent call last)
<ipython-input-1-73202afd3146> in <module>
3
4 import fibo
----> 5 result = fibo.fib(10)
AttributeError: module 'fibo' has no attribute 'fib'
I tried also
from fibo import fib
And I get the following:
ImportError Traceback (most recent call last)
<ipython-input-2-b2d78eaf1dcb> in <module>
4 import fibo
5 #result = fibo.fib(10)
----> 6 from fibo import fib
ImportError: cannot import name 'fib' from 'fibo' (C:\Users\my_folder\Documents\JupyterWork\fibo.py)
Save the file as .py using any text editor other than jupyter notebook.
Because jupyter notebook file are stored in JSON format.
I'm stuck on importing a module from hardPredictions. I can find documentation on installing the libraryand using the module module, but not how to import the module.
I've tried the following:
from hardPredictions import HoltWinters
model = HoltWinters()
This gives me an error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-10-72e6928695cf> in <module>
----> 1 model = HoltWinters()
TypeError: 'module' object is not callable
This works, but then I can't pass my ts to the model:
from hardPredictions import HoltWinters
model = HoltWinters
model.fit(ts)
gives an error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-13-f40ff401465a> in <module>
1 model = HoltWinters
----> 2 model.fit(ts)
AttributeError: module 'hardPredictions.HoltWinters' has no attribute 'fit'
I've also tried running help() and dir() on HoltWinters, but think the main thing is I'm lacking the skill to ask the library how to import a model. Your advice is much appreciated!
Given a django app called mattermost which has a model called Channel we can do something like this.
import mattermost
for channel in Channel.objects.all():
print(channel)
I'd like to be able to do something like this
import mattermost
mattermost.channels.list
I've tried adding channels.py with a def list(): function in the same folder as mattermost/init.py.
I'm getting the following error.
In [7]: reload(mattermost)
Out[7]: <module 'mattermost' from '/home/csmu/mipgen-django/mattermost/__init__.py'>
In [8]: mattermost.channels.list
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-8-d4715777f4f1> in <module>()
----> 1 mattermost.channels.list
AttributeError: module 'mattermost' has no attribute 'channels'
How do you add an attribute to a django app python module?
The contents of channels.py:
import mattermost
def list():
for channel in mattermost.models.Channel.objects.all():
print(channel)
Try
from mattermost import channels
print(channels.list())
This resulted in:
In [1]: import mattermost
In [2]: mattermost.channels.list()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-2-249466f32547> in <module>()
----> 1 mattermost.channels.list()
AttributeError: module 'mattermost' has no attribute 'channels'
In [3]: from mattermost import channels
In [4]: mattermost.channels.list()
list.stub
In [5]:
which is close.
I discovered you can do this by adding the following to mattermost/__init__py
#!/usr/bin/env python
import os, pkgutil
__all__ = list(module for _, module, _ in
pkgutil.iter_modules([os.path.dirname(__file__)]))
then the following works
import mattermost
from mattermost import *
mattermost.channels.list()
which produces output for each mattermost channel.
Hi I'm having trouble importing classes and variables into python files from other python files. Functions work fine.
As a test, I set up file1 with a function, a class, an assigned instance of the class, and a random variable. I then used various methods in file2:
1.
import file1
2.
from file1 import *
error: 'name not defined'
3.
from file1 import variable,class,instance,etc
error: cannot import name Class
4+. And then doing some other things...
creating a init.py file
or then trying to set directory:
import os
os.chdir("/Users/mardersteina/Documents")
Not sure what I'm doing wrong. Function imports fine, but can't figure this one out with the classes and variables no matter what I'm looking up.
Untitled7:
def happy():
print "yo!"
class Tap(object):
def __init__(self,level):
self.level = level
level4 = Tap(4)
x = 14
Untitled9:
%run "/Users/mardersteina/Documents/Untitled9.py"
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/Users/mardersteina/Documents/Untitled9.py in <module>()
1 import Untitled7
2
----> 3 print Untitled7.x
4 """
5 from Untitled7 import Tap
AttributeError: 'module' object has no attribute 'x'
%run "/Users/mardersteina/Documents/Untitled9.py"
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
/Users/mardersteina/Documents/Untitled9.py in <module>()
4 print Untitled7.x
5 """
----> 6 from Untitled7 import Tap
7
8 print Tap(4).level
ImportError: cannot import name Tap
%run "/Users/mardersteina/Documents/Untitled9.py"
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/Users/mardersteina/Documents/Untitled9.py in <module>()
11 from Untitled7 import *
12
---> 13 print level4.level
NameError: name 'level4' is not defined
I can see that you are running the file from an open console .
Most probably the issue is that you had imported the Untitled7.py previously when it only had one function . When you do that Python caches the module in sys.modules .
So if you try to import it in same session again, you would get the cached version from sys.modules , and that would be the reason any changes to the Untitled7 you did after importing it once are not visible.
To fix this issue, you can reload the module -
In Python 3.x , use importlib.reload() to reload the module (to take in new changes) , Example -
import importlib
importlib.reload(Untitled7)
In Python 2.x , use reload() method -
reload(Untitled7)
Or you can also close the python terminal and reopen it, and it should fix the issue.