Arunima Khanna
Arunima Khanna

Reputation: 11

Why am I not able to import modules from package?

I have created a package named math. Inside this package I have created 2 files:

And outside the package math is my file main.py.

__init__.py looks like this:

a=1 

This is the contents of main.py:

import math
print(math.a)

The output should be 1, but I am getting no output.

Even in case of main.py:

from math import simple
print(simple.add(1,2))

I am getting the error as: cannot import name simple.

Can someone tell where I am going wrong?

Upvotes: 0

Views: 73

Answers (1)

Derlin
Derlin

Reputation: 9891

There are three leads to this problem.

  1. init.py should be called __init__.py,
  2. math is also the name of a builtin package. Good chances are it is loaded instead of your package, and doesn't define a,
  3. In order for your package to be discoverable, ensure that the root directory where math is defined is in your pythonpath (PYTHONPATH environment variable).

Upvotes: 1

Related Questions