How to Import math.pi in Python

Import π by loading Python’s built-in math module, then using math.pi. You do not need to install anything: math is part of Python’s Standard Library. This guide shows the recommended import, direct-import alternatives, a practical circle calculation, and the common errors that prevent math.pi from working.

What math.pi Is in Python

math.pi is Python’s floating-point representation of the mathematical constant π, the ratio of a circle’s circumference to its diameter. It is more precise than typing a short approximation such as 3.14.

The name has two parts:

  • math is the module containing mathematical functions and constants.
  • pi is the constant inside that module.

Python uses dot notation to access a member of a module:

math.pi

math.pi is a float, not a special built-in number type. Printing it normally displays:

3.141592653589793

Using the named constant makes the source of your value clear and avoids manually hard-coding a less precise approximation.

How to Import math.pi

Step 1: Import the math Module

  1. Import the standard module before any code that uses π:
import math
  1. Do not run pip install math: the module already ships with Python. The module already ships with Python.

This import makes the module available under the name math.

Step 2: Access Pi With Dot Notation

  1. After importing the module, use math.pi wherever your calculation needs π:
import math
print(math.pi)
  1. With import math, write math.pi rather than pi by itself. It tells Python to look for pi inside the math module.

The program prints:

3.141592653589793

Step 3: Use Pi in Your Calculation

  1. Replace a typed approximation such as 3.14 with math.pi.
  2. Run the script normally after saving it.

For example, the circumference of a circle is 2 × π × radius:

import math
radius = 5
circumference = 2 * math.pi * radius
print(circumference)

With a radius of 5, this prints:

31.41592653589793

Other Ways to Import Pi

The standard import math form is usually the clearest choice, especially when a file uses several math functions. Python also supports more targeted imports.

Import Pi Directly

Use this when your code needs only π and you prefer to write pi without the math. prefix.

from math import pi
print(pi)

This imports the value into your current namespace. The following will work:

from math import pi
radius = 5
area = pi * radius ** 2
print(area)

The trade-off is that readers cannot immediately see where pi came from when looking at a later line of code.

Import Several Math Names Directly

You can import π alongside the functions your code needs:

from math import pi, sqrt
radius = 5
diagonal = sqrt(2) * radius
print(pi)
print(diagonal)

This is useful in a small script, but use it deliberately. A local variable or another import named pi can overwrite the imported name.

Use a Module Alias

You can give math a shorter name:

import math as m
print(m.pi)

For such a short module name, import math is generally easier to read than import math as m. Aliases are more useful for modules with long names or established conventions.

Avoid Wildcard Imports

Avoid this form:

from math import *

It makes pi, sqrt, sin, and many other names appear without showing their source. It also increases the chance that a name from another import or variable assignment will collide with a math name.

Use math.pi in Common Formulas

Python’s math module is especially useful for geometry, trigonometry, logarithms, rounding, and square roots. Angles passed to trigonometric functions such as math.sin() and math.cos() are measured in radians.

Here is a circle-area example:

import math
radius = 5
area = math.pi * radius ** 2
print(area)

Here is a trigonometry example using π radians:

import math
print(math.sin(math.pi / 2))
print(math.cos(math.pi))

You can also convert degrees to radians before using a trigonometric function:

import math
angle_in_degrees = 90
angle_in_radians = math.radians(angle_in_degrees)
print(math.sin(angle_in_radians))

Use math.pi when the value is part of a mathematical formula. You do not need the math module for ordinary addition, subtraction, multiplication, division, or exponentiation with **.

Fix Errors When Importing Pi

NameError: name 'math' is not defined

This error means Python reached math.pi before the module was imported.

Incorrect code:

print(math.pi)

Correct code:

import math
print(math.pi)

Make sure import math appears earlier in the same file or has run earlier in the current interactive session.

NameError: name 'pi' is not defined

This usually happens when you use pi by itself after importing the whole module.

Incorrect code:

import math
print(pi)

Use math.pi after import math, or import pi directly with from math import pi.

Use the module prefix:

import math
print(math.pi)

Or import the constant directly:

from math import pi
print(pi)

AttributeError or a Partially Initialized math Module

A file in your project named math.py can shadow Python’s real math module. Python may import your local file instead, so math.pi is missing or the import fails with an error about a partially initialized module.

Check the filename of your script and the current project folder. Rename any of these if they are part of your project:

  • math.py
  • a math folder
  • an old compiled file associated with a renamed math.py file

Then restart the Python interpreter or rerun the script so Python does not keep using the old import result.

I use a Python virtual environment when testing imports because it keeps unrelated project dependencies out of the check.

ModuleNotFoundError: No module named 'math'

In a normal CPython installation, math is included with Python and should import directly. If this error occurs, first confirm that you are running Python rather than a different tool, file runner, or restricted environment.

A local file or folder that interferes with imports is still the most likely project-level cause. If import math succeeds but math.pi fails, check which module Python imported:

import math
print(math.__file__)

On many systems, the built-in math module may not have a normal project-file path. If the displayed path points into your project directory, rename the conflicting file or folder and restart Python.

Confirm the Import Works

Use this short check in a new file or Python session:

import math
print(math.pi)

A successful import prints a value beginning with:

3.141592653589793

You can also verify that π behaves as a float:

import math
print(type(math.pi))

The result should be:

<class 'float'>

For a project check, test the actual formula your code needs rather than only printing π. A circle formula, trigonometric calculation, or conversion from degrees to radians confirms both the import and your intended usage.

Frequently Asked Questions

How do I import pi into Python?

Import the module with import math, then write math.pi in your code. If you want to write pi without the prefix, use from math import pi instead.

from math import pi
print(pi)

What does from math import pi mean?

from math import pi copies the pi name from the math module into the current file or interactive session. After that import, use pi directly rather than math.pi.

from math import pi
print(pi)

Is 3.14 a float in Python?

Yes. Writing 3.14 creates a Python float, but it is only a short approximation of π. math.pi is also a float and normally displays as 3.141592653589793, giving you Python’s available value for the constant.

What is import math?

import math loads Python’s standard math module and makes it available under the name math. The module includes constants such as math.pi and math.e, plus functions such as math.sqrt(), math.sin(), math.floor(), and math.radians().

Why does math.pi not work in my script?

The common causes are forgetting import math, writing pi when you imported only math, or naming your own script math.py. Put import math before the calculation and make sure no local file or folder shadows the standard module.