Python 为什么子级必须导入与父级相同的模块?

Python 为什么子级必须导入与父级相同的模块?,python,python-import,Python,Python Import,我的主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

我的主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
当我的程序/脚本接近5000行时,我转向光明面/(黑暗面?),开始使用我自己设计的导入模块。第一个模块称为
location.py
but!,瞧,我不得不重复已经在父程序
mserve
中导入的导入语句

例如,在标题处:

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已经过时之前,请注意我使用的是Ubuntu16.04,其EOL为2021。还要注意的是,我们在工作中使用Windows2008Server,用COBOL编写的遗留系统很常见,所以谁在乎呢

我一定是做错了什么,因为导入的模块不必导入父模块已经做过的事情?在正常的环境中,孩子应该知道父母已经知道的事情

另一方面,今晚的新课程“MsgDisplay”应该在parent中,而不是child中。将类放在子类中比计算子类如何调用父类更简单

我一定是做错了什么,因为导入的模块不应该有>来导入父级已经做过的事情?在正常环境中,孩子应该知道父母已经知道的


您正在描述一个类似于cpp的include系统,其中声明仅按顺序放置在单个翻译单元中。这不适用于python。在您使用的任何地方导入相同的模块都是必要的。将导入视为将模块内容绑定到本地命名空间。如果您在模块B中使用模块A内容,则需要在模块B中导入该内容,即使模块C已经导入A和更高版本的B。不要太担心性能。一旦python解释器加载了一个模块,其余的导入就不是很昂贵了。

如果
from\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu语句的导入实际上对您有所帮助,那么您使用的是一些疯狂的老python版本-
with
在python 2.6中默认可用。唯一需要导入的系列是Python2.5,在此之前,
with
根本不存在"? 您正在执行子流程吗?使用
多处理.Process
?我认为平台在这一点上很重要,您是在类似unix的forking系统还是Windows execute系统上?每个模块都有自己的名称空间,其中需要的东西必须从其他模块/包(或新创建的)导入。这是正常的。哦,等等,“子”是否只是一个导入的模块,与父模块处于相同的过程中?一般来说,一个模块应该导入它需要直接访问的模块,而不应该导入它不需要触摸的任何东西。如果模块已经导入,则不会再次导入,因此不会造成实际伤害。导入语句记录模块使用的内容。您可以使用一个特殊模块导入所有内容,然后执行类似于从masterimporter导入的
*
,但给定模块实际需要导入多少个模块?