AttributeError: 'module' object has no attribute
I have two python modules:
a.py
import b
def hello():
print "hello"
print "a.py"
print hello()
print b.hi()
b.py
import a
def hi():
print "hi"
When I run a.py
, I get:
AttributeError: 'module' object has no attribute 'hi'
What does the error mean? How do I fix it?
python attributeerror
add a comment |
I have two python modules:
a.py
import b
def hello():
print "hello"
print "a.py"
print hello()
print b.hi()
b.py
import a
def hi():
print "hi"
When I run a.py
, I get:
AttributeError: 'module' object has no attribute 'hi'
What does the error mean? How do I fix it?
python attributeerror
13
This is an awful design. WHy does b.py import a when it doesn't reference any part of a.py? Are you asking how to fix this problem? stackoverflow.com/search?q=%5Bpython%5D+circular+dependency
– S.Lott
Aug 8 '09 at 23:40
Note that your questions is very similar to this answer. Apparently the code in this answer works just find, but yours does not? stackoverflow.com/a/7336880/565879
– Buttons840
May 2 '14 at 18:15
add a comment |
I have two python modules:
a.py
import b
def hello():
print "hello"
print "a.py"
print hello()
print b.hi()
b.py
import a
def hi():
print "hi"
When I run a.py
, I get:
AttributeError: 'module' object has no attribute 'hi'
What does the error mean? How do I fix it?
python attributeerror
I have two python modules:
a.py
import b
def hello():
print "hello"
print "a.py"
print hello()
print b.hi()
b.py
import a
def hi():
print "hi"
When I run a.py
, I get:
AttributeError: 'module' object has no attribute 'hi'
What does the error mean? How do I fix it?
python attributeerror
python attributeerror
edited Dec 7 '13 at 22:05
Eric Leschinski
89.7k40330281
89.7k40330281
asked Aug 8 '09 at 23:12
Stephen HsuStephen Hsu
2,27462438
2,27462438
13
This is an awful design. WHy does b.py import a when it doesn't reference any part of a.py? Are you asking how to fix this problem? stackoverflow.com/search?q=%5Bpython%5D+circular+dependency
– S.Lott
Aug 8 '09 at 23:40
Note that your questions is very similar to this answer. Apparently the code in this answer works just find, but yours does not? stackoverflow.com/a/7336880/565879
– Buttons840
May 2 '14 at 18:15
add a comment |
13
This is an awful design. WHy does b.py import a when it doesn't reference any part of a.py? Are you asking how to fix this problem? stackoverflow.com/search?q=%5Bpython%5D+circular+dependency
– S.Lott
Aug 8 '09 at 23:40
Note that your questions is very similar to this answer. Apparently the code in this answer works just find, but yours does not? stackoverflow.com/a/7336880/565879
– Buttons840
May 2 '14 at 18:15
13
13
This is an awful design. WHy does b.py import a when it doesn't reference any part of a.py? Are you asking how to fix this problem? stackoverflow.com/search?q=%5Bpython%5D+circular+dependency
– S.Lott
Aug 8 '09 at 23:40
This is an awful design. WHy does b.py import a when it doesn't reference any part of a.py? Are you asking how to fix this problem? stackoverflow.com/search?q=%5Bpython%5D+circular+dependency
– S.Lott
Aug 8 '09 at 23:40
Note that your questions is very similar to this answer. Apparently the code in this answer works just find, but yours does not? stackoverflow.com/a/7336880/565879
– Buttons840
May 2 '14 at 18:15
Note that your questions is very similar to this answer. Apparently the code in this answer works just find, but yours does not? stackoverflow.com/a/7336880/565879
– Buttons840
May 2 '14 at 18:15
add a comment |
9 Answers
9
active
oldest
votes
You have mutual top-level imports, which is almost always a bad idea.
If you really must have mutual imports in Python, the way to do it is to import them within a function:
# In b.py:
def cause_a_to_do_something():
import a
a.do_something()
Now a.py can safely do import b
without causing problems.
(At first glance it might appear that cause_a_to_do_something()
would be hugely inefficient because it does an import
every time you call it, but in fact the import work only gets done the first time. The second and subsequent times you import a module, it's a quick operation.)
Thank you! I know for the first time that importing in different places make such a difference.
– Stephen Hsu
Aug 9 '09 at 0:58
Please be aware that this adds over-head when the function is called, as you put the import logic at function call time, rather than program load time.
– Rebs
Nov 20 '15 at 3:03
4
Interesting; I wonder why the interpreter does not give a proper error message in this case?
– Haroldo_OK
May 17 '16 at 1:07
add a comment |
I have also seen this error when inadvertently naming a module with the same name as one of the standard Python modules. E.g. I had a module called commands
which is also a Python library module. This proved to be difficult to track down as it worked correctly on my local development environment but failed with the specified error when running on Google App Engine.
6
this is the answer that solved my problem.
– Tommy
Aug 17 '15 at 18:46
2
I usedabc.py
to write a test to demonstrate the import behavior in python, that bites me a lot...
– Bin
Sep 23 '16 at 20:57
2
I suspected this and deleted the .py module but forgot to delete the .pyc which was still causing the error.
– Echelon
Apr 26 '17 at 21:23
I created a math module , which is already standard module.
– refactor
Nov 8 '17 at 16:31
add a comment |
The problem is the circular dependency between the modules. a
imports b
and b
imports a
. But one of them needs to be loaded first - in this case python ends up initializing module a
before b
and b.hi()
doesn't exist yet when you try to access it in a
.
Thank you! It is what I guessed. But I cannot find some documents mention it. If I do need two modules import some attributes from each other, what should I do?
– Stephen Hsu
Aug 8 '09 at 23:25
1
@Stephen Hsu: Breaking circular dependencies is easy. It's already been asked on SO several times. stackoverflow.com/search?q=%5Bpython%5D+circular+dependency
– S.Lott
Aug 8 '09 at 23:39
@S.Lott: Thank you. I just know that it is a circular dependencies problem.
– Stephen Hsu
Aug 9 '09 at 1:01
add a comment |
I got this error by referencing an enum which was imported in a wrong way, e.g.:
from package import MyEnumClass
# ...
# in some method:
return MyEnumClass.Member
Correct import:
from package.MyEnumClass import MyEnumClass
Hope that helps someone
add a comment |
I experienced this error because the module was not actually imported. The code looked like this:
import a.b, a.c
# ...
something(a.b)
something(a.c)
something(a.d) # My addition, which failed.
The last line resulted in an AttributeError
. The cause was that I had failed to notice that the submodules of a
(a.b
and a.c
) were explicitly imported, and assumed that the import
statement actually imported a
.
add a comment |
I ran into this problem when I checked out an older version of a repository from git. Git replaced my .py
files, but left the untracked .pyc
files. Since the .py
files and .pyc
files were out of sync, the import
command in a .py
file could not find the corresponding module in the .pyc
files.
The solution was simply to delete the .pyc
files, and let them be automatically regenerated.
You can use this command to delete all.pyc
files:find . -name "*.pyc" -exec rm -f {} ;
– Ollie
Jan 24 at 2:26
add a comment |
All the above answers are great, but I'd like to chime in here. If you did not spot any issue mentioned above, try clear up your working environment. It worked for me.
add a comment |
Not sure how but the below change sorted my issue:
i was having the name of file and import name same for eg i had file name as emoji.py and i was trying to import emoji. But changing the name of file solved the issue .
Hope so it helps
add a comment |
Circular imports cause problems, but Python has ways to mitigate it built-in.
The problem is when you run python a.py
, it runs a.py
but not mark it imported as a module. So in turn a.py
-> imports module b -> imports module a -> imports module b. The last import a no-op since b is currently being imported and Python guards against that. And b is an empty module for now. So when it executes b.hi()
, it can't find anything.
Note that the b.hi()
that got executed is during a.py
-> module b -> module a, not in a.py
directly.
In your specific example, you can just run python -c 'import a'
at top-level, so the first execution of a.py
is registered as importing a module.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1250103%2fattributeerror-module-object-has-no-attribute%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have mutual top-level imports, which is almost always a bad idea.
If you really must have mutual imports in Python, the way to do it is to import them within a function:
# In b.py:
def cause_a_to_do_something():
import a
a.do_something()
Now a.py can safely do import b
without causing problems.
(At first glance it might appear that cause_a_to_do_something()
would be hugely inefficient because it does an import
every time you call it, but in fact the import work only gets done the first time. The second and subsequent times you import a module, it's a quick operation.)
Thank you! I know for the first time that importing in different places make such a difference.
– Stephen Hsu
Aug 9 '09 at 0:58
Please be aware that this adds over-head when the function is called, as you put the import logic at function call time, rather than program load time.
– Rebs
Nov 20 '15 at 3:03
4
Interesting; I wonder why the interpreter does not give a proper error message in this case?
– Haroldo_OK
May 17 '16 at 1:07
add a comment |
You have mutual top-level imports, which is almost always a bad idea.
If you really must have mutual imports in Python, the way to do it is to import them within a function:
# In b.py:
def cause_a_to_do_something():
import a
a.do_something()
Now a.py can safely do import b
without causing problems.
(At first glance it might appear that cause_a_to_do_something()
would be hugely inefficient because it does an import
every time you call it, but in fact the import work only gets done the first time. The second and subsequent times you import a module, it's a quick operation.)
Thank you! I know for the first time that importing in different places make such a difference.
– Stephen Hsu
Aug 9 '09 at 0:58
Please be aware that this adds over-head when the function is called, as you put the import logic at function call time, rather than program load time.
– Rebs
Nov 20 '15 at 3:03
4
Interesting; I wonder why the interpreter does not give a proper error message in this case?
– Haroldo_OK
May 17 '16 at 1:07
add a comment |
You have mutual top-level imports, which is almost always a bad idea.
If you really must have mutual imports in Python, the way to do it is to import them within a function:
# In b.py:
def cause_a_to_do_something():
import a
a.do_something()
Now a.py can safely do import b
without causing problems.
(At first glance it might appear that cause_a_to_do_something()
would be hugely inefficient because it does an import
every time you call it, but in fact the import work only gets done the first time. The second and subsequent times you import a module, it's a quick operation.)
You have mutual top-level imports, which is almost always a bad idea.
If you really must have mutual imports in Python, the way to do it is to import them within a function:
# In b.py:
def cause_a_to_do_something():
import a
a.do_something()
Now a.py can safely do import b
without causing problems.
(At first glance it might appear that cause_a_to_do_something()
would be hugely inefficient because it does an import
every time you call it, but in fact the import work only gets done the first time. The second and subsequent times you import a module, it's a quick operation.)
answered Aug 8 '09 at 23:27
RichieHindleRichieHindle
200k40302370
200k40302370
Thank you! I know for the first time that importing in different places make such a difference.
– Stephen Hsu
Aug 9 '09 at 0:58
Please be aware that this adds over-head when the function is called, as you put the import logic at function call time, rather than program load time.
– Rebs
Nov 20 '15 at 3:03
4
Interesting; I wonder why the interpreter does not give a proper error message in this case?
– Haroldo_OK
May 17 '16 at 1:07
add a comment |
Thank you! I know for the first time that importing in different places make such a difference.
– Stephen Hsu
Aug 9 '09 at 0:58
Please be aware that this adds over-head when the function is called, as you put the import logic at function call time, rather than program load time.
– Rebs
Nov 20 '15 at 3:03
4
Interesting; I wonder why the interpreter does not give a proper error message in this case?
– Haroldo_OK
May 17 '16 at 1:07
Thank you! I know for the first time that importing in different places make such a difference.
– Stephen Hsu
Aug 9 '09 at 0:58
Thank you! I know for the first time that importing in different places make such a difference.
– Stephen Hsu
Aug 9 '09 at 0:58
Please be aware that this adds over-head when the function is called, as you put the import logic at function call time, rather than program load time.
– Rebs
Nov 20 '15 at 3:03
Please be aware that this adds over-head when the function is called, as you put the import logic at function call time, rather than program load time.
– Rebs
Nov 20 '15 at 3:03
4
4
Interesting; I wonder why the interpreter does not give a proper error message in this case?
– Haroldo_OK
May 17 '16 at 1:07
Interesting; I wonder why the interpreter does not give a proper error message in this case?
– Haroldo_OK
May 17 '16 at 1:07
add a comment |
I have also seen this error when inadvertently naming a module with the same name as one of the standard Python modules. E.g. I had a module called commands
which is also a Python library module. This proved to be difficult to track down as it worked correctly on my local development environment but failed with the specified error when running on Google App Engine.
6
this is the answer that solved my problem.
– Tommy
Aug 17 '15 at 18:46
2
I usedabc.py
to write a test to demonstrate the import behavior in python, that bites me a lot...
– Bin
Sep 23 '16 at 20:57
2
I suspected this and deleted the .py module but forgot to delete the .pyc which was still causing the error.
– Echelon
Apr 26 '17 at 21:23
I created a math module , which is already standard module.
– refactor
Nov 8 '17 at 16:31
add a comment |
I have also seen this error when inadvertently naming a module with the same name as one of the standard Python modules. E.g. I had a module called commands
which is also a Python library module. This proved to be difficult to track down as it worked correctly on my local development environment but failed with the specified error when running on Google App Engine.
6
this is the answer that solved my problem.
– Tommy
Aug 17 '15 at 18:46
2
I usedabc.py
to write a test to demonstrate the import behavior in python, that bites me a lot...
– Bin
Sep 23 '16 at 20:57
2
I suspected this and deleted the .py module but forgot to delete the .pyc which was still causing the error.
– Echelon
Apr 26 '17 at 21:23
I created a math module , which is already standard module.
– refactor
Nov 8 '17 at 16:31
add a comment |
I have also seen this error when inadvertently naming a module with the same name as one of the standard Python modules. E.g. I had a module called commands
which is also a Python library module. This proved to be difficult to track down as it worked correctly on my local development environment but failed with the specified error when running on Google App Engine.
I have also seen this error when inadvertently naming a module with the same name as one of the standard Python modules. E.g. I had a module called commands
which is also a Python library module. This proved to be difficult to track down as it worked correctly on my local development environment but failed with the specified error when running on Google App Engine.
answered Jun 26 '10 at 14:18
lucrusselllucrussell
4,08312636
4,08312636
6
this is the answer that solved my problem.
– Tommy
Aug 17 '15 at 18:46
2
I usedabc.py
to write a test to demonstrate the import behavior in python, that bites me a lot...
– Bin
Sep 23 '16 at 20:57
2
I suspected this and deleted the .py module but forgot to delete the .pyc which was still causing the error.
– Echelon
Apr 26 '17 at 21:23
I created a math module , which is already standard module.
– refactor
Nov 8 '17 at 16:31
add a comment |
6
this is the answer that solved my problem.
– Tommy
Aug 17 '15 at 18:46
2
I usedabc.py
to write a test to demonstrate the import behavior in python, that bites me a lot...
– Bin
Sep 23 '16 at 20:57
2
I suspected this and deleted the .py module but forgot to delete the .pyc which was still causing the error.
– Echelon
Apr 26 '17 at 21:23
I created a math module , which is already standard module.
– refactor
Nov 8 '17 at 16:31
6
6
this is the answer that solved my problem.
– Tommy
Aug 17 '15 at 18:46
this is the answer that solved my problem.
– Tommy
Aug 17 '15 at 18:46
2
2
I used
abc.py
to write a test to demonstrate the import behavior in python, that bites me a lot...– Bin
Sep 23 '16 at 20:57
I used
abc.py
to write a test to demonstrate the import behavior in python, that bites me a lot...– Bin
Sep 23 '16 at 20:57
2
2
I suspected this and deleted the .py module but forgot to delete the .pyc which was still causing the error.
– Echelon
Apr 26 '17 at 21:23
I suspected this and deleted the .py module but forgot to delete the .pyc which was still causing the error.
– Echelon
Apr 26 '17 at 21:23
I created a math module , which is already standard module.
– refactor
Nov 8 '17 at 16:31
I created a math module , which is already standard module.
– refactor
Nov 8 '17 at 16:31
add a comment |
The problem is the circular dependency between the modules. a
imports b
and b
imports a
. But one of them needs to be loaded first - in this case python ends up initializing module a
before b
and b.hi()
doesn't exist yet when you try to access it in a
.
Thank you! It is what I guessed. But I cannot find some documents mention it. If I do need two modules import some attributes from each other, what should I do?
– Stephen Hsu
Aug 8 '09 at 23:25
1
@Stephen Hsu: Breaking circular dependencies is easy. It's already been asked on SO several times. stackoverflow.com/search?q=%5Bpython%5D+circular+dependency
– S.Lott
Aug 8 '09 at 23:39
@S.Lott: Thank you. I just know that it is a circular dependencies problem.
– Stephen Hsu
Aug 9 '09 at 1:01
add a comment |
The problem is the circular dependency between the modules. a
imports b
and b
imports a
. But one of them needs to be loaded first - in this case python ends up initializing module a
before b
and b.hi()
doesn't exist yet when you try to access it in a
.
Thank you! It is what I guessed. But I cannot find some documents mention it. If I do need two modules import some attributes from each other, what should I do?
– Stephen Hsu
Aug 8 '09 at 23:25
1
@Stephen Hsu: Breaking circular dependencies is easy. It's already been asked on SO several times. stackoverflow.com/search?q=%5Bpython%5D+circular+dependency
– S.Lott
Aug 8 '09 at 23:39
@S.Lott: Thank you. I just know that it is a circular dependencies problem.
– Stephen Hsu
Aug 9 '09 at 1:01
add a comment |
The problem is the circular dependency between the modules. a
imports b
and b
imports a
. But one of them needs to be loaded first - in this case python ends up initializing module a
before b
and b.hi()
doesn't exist yet when you try to access it in a
.
The problem is the circular dependency between the modules. a
imports b
and b
imports a
. But one of them needs to be loaded first - in this case python ends up initializing module a
before b
and b.hi()
doesn't exist yet when you try to access it in a
.
answered Aug 8 '09 at 23:19
sthsth
168k42248335
168k42248335
Thank you! It is what I guessed. But I cannot find some documents mention it. If I do need two modules import some attributes from each other, what should I do?
– Stephen Hsu
Aug 8 '09 at 23:25
1
@Stephen Hsu: Breaking circular dependencies is easy. It's already been asked on SO several times. stackoverflow.com/search?q=%5Bpython%5D+circular+dependency
– S.Lott
Aug 8 '09 at 23:39
@S.Lott: Thank you. I just know that it is a circular dependencies problem.
– Stephen Hsu
Aug 9 '09 at 1:01
add a comment |
Thank you! It is what I guessed. But I cannot find some documents mention it. If I do need two modules import some attributes from each other, what should I do?
– Stephen Hsu
Aug 8 '09 at 23:25
1
@Stephen Hsu: Breaking circular dependencies is easy. It's already been asked on SO several times. stackoverflow.com/search?q=%5Bpython%5D+circular+dependency
– S.Lott
Aug 8 '09 at 23:39
@S.Lott: Thank you. I just know that it is a circular dependencies problem.
– Stephen Hsu
Aug 9 '09 at 1:01
Thank you! It is what I guessed. But I cannot find some documents mention it. If I do need two modules import some attributes from each other, what should I do?
– Stephen Hsu
Aug 8 '09 at 23:25
Thank you! It is what I guessed. But I cannot find some documents mention it. If I do need two modules import some attributes from each other, what should I do?
– Stephen Hsu
Aug 8 '09 at 23:25
1
1
@Stephen Hsu: Breaking circular dependencies is easy. It's already been asked on SO several times. stackoverflow.com/search?q=%5Bpython%5D+circular+dependency
– S.Lott
Aug 8 '09 at 23:39
@Stephen Hsu: Breaking circular dependencies is easy. It's already been asked on SO several times. stackoverflow.com/search?q=%5Bpython%5D+circular+dependency
– S.Lott
Aug 8 '09 at 23:39
@S.Lott: Thank you. I just know that it is a circular dependencies problem.
– Stephen Hsu
Aug 9 '09 at 1:01
@S.Lott: Thank you. I just know that it is a circular dependencies problem.
– Stephen Hsu
Aug 9 '09 at 1:01
add a comment |
I got this error by referencing an enum which was imported in a wrong way, e.g.:
from package import MyEnumClass
# ...
# in some method:
return MyEnumClass.Member
Correct import:
from package.MyEnumClass import MyEnumClass
Hope that helps someone
add a comment |
I got this error by referencing an enum which was imported in a wrong way, e.g.:
from package import MyEnumClass
# ...
# in some method:
return MyEnumClass.Member
Correct import:
from package.MyEnumClass import MyEnumClass
Hope that helps someone
add a comment |
I got this error by referencing an enum which was imported in a wrong way, e.g.:
from package import MyEnumClass
# ...
# in some method:
return MyEnumClass.Member
Correct import:
from package.MyEnumClass import MyEnumClass
Hope that helps someone
I got this error by referencing an enum which was imported in a wrong way, e.g.:
from package import MyEnumClass
# ...
# in some method:
return MyEnumClass.Member
Correct import:
from package.MyEnumClass import MyEnumClass
Hope that helps someone
answered Apr 16 '15 at 23:32
StoyanStoyan
16912
16912
add a comment |
add a comment |
I experienced this error because the module was not actually imported. The code looked like this:
import a.b, a.c
# ...
something(a.b)
something(a.c)
something(a.d) # My addition, which failed.
The last line resulted in an AttributeError
. The cause was that I had failed to notice that the submodules of a
(a.b
and a.c
) were explicitly imported, and assumed that the import
statement actually imported a
.
add a comment |
I experienced this error because the module was not actually imported. The code looked like this:
import a.b, a.c
# ...
something(a.b)
something(a.c)
something(a.d) # My addition, which failed.
The last line resulted in an AttributeError
. The cause was that I had failed to notice that the submodules of a
(a.b
and a.c
) were explicitly imported, and assumed that the import
statement actually imported a
.
add a comment |
I experienced this error because the module was not actually imported. The code looked like this:
import a.b, a.c
# ...
something(a.b)
something(a.c)
something(a.d) # My addition, which failed.
The last line resulted in an AttributeError
. The cause was that I had failed to notice that the submodules of a
(a.b
and a.c
) were explicitly imported, and assumed that the import
statement actually imported a
.
I experienced this error because the module was not actually imported. The code looked like this:
import a.b, a.c
# ...
something(a.b)
something(a.c)
something(a.d) # My addition, which failed.
The last line resulted in an AttributeError
. The cause was that I had failed to notice that the submodules of a
(a.b
and a.c
) were explicitly imported, and assumed that the import
statement actually imported a
.
answered Jun 24 '16 at 20:26
Dag HøidahlDag Høidahl
4,76843657
4,76843657
add a comment |
add a comment |
I ran into this problem when I checked out an older version of a repository from git. Git replaced my .py
files, but left the untracked .pyc
files. Since the .py
files and .pyc
files were out of sync, the import
command in a .py
file could not find the corresponding module in the .pyc
files.
The solution was simply to delete the .pyc
files, and let them be automatically regenerated.
You can use this command to delete all.pyc
files:find . -name "*.pyc" -exec rm -f {} ;
– Ollie
Jan 24 at 2:26
add a comment |
I ran into this problem when I checked out an older version of a repository from git. Git replaced my .py
files, but left the untracked .pyc
files. Since the .py
files and .pyc
files were out of sync, the import
command in a .py
file could not find the corresponding module in the .pyc
files.
The solution was simply to delete the .pyc
files, and let them be automatically regenerated.
You can use this command to delete all.pyc
files:find . -name "*.pyc" -exec rm -f {} ;
– Ollie
Jan 24 at 2:26
add a comment |
I ran into this problem when I checked out an older version of a repository from git. Git replaced my .py
files, but left the untracked .pyc
files. Since the .py
files and .pyc
files were out of sync, the import
command in a .py
file could not find the corresponding module in the .pyc
files.
The solution was simply to delete the .pyc
files, and let them be automatically regenerated.
I ran into this problem when I checked out an older version of a repository from git. Git replaced my .py
files, but left the untracked .pyc
files. Since the .py
files and .pyc
files were out of sync, the import
command in a .py
file could not find the corresponding module in the .pyc
files.
The solution was simply to delete the .pyc
files, and let them be automatically regenerated.
answered Sep 21 '18 at 2:43
craqcraq
4011723
4011723
You can use this command to delete all.pyc
files:find . -name "*.pyc" -exec rm -f {} ;
– Ollie
Jan 24 at 2:26
add a comment |
You can use this command to delete all.pyc
files:find . -name "*.pyc" -exec rm -f {} ;
– Ollie
Jan 24 at 2:26
You can use this command to delete all
.pyc
files: find . -name "*.pyc" -exec rm -f {} ;
– Ollie
Jan 24 at 2:26
You can use this command to delete all
.pyc
files: find . -name "*.pyc" -exec rm -f {} ;
– Ollie
Jan 24 at 2:26
add a comment |
All the above answers are great, but I'd like to chime in here. If you did not spot any issue mentioned above, try clear up your working environment. It worked for me.
add a comment |
All the above answers are great, but I'd like to chime in here. If you did not spot any issue mentioned above, try clear up your working environment. It worked for me.
add a comment |
All the above answers are great, but I'd like to chime in here. If you did not spot any issue mentioned above, try clear up your working environment. It worked for me.
All the above answers are great, but I'd like to chime in here. If you did not spot any issue mentioned above, try clear up your working environment. It worked for me.
answered Jun 6 '18 at 18:21
JianJian
211
211
add a comment |
add a comment |
Not sure how but the below change sorted my issue:
i was having the name of file and import name same for eg i had file name as emoji.py and i was trying to import emoji. But changing the name of file solved the issue .
Hope so it helps
add a comment |
Not sure how but the below change sorted my issue:
i was having the name of file and import name same for eg i had file name as emoji.py and i was trying to import emoji. But changing the name of file solved the issue .
Hope so it helps
add a comment |
Not sure how but the below change sorted my issue:
i was having the name of file and import name same for eg i had file name as emoji.py and i was trying to import emoji. But changing the name of file solved the issue .
Hope so it helps
Not sure how but the below change sorted my issue:
i was having the name of file and import name same for eg i had file name as emoji.py and i was trying to import emoji. But changing the name of file solved the issue .
Hope so it helps
answered Nov 13 '18 at 6:31
MD5MD5
472411
472411
add a comment |
add a comment |
Circular imports cause problems, but Python has ways to mitigate it built-in.
The problem is when you run python a.py
, it runs a.py
but not mark it imported as a module. So in turn a.py
-> imports module b -> imports module a -> imports module b. The last import a no-op since b is currently being imported and Python guards against that. And b is an empty module for now. So when it executes b.hi()
, it can't find anything.
Note that the b.hi()
that got executed is during a.py
-> module b -> module a, not in a.py
directly.
In your specific example, you can just run python -c 'import a'
at top-level, so the first execution of a.py
is registered as importing a module.
add a comment |
Circular imports cause problems, but Python has ways to mitigate it built-in.
The problem is when you run python a.py
, it runs a.py
but not mark it imported as a module. So in turn a.py
-> imports module b -> imports module a -> imports module b. The last import a no-op since b is currently being imported and Python guards against that. And b is an empty module for now. So when it executes b.hi()
, it can't find anything.
Note that the b.hi()
that got executed is during a.py
-> module b -> module a, not in a.py
directly.
In your specific example, you can just run python -c 'import a'
at top-level, so the first execution of a.py
is registered as importing a module.
add a comment |
Circular imports cause problems, but Python has ways to mitigate it built-in.
The problem is when you run python a.py
, it runs a.py
but not mark it imported as a module. So in turn a.py
-> imports module b -> imports module a -> imports module b. The last import a no-op since b is currently being imported and Python guards against that. And b is an empty module for now. So when it executes b.hi()
, it can't find anything.
Note that the b.hi()
that got executed is during a.py
-> module b -> module a, not in a.py
directly.
In your specific example, you can just run python -c 'import a'
at top-level, so the first execution of a.py
is registered as importing a module.
Circular imports cause problems, but Python has ways to mitigate it built-in.
The problem is when you run python a.py
, it runs a.py
but not mark it imported as a module. So in turn a.py
-> imports module b -> imports module a -> imports module b. The last import a no-op since b is currently being imported and Python guards against that. And b is an empty module for now. So when it executes b.hi()
, it can't find anything.
Note that the b.hi()
that got executed is during a.py
-> module b -> module a, not in a.py
directly.
In your specific example, you can just run python -c 'import a'
at top-level, so the first execution of a.py
is registered as importing a module.
answered Jan 9 at 15:12
Hot.PxLHot.PxL
8371925
8371925
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1250103%2fattributeerror-module-object-has-no-attribute%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
13
This is an awful design. WHy does b.py import a when it doesn't reference any part of a.py? Are you asking how to fix this problem? stackoverflow.com/search?q=%5Bpython%5D+circular+dependency
– S.Lott
Aug 8 '09 at 23:40
Note that your questions is very similar to this answer. Apparently the code in this answer works just find, but yours does not? stackoverflow.com/a/7336880/565879
– Buttons840
May 2 '14 at 18:15