Not able to import custom module in Python

How can I make my python file a module that can be imported from any where without specifying that path when not in the CD.

@gururajankappa Hello, do you have an example of your code so we can see what the issue is?

/utility/mathFunctions.py

def multiply(a, b):
    return a * b
def add(a, b):
    return a + b

/home/main.py

from utility.mathFunction import add
add(a, b)

Instead of having to import mathFunctions.py like I did.
I want to import it like I import normal modules
Like import tkinter
import math
etc.
Just from mathFunction import add
How can I do that.
Thanks

You will not be able to import from anywhere. Make sure that you make a separate folder to store each project and you can import like this.

myproject/

myproject/main.py

myproject/math_functions.py

In main.py

from math_functions import add_two_numbers

add_two_numbers(10, 20)

Or you can create a Python package.

myproject/

myproject/main.py

myproject/math_functions/__init__.py

myproject/math_functions/addition.py

In main.py

from math_function.addition import add_two_numbers

add_two_numbers(11, 22)

Here is for info on how to import modules.

Thanks!

some more characters

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.