Fixing Python Sympy AttributeError: ‘Mul’ object has no attribute ‘cos’ in Python

Python Sympy AttributeError: ‘Mul’ object has no attribute ‘cos’ in Python is an error which occurs because of a namespace clash when importing numpy.

In this blog post I attempt to present a clear explanation of why this error takes place and how you to solve it, I am also going to explain other ways to solve the error if possible.

Exploring the Python Sympy AttributeError: ‘Mul’ object has no attribute ‘cos’ in Python

This is an error which occurs because of a namespace clash when importing numpy.

Bellow is the error message, please make sure it is the right one.

                                                                       #
AttributeError: 'Mul' object has no attribute 'cos' 
                                                                       #

In the section bellow we will explain the root of the error more and propose a possible fixe.

The Method which solved my issue : Get rid of the namespace clash between libraries

The message of the error tells it all, ‘Mul’ object has no attribute ‘cos’.

This tells me that you are using sympy in your code since the Mul() method is used in sympy like this sympy.Mul().

The error you are seeing must be caused by a namespace clash. In order t solve the issue, you should not import numpy and sympy like this

                                                                       #
from numpy import *
from sympy import *
                                                                       #

But you should avoid using * and import the modules like this, since each one has a definition for cos and that is exactly why the error takes place.

                                                                       #
import numpy as numpy
import sympy as sympy
                                                                       #

You do not have to use the exact names of the modules, for instance you can import numpy like this

                                                                       #
import numpy as np
# or : import numpy as npA etc.
                                                                       #

I believe the fix above should be enough to remove the error for good for most developers.

I hope the fixes above fixed your problem. Thank you for reading the whole article, good luck with your projects.

Summing-up : 

Here we are at the end of the road, at the end of this article, if you solved the error : Python Sympy AttributeError Mul object has no attribute cos in Python congrats, this was a confusing error for me the first time I encountered it.

Thank you for reading my blog post to the end, If you want to learn more about the Python programming language, check out the official Python Documentation : https://docs.python.org/3/