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.)
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.
The problem is submodules are not automatically imported. You have to explicitly import the api module:
import myproject.mymodule.api
print myproject.mymodule.api.MyClass
If you really insist on api being available when importing myproject.mymodule you can put this in myproject/mymodule/__init__.py:
import myproject.mymodule.api
Then this will work as expected:
from myproject import mymodule
print mymodule.api.MyClass
Also check whether you didn't name your python file the same as the module you are trying to import.
package - Python error: AttributeError: 'module' object has no attribute - Stack Overflow
"Module ... has no attribute ..."
Import Client Module Error - AttributeError: ‘module’ object has no attribute - Talk Python
How to fix AttributeError: module has no attribute
Videos
When you import lib, you're importing the package. The only file to get evaluated and run in this case is the 0 byte __init__.py in the lib directory.
If you want access to your function, you can do something like this from lib.mod1 import mod1 and then run the mod12 function like so mod1.mod12().
If you want to be able to access mod1 when you import lib, you need to put an import mod1 inside the __init__.py file inside the lib directory.
More accurately, your mod1 and lib directories are not modules, they are packages. The file mod11.py is a module.
Python does not automatically import subpackages or modules. You have to explicitly do it, or "cheat" by adding import statements in the initializers.
>>> import lib
>>> dir(lib)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']
>>> import lib.pkg1
>>> import lib.pkg1.mod11
>>> lib.pkg1.mod11.mod12()
mod12
An alternative is to use the from syntax to "pull" a module from a package into you scripts namespace.
>>> from lib.pkg1 import mod11
Then reference the function as simply mod11.mod12().
I'm writing python in vscode. I have 2 py files (main and converters).
Converters:
def lbstokg(weight):return weight*0.45
def kgtolbs(weight):return weight / 0.45
Main:
import convertersprint(converters.lbstokg(10))
Error when running: AttributeError: module 'converters' has no attribute 'lbstokg'
Why? It's in the same folder, it knows that the module is there because it's in the dropdown list after typing converters.. The code works in pycharm but doesn't in vscode.