3

Suppose I have two files. The first one contains all the functions I've written and which I usually import from my main file:

# my_functions.py

def sqrt_product(a, b):
    from math import sqrt
    return sqrt(a*b)

def create_df(lst):
    from pandas import DataFrame as df
    return df(lst)

and my main file:

# main.py
from my_functions import sqrt_product, create_df

print(sqrt_product(3, 3))
print(create_df([1, 2, 3])

Is there a more efficient way to import this function? Do I have to import every module for every function I create? WHat if I have several functions in the same file that relies on the same module?

mat
  • 2,412
  • 5
  • 31
  • 69
  • [this](http://stackoverflow.com/questions/2349991/python-how-to-import-other-python-files) seems highly relevant. – R Nar Jun 09 '16 at 20:08
  • 1
    What's the objection here? What's inefficient about importing this way? What do you mean by "do I have to import every module for every function I create"? – Two-Bit Alchemist Jun 09 '16 at 20:27
  • 1
    @Two-BitAlchemist my specific concern is with the import... inside every function. Is it the best way to import every module inside the function (in my_functions.py)? – mat Jun 09 '16 at 20:29
  • 1
    @mat Importing individually is the "best" to import functions/class methods because it keep the namespace clean and is memory efficient. – Merlin Jun 09 '16 at 20:33
  • 1
    @mat I don't ever import inside functions, as a rule. I'm not following why you think that's necessary. Your example certainly doesn't demonstrate it. – Two-Bit Alchemist Jun 09 '16 at 20:34
  • 2
    @mat Oh I think I get it now. You can move that `pandas` import to the top of the file and that function will still work no matter where you import it, with no additional imports from `pandas` necessary. – Two-Bit Alchemist Jun 09 '16 at 20:35
  • 1
    @Two-BitAlchemist that was the answer I was looking for! Thank you! – mat Jun 09 '16 at 20:56

4 Answers4

5

This is how.

 import my_functions as repo

Usage:

repo.sqrt_product(a, b)
repo.create_df(lst)

print(repo.sqrt_product(3, 3))
print(repo.create_df([1, 2, 3])

"repo" is now in the namespace. Just like import pandas as pd, pd is in namespace.

# my_functions.py
from math   import sqrt
from pandas import DataFrame as df
#Or import pandas as pd 

def sqrt_product(a, b):
    return sqrt(a*b)

def create_df(lst):
    return df(lst)
    #return pd.DataFrame(lst)
Merlin
  • 24,552
  • 41
  • 131
  • 206
1

You can move the from pandas import DataFrame (optionally with as df) to the top of my_functions.py and redefine create_df just to be:

def create_df(lst):
    return DataFrame(lst)   # or df(lst) if you used as

The create_df function will still work when you import it without requiring you to import anything from pandas. It will be imported with everything it needs to do its thing.

This isn't just true for imported dependencies.

x = 5
def y():
    return x

If you go somewhere else and import y, you will find that y() returns 5, whether or not you imported x. Whatever the function object needs to do its job, it carries with it. This includes when it is imported into another module.

Two-Bit Alchemist
  • 17,966
  • 6
  • 47
  • 82
  • Just by the way, I'm assuming this is a toy example, but if all `create_df` does is pass an argument to `DataFrame`, I'd reconsider having it in the first place and just call `DataFrame`. – Two-Bit Alchemist Jun 09 '16 at 20:42
0

U can do this as well.

# main.py
from my_functions import *

print(sqrt_product(3, 3))
print(create_df([1, 2, 3])
Kozak Poprostu
  • 133
  • 2
  • 9
0

There are a few ways to do this.

  1. Import the file from the current directory.

    • make sure the file you are importing from doesn't have any spaces or dashes in its file name. (Ex: thing_to_import_from.py, not thing to import from.py.)
    • make sure both files are in the same directory.

      Import the file and function like so:

      from thing_to_import_from import function_to_import1, function_to_import2
      
  2. Import the file from another directory.

    • If the file is in a folder or subfolder within the same directory, and the path contains no spaces, it can be imported like so:

      from folder/subfolder/thing_folder/thing_to_import_from import function_to_import
      
    • else, add the path to the python sys.path, then import as before.

      import sys
      sys.path.insert(0, "/folder/subfolder/thing folder")
      from thing_to_import_from import function_to_import
      
  3. Make the file into a python module.

    • You should ONLY do this if you intend to use the file with multiple programs for an extended period of time. Don't junk up your Lib folder!

      1. Make a folder in your Lib folder with the desired library name. (The default location of Lib for Python2.7 on Windows is C:\Python27\Lib, for example.)
      2. Place your script with the functions to import within this folder.
      3. Create an __init__.py script within this folder. If your script doesn't need any special initialization and doesn't import any other non-library scripts, this can be a blank file.
      4. Import your script as a module!

        from thing_to_import_from import function_to_import
        
Triggernometry
  • 573
  • 2
  • 9