Python 如何为导入的所有其他模块导入一个模块?

Python 如何为导入的所有其他模块导入一个模块?,python,python-2.7,Python,Python 2.7,我已经尝试解决这个问题好几个小时了,但没有成功,我有PHP背景,它可以在那里工作,但不能与python一起工作 假设我有一个名为Main.py的文件: import time import myfunction myfunction.Calculate() myfunction.py类似于: def Calculate() print('Thank you') time.sleep(1) 当我运行Main.py时,它会崩溃,说“我的函数中没有定义时间”,但它甚至在导入我的函数之前就

我已经尝试解决这个问题好几个小时了,但没有成功,我有PHP背景,它可以在那里工作,但不能与python一起工作

假设我有一个名为Main.py的文件:

import time
import myfunction

myfunction.Calculate()
myfunction.py类似于:

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

当我运行Main.py时,它会崩溃,说“我的函数中没有定义时间”,但它甚至在导入我的函数之前就定义了,为什么它不工作?

最简单的解决方案是在
myfunction.py
文件中导入
time
。在实际使用的地方导入物品:

myfunction.py

import time

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

myfunction.Calculate()
main.py

import time

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

myfunction.Calculate()

最简单的解决方案是在
myfunction.py
文件中导入
time
。在实际使用的地方导入物品:

myfunction.py

import time

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

myfunction.Calculate()
main.py

import time

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

myfunction.Calculate()

每个文件都有自己的名称空间。每个文件都有自己的名称空间。非常感谢,它真的很有效。我只有最后一个问题,它是如何工作的?。我的意思是,我只是在执行Calculate()函数,它应该是该函数中唯一运行的代码,不是吗?非常感谢,它真的很有效。我只有最后一个问题,它是如何工作的?。我的意思是,我只是在执行Calculate()函数,它被认为唯一运行的代码就是该函数中的代码,不是吗?