How to import a module for all others modules imported?

I have been trying to solve this problem for some hours without any success, I have a PHP background and it will work there but not with python

Let's suppose I have a file named Main.py:

import time
import myfunction

myfunction.Calculate()

And myfunction.py is something like:

def Calculate()
  print('Thank you')
  time.sleep(1)

When I run Main.py, it will crash saying 'time is not defined in my function', but it was defined even before importing my_function, why it does not work?

Upvotes: 0

Views: 34

Answers (1)

Netwave
Netwave

Reputation: 42678

The simplest solution is to import time in your myfunction.py file. Import things where they are really used:

myfunction.py

import time

def Calculate()
  print('Thank you')
  time.sleep(1)

main.py

import myfunction

myfunction.Calculate()

Upvotes: 2

Related Questions