为什么 child 必须导入与 parent 相同的模块?
Why does child have to import same modules parent did?
我的主要 Python 程序(大多数脚本)有详细的导入语句,我不想在我导入的模块中重复:
from __future__ import print_function # Must be first import
from __future__ import with_statement # Error handling for file opens
try:
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.font as font
import tkinter.filedialog as filedialog
import tkinter.messagebox as messagebox
PYTHON_VER="3"
except ImportError: # Python 2
import Tkinter as tk
import ttk
import tkFont as font
import tkFileDialog as filedialog
import tkMessageBox as messagebox
PYTHON_VER="2"
# print ("Python version: ", PYTHON_VER)
import subprocess32 as sp
import sys
import os
import time
import datetime
from PIL import Image, ImageTk, ImageDraw, ImageFont
import pickle
from random import shuffle
import getpass # Get user name for file storage
import locale # To set thousands separator as , or .
locale.setlocale(locale.LC_ALL, '') # Use '' for auto
# mserve modules
import location as lc # Home grown module
当我的 program/script 接近 5,000 行时,我转向光明面/(黑暗面?)并开始使用我自己设计的导入模块。第一个模块称为 location.py
但是!,你瞧,我不得不重复已在 parent 程序 mserve
.
中导入的导入语句
EG 在 header:
from __future__ import print_function # Must be first import
import getpass
import os
import pickle
import time
就在今晚,我正在编写一个新函数:
import Tkinter as tk
class MsgDisplay:
''' Text Widget with status messages
'''
def __init__(self, title, toplevel, width, height):
self.line_cnt = 0 # Message lines displayed so far
toplevel.update_idletasks() # Get up-to-date window co-ords
x = toplevel.winfo_x()
y = toplevel.winfo_y()
w = toplevel.winfo_width()
h = toplevel.winfo_height()
xd = (w/2) - (width/2)
yd = (h/2) - (height/2)
print('x:',x,'y:',y,'w:',w,'h:',h,
'width:',width,'height:',height,'xd:',xd,'yd:',yd)
''' Mount message textbox window at centered in passed toplevel '''
self.msg_top = tk.Toplevel()
self.textbox = tk.Text(self.msg_top, height=height, width=width)
self.msg_top.geometry('%dx%d+%d+%d'%(width, height, x + xd, y + yd))
# self.textbox.columnconfigure(0, weight=1)
# self.textbox.rowconfigure(0, weight=1)
self.textbox.pack()
self.textbox.insert(tk.END, "Just a text Widget\nin two lines\n")
def Update(self, msg_list):
self.textbox.insert(tk.END, "Just a text Widget\nin two lines\n")
time.sleep(.1)
def Close(self):
self.msg_top.destroy()
我刚刚添加的新导入:
import Tkinter as tk
是一个捷径/胡说八道,因为生产版本需要:
try:
import tkinter as tk
except ImportError: # Python 2
import Tkinter as tk
布道之前 python 2.7.12 已过时 请注意我使用的是 Ubuntu 16.04,其 EOL 是 2021。另请注意,我们在工作中使用 Windows 2008 服务器和遗留系统用 COBOL 编写的很常见,所以谁在乎?
我一定是做错了什么,因为导入的模块不应该导入 parent 已经导入的模块?在正常环境中,child 应该知道/固有 parent 已经知道的内容。
顺便说一句,今晚的新 class“MsgDisplay”应该在 parent 而不是 child。将 class 放在 child 中比弄清楚 child 如何调用 parent class.
更简单
I must be doing something wrong because a module that is imported should not have > to import what parent already did? In a normal environment the child should know > inherent what the parent already knows.
您描述的是一个 cpp-like 包含系统,其中的声明只是按顺序放置在单个翻译单元中。这不适用于 python。在你使用它的任何地方导入相同的模块是必要的。将导入视为将模块内容绑定到本地命名空间。如果您在模块 B 中使用模块 A 的内容,即使模块 C 已经导入 A 和稍后的 B,您也需要将其导入 B。不要太担心性能。一旦一个模块被 python 解释器加载,剩下的导入就不是很昂贵了。
我的主要 Python 程序(大多数脚本)有详细的导入语句,我不想在我导入的模块中重复:
from __future__ import print_function # Must be first import
from __future__ import with_statement # Error handling for file opens
try:
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.font as font
import tkinter.filedialog as filedialog
import tkinter.messagebox as messagebox
PYTHON_VER="3"
except ImportError: # Python 2
import Tkinter as tk
import ttk
import tkFont as font
import tkFileDialog as filedialog
import tkMessageBox as messagebox
PYTHON_VER="2"
# print ("Python version: ", PYTHON_VER)
import subprocess32 as sp
import sys
import os
import time
import datetime
from PIL import Image, ImageTk, ImageDraw, ImageFont
import pickle
from random import shuffle
import getpass # Get user name for file storage
import locale # To set thousands separator as , or .
locale.setlocale(locale.LC_ALL, '') # Use '' for auto
# mserve modules
import location as lc # Home grown module
当我的 program/script 接近 5,000 行时,我转向光明面/(黑暗面?)并开始使用我自己设计的导入模块。第一个模块称为 location.py
但是!,你瞧,我不得不重复已在 parent 程序 mserve
.
EG 在 header:
from __future__ import print_function # Must be first import
import getpass
import os
import pickle
import time
就在今晚,我正在编写一个新函数:
import Tkinter as tk
class MsgDisplay:
''' Text Widget with status messages
'''
def __init__(self, title, toplevel, width, height):
self.line_cnt = 0 # Message lines displayed so far
toplevel.update_idletasks() # Get up-to-date window co-ords
x = toplevel.winfo_x()
y = toplevel.winfo_y()
w = toplevel.winfo_width()
h = toplevel.winfo_height()
xd = (w/2) - (width/2)
yd = (h/2) - (height/2)
print('x:',x,'y:',y,'w:',w,'h:',h,
'width:',width,'height:',height,'xd:',xd,'yd:',yd)
''' Mount message textbox window at centered in passed toplevel '''
self.msg_top = tk.Toplevel()
self.textbox = tk.Text(self.msg_top, height=height, width=width)
self.msg_top.geometry('%dx%d+%d+%d'%(width, height, x + xd, y + yd))
# self.textbox.columnconfigure(0, weight=1)
# self.textbox.rowconfigure(0, weight=1)
self.textbox.pack()
self.textbox.insert(tk.END, "Just a text Widget\nin two lines\n")
def Update(self, msg_list):
self.textbox.insert(tk.END, "Just a text Widget\nin two lines\n")
time.sleep(.1)
def Close(self):
self.msg_top.destroy()
我刚刚添加的新导入:
import Tkinter as tk
是一个捷径/胡说八道,因为生产版本需要:
try:
import tkinter as tk
except ImportError: # Python 2
import Tkinter as tk
布道之前 python 2.7.12 已过时 请注意我使用的是 Ubuntu 16.04,其 EOL 是 2021。另请注意,我们在工作中使用 Windows 2008 服务器和遗留系统用 COBOL 编写的很常见,所以谁在乎?
我一定是做错了什么,因为导入的模块不应该导入 parent 已经导入的模块?在正常环境中,child 应该知道/固有 parent 已经知道的内容。
顺便说一句,今晚的新 class“MsgDisplay”应该在 parent 而不是 child。将 class 放在 child 中比弄清楚 child 如何调用 parent class.
更简单I must be doing something wrong because a module that is imported should not have > to import what parent already did? In a normal environment the child should know > inherent what the parent already knows.
您描述的是一个 cpp-like 包含系统,其中的声明只是按顺序放置在单个翻译单元中。这不适用于 python。在你使用它的任何地方导入相同的模块是必要的。将导入视为将模块内容绑定到本地命名空间。如果您在模块 B 中使用模块 A 的内容,即使模块 C 已经导入 A 和稍后的 B,您也需要将其导入 B。不要太担心性能。一旦一个模块被 python 解释器加载,剩下的导入就不是很昂贵了。