Search code examples
pythonmodulepackagesyntax-error

Indentation error while creating a package


I'm trying to create a simple package like this:

areas
    __init__.py
    circunferencia.py
    rectangulo.py

I saved this file as areas.py. circunferencia.py cointains this:

PI=3.1415
def area(r):
    return PI*r**2

And rectangulo:

def area(a):
    return a*a

I create another .py:

import areas
print(areas.circunferencia.area(3))
print(areas.rectangulo.area(2))

And when I try to run my package it gives me this error

    __init__.py
    ^
IndentationError: unexpected indent

I don't understand what is wrong with the code, can you help me? Thank you!


Solution

  • If you want to create a package. On your current project directory for example (/home/jack/pyproject) you can create a folder called area then put all the python files inside the folder, with file __init__.py inside of it so you can import the package when working on (/home/jack/pyproject).

    If you want to import the package from another directory, then you can add the path of /home/jack/pyproject into the PYTHONPATH environment variable. therefore you do not need to add it in your site-package.