Python: Problem Importing Function From Another Module
I have a module called nbemail.py and in this module I want to use the function package_post defined in the module main.py. I am using this statement:
from api.main import package_post
But I am getting this error:
ImportError: cannot import name package_post
I really don't know why I am getting this error! I do have _init_.py files in the api directory (which contains the files nbemail.py and main.py) and 开发者_如何学运维I do have the function package_post defined in main.py.
Any idea to help fixing this problem?
I suspect you have a circular dependency error. Do you import nbemail
from main
as well? If so, Python won't be able to resolve the dependency. The best way to fix this is to move one of the imports into a function, so it doesn't happen when the module is first imported.
If nbemail.py and main.py are in the same folder (as i understood in your question), just do this in nbemail.py :
from main import package_post
精彩评论