将一个模块调用到另一个模块中

时间:2014-11-02 11:40:14

标签: python python-2.7

我有两个名为main.py和black.py的python模块:

Main.py内容如下:

import os,sys,string,time
import subprocess,commands,re
import random
import black
class bcolors:
    BOLD = '\033[1m'
    UNBOLD = '033[0m'
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
def disable(self):
    self.BOLD = ''
    self.UNBOLD = ''
    self.HEADER = ''
    self.OKBLUE = ''
    self.OKGREEN = ''
    self.WARNING = ''
    self.FAIL = ''
    self.ENDC = ''
def print_menu():
        #subprocess.call("tput clear")
        print " "
        #print bcolors.OKBLUE + (35 * '-') + bcolors.OKBLUE
        print bcolors.OKBLUE + '\t\t\t\t' + (52 * '*') + bcolors.OKBLUE
        print ("\t\t\t\t\t\tM A I N - M E N U")
        print '\t\t\t\t' + (52 * '*')
        print ("\t\t\t\t 0. Enter POD Name(s).")
        print ("\t\t\t\t 1. QUIT")
        print '\t\t\t\t' + (52 * '*') + bcolors.ENDC
        is_valid=0
        while not is_valid:
                try :
                        choice = int ( raw_input('Enter your choice [0-24] : ') )
                        is_valid = 1
                except ValueError, e :
                        print ("'%s' is not a valid integer." % e.args[0].split(": ")[1])
        return choice
########## M A I N #####################
rUser=commands.getoutput("id -un").strip()
if rUser != "oracle":
        print bcolors.FAIL + "  Login as mc_admin to run the script. Exiting .." + bcolors.ENDC
        exit ()
else:
        os.system("tput clear")
        choice = print_menu()
###################
global podList    # 
podList = None    #
###################
while choice >= 0 and choice < 1:
        if choice == 0:
                pPODName()
black.blackout()

和black.py内容如下:

def blackout():
    def pPODName():
        global podList
        podList = str(raw_input('Enter pipe separated list of PODS : ')).upper().strip()
        if podList:
            try:
                cmd = "/fsnadmin/f516478/romeshar/myfolder/PodDetails.sh -p " + "\"" + podList + "\""
                prodP = commands.getoutput(cmd).strip()
                if prodP:
                    print bcolors.FAIL + "Production Pods [Status: Customer Active|Dev Post Provisioning|Cloud Ops Provisioning]: \n" + bcolors.OKBLUE + prodP + bcolors.ENDC
                cmd = "/fsnadmin/f516478/romeshar/myfolder/PodDetails.sh -t " + "\"" + podList + "\""
                stagP = commands.getoutput(cmd).strip()
                if stagP:
                    print bcolors.FAIL + "Stage/Test Pods [Status: Customer Active|Dev Post Provisioning|Cloud Ops Provisioning]: \n" + bcolors.OKBLUE + stagP + bcolors.ENDC
                cmd = "/fsnadmin/f516478/romeshar/myfolder/PodDetails.sh -d " + "\"" + podList + "\""
                devP = commands.getoutput(cmd).strip()
                if devP:
                    print bcolors.FAIL + "Dev/Upgrade Pods [Status: Customer Active|Dev Post Provisioning|Cloud Ops Provisioning]: \n" + bcolors.OKBLUE + devP + bcolors.ENDC
                cmd = "/fsnadmin/f516478/romeshar/myfolder/PodDetails.sh -i " + "\"" + podList + "\""
                intstgP = commands.getoutput(cmd).strip()
                if intstgP:
                    print bcolors.FAIL + "Internal Staging Pods [Status: Customer Active|Dev Post Provisioning|Cloud Ops Provisioning]: \n" + bcolors.OKBLUE + intstgP + bcolors.ENDC
                cmd = "/fsnadmin/f516478/romeshar/myfolder/PodDetails.sh -r " + "\"" + podList + "\""
                prtnP = commands.getoutput(cmd).strip()
                if prtnP:
                    print bcolors.FAIL + "Partner Pods [Status: Customer Active|Dev Post Provisioning|Cloud Ops Provisioning]: \n" + bcolors.OKBLUE + prtnP + bcolors.ENDC
                cmd = "/fsnadmin/f516478/romeshar/myfolder/PodDetails.sh -s " + "\"" + podList + "\""
                stbyP = commands.getoutput(cmd).strip()
                if stbyP:
                    print bcolors.FAIL + "DR/Standby Pods [Status: Customer Active|Dev Post Provisioning|Cloud Ops Provisioning]: \n" + bcolors.OKBLUE + stbyP + bcolors.ENDC
                cmd = "/fsnadmin/f516478/romeshar/myfolder/PodDetails.sh -u " + "\"" + podList + "\""
                unalloP = commands.getoutput(cmd).strip()
                if unalloP:
                    print bcolors.FAIL + "Unallocated Pods [Status: Dev Post Provisioning|Cloud Ops Provisioning]: \n" + bcolors.OKBLUE + unalloP + bcolors.ENDC
                cmd = "/fsnadmin/f516478/romeshar/myfolder/PodDetails.sh -l " + "\"" + podList + "\""
                trlP = commands.getoutput(cmd).strip()
                if trlP:
                    print bcolors.FAIL + "Trial Pods [Status: Customer Active|Dev Post Provisioning|Cloud Ops Provisioning]: \n" + bcolors.OKBLUE + trlP + bcolors.ENDC
            except OSError:
                print bcolors.FAIL + "Could not invoke Pod Details Script. " + bcolors.ENDC
                podList = vPodName(podList)
            print bcolors.FAIL + "\nYou Have Entered PodList as: " + podList + "\n" + bcolors.ENDC
            uResp = str(raw_input('Do You Want To Continue [YES|Y|NO|N] : ')).upper().strip()
            #if uResp == "NO" or uResp == "N":
            if uResp != "YES" and uResp != 'Y':
                pPODName ()
if __name__ == '__main__':
    blackout()

我想在main.py中调用black.py模块但是当我运行main.py时出现以下错误:

[oracle@localhost tmp]$ ./main.py
****************************************************
                M A I N - M E N U
****************************************************
 0. Enter POD Name(s).
 1. QUIT

Enter your choice [0-24] : 0
Traceback (most recent call last):
  File "./main.py", line 131, in <module>
    pPODName()
NameError: name 'pPODName' is not defined

2 个答案:

答案 0 :(得分:2)

就main.py而言,

pPODName尚未定义。

首先

pPODName在另一个函数中定义,因此只有该函数才能识别它。这被称为在本地范围内。基本上,函数中定义的任何内容只能由该函数访问,而不能用于任何其他函数。

举个例子:

def test():
    def test_local():
        return 3
    return test_local()

运行功能测试为您提供

>>> test()
3

然而运行函数test_local

>>> test_local()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'test_local' is not defined

其次

其次,在导入模块时,您需要使用模块名称对其进行限定,就像您使用black.blackout()一样,需要使用:

from black import *

虽然我个人不喜欢这样,但很难看出方法的来源。

修复

要让main.py能够调用pPODName,必须在整个模块中定义它,例如:

def blackout()
    #do stuff here

def pPODNAME()
    #do other stuff here
    #note the indentation level is the same as blackout

你必须用

来调用它
black.pPODName()

来自main.py

答案 1 :(得分:0)

您有一些编程层次结构错误 为什么pPODName是停电的内在功能? 詹姆斯说 - 如果你使用外部和内部功能,外部需要调用内部。

我建议你在black.py中使用常规功能 blackout函数除了包含pPODName之外什么都不做,所以你不需要它

Shlomy

相关问题