In this Python Tutorial we will explain how you can install the SymPy Library on your device.
SymPy is a powerful library in Python used for a variety of mathematical operations, such as symbolic computation, creating and representing expressions, solving expressions, differentials and integrals, etc.
Installing SymPy in Python
Before we can use any SymPy code in our Python Program, we need to download and install it. We can do this using pip
or any equivalent method. Let’s discuss a few of these methods here.
Note: If you are an Anaconda user, you will already have the SymPy library pre-installed, along with a bunch other computing libraries like numpy.
pip
The first, and most common method is by using pip
. This should work almost universally on all major operating systems. Just use the following command in the command prompt or terminal that you are using.
pip install sympy
GitHub Repository
If you wish to contribute to SymPy or you want to easily get access to the newest updates, then install SymPy from git. In order to download the repository, you need to execute the following command from the command prompt or terminal:
git clone https://github.com/sympy/sympy.git
To update to the latest version of SymPy, execute the following:
git pull origin master
The Mpmath dependency
There’s a chance that you might run into an error regarding the mpmath
library when using SymPy. This is because prior to version 1.0, this library was included within SymPy. Now it’s an external dependency.
If you run into any errors regarding this library, then run the following command to install it.
pip install mpmath
Importing and Running SymPy
Now that we have SymPy setup, let’s import it and run some code. If the following snippet runs without any errors, that means the installation was successful.
import sympy
from sympy import symbols
x, y = symbols("x y")
expr = 2*x + 4*y
print(expr)
2x + 4y
Are you interested in learning more about SymPy and Symbolic Computation? Check out our SymPy tutorial!
This marks the end of the “How to install SymPy Library in Python” Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.