0

I wrote a function_timer() to time the runtime of programs and I want to use the function when writing code across different directories when working on other programs. How do I make it such that importing the function is simplified across different directories?

Xuan
  • 101
  • 9
  • Welcome to SO. Please take the time to read [ask] and the links it contains. – wwii Oct 26 '17 at 18:05
  • Possible duplicate of [Importing files from different folder](https://stackoverflow.com/q/4383571/2823755) – wwii Oct 26 '17 at 18:07

1 Answers1

1

What you are trying to do is to create a python module. A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py

create timer.py then write the following function as its content:

def mytimer():
   #your timer code here

Then you can import hello as long as the file location is accessible from where the calling program is working

import timer

timer.mytimer()

To group many .py files put them in a folder. Any folder with an init.py is considered a module by python and you can call them a package

|-TimerModule
  |_ __init__.py
  |_ timer.py
Paula Livingstone
  • 1,175
  • 1
  • 12
  • 21