module has no attribute
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
AttributeError: 'module' object has no attribute
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.)
AttributeError: module 'MyModule' has no attribute 'Module1'
This is expected because by default submodules are not imported.
You should import them like so to use them:
import MyModule.Module1
To change this you have to tweak the MyModule/__init__.py
file by adding:
import MyModule.Module1
This way, MyModule.Module1
will be available when you import MyModule
, as the __init__.py
file is executed.
module has no attribute 'func'
You might want to name your module something else, since test
is a standard Python module, and apparently you are importing that instead of your own test.py
file.
module has no attribute even though the module actually contains the script
Edit: I changed my answer after better understanding the problem.
Your problem comes from circular imports (see this tutorial for instance): your files settings_centroid.py
and gizmo_read/read.py
both include each other.
When you import settings_centroid.py
, it imports reads.py
that directly runs settings_centroid.init()
, but at that point, Python didn't load all the symbols that are within settings_centroid.py
, hence it doesn't find init()
.
Circular imports bring tricky problems to solve.
My advice would be to refactor your code to avoid them, which could take a bit of time if your codebase is already big.
One option, that may not make sense with the logic of your whole code, (if it doesn't, sorry you'll have to think this through) if settings_centroid.py
is something of a helper class, is to make a sub-package with it and try to limit its dependency on other modules.
If you really can't refactor, you can try to limit your imports to function scopes.
For instance, settings_centroid.py
could become
import utilities as ut
import gizmo_analysis
import rockstar_analysis
# import gizmo_read <-- important, remove this import
def init():
import gizmo_read # <-- do the import here
global h, omega_m, omega_l, part, species, properties
species, properties = 'all', 'all'
part=gizmo_read.read.ReadClass.read_snapshot(species, properties, directory='./output/')
But I'm pretty sure you'll have other similar problems down the line.
Hope it helps :)
Related Topics
Python - How to Check List Monotonicity
Saving the State of a Program to Allow It to Be Resumed
Django Rest Framework Serializing Many to Many Field
How to Compare Two JSON Objects with the Same Elements in a Different Order Equal
Is There a Simple Way to Change a Column of Yes/No to 1/0 in a Pandas Dataframe
Why Is Exponentiation Applied Right to Left
How Does Python Importing Exactly Work
Oserror: [Winerror 193] %1 Is Not a Valid Win32 Application
<Django Object > Is Not JSON Serializable
How to Show Explosion Image When Collision Happens
Why Do Many Examples Use 'Fig, Ax = Plt.Subplots()' in Matplotlib/Pyplot/Python
Python - Download Images from Google Image Search
Python Unittest.Testcase Execution Order
Python: One Try Multiple Except
Python: How to Send Mail with To, Cc and Bcc