如何解决ImportError:没有名为tarfile

时间:2016-09-23 18:54:20

标签: python jython

我无法在我的python脚本中使用tarfile模块。

当我运行脚本时,我收到错误“ImportError:no module named tarfile”

如果我删除“import tarfile”,那么它在tarfile.open失败; 错误说 - NameError:tarfile

def make_tarfile(output_filename, source_dir):
    tar = tarfile.open(output_filename, "w:gz")
    #tar.add(source_dir, arcname=os.path.basename(source_dir))
    tar.add(source_dir)
    tar.close()

子进程模块也发生了同样的事情。 我已经验证了模块,它们存在。请注意,所有模块都在python控制台或命令行中工作

Python版本#2.6

==代码==

!USR /斌/ Python的

from java.io import FileInputStream
#import subprocess
import os
import time as systime
import datetime
import shutil
import sys
print  sys.path
import tarfile
import socket

def weblogicPassChange(dName,aUrl,aUser,aServerName,mServerName,aPort,oAdminPassword,nAdminPassword):
    print '############################################################'
    print '        Changing the admin password for :', dName
    print '############################################################'
    print ' '
    print '####'
    print 'dname : =',dName
    print 'aUrl  :=',aUrl
    print 'aUser :=',aUser
    print 'aServerName :=',aServerName
    print 'mServerName :=',mServerName
    print 'aPort  :=',aPort
    print 'oAdminPassword :=',oAdminPassword
    print 'nAdminPassword :=',nAdminPassword
    print '####'
   connect(aUser,oAdminPassword,aUrl+':'+aPort)
    cd('/SecurityConfiguration/'+dName+'/Realms/myrealm/AuthenticationProviders/DefaultAuthenticator')
    cmo.resetUserPassword(aUser,nAdminPassword)
    print '++++++++++++ +++++++++++ +++++++++++ +++++++++++ +++++++++++ +++++++++++ +++++++++++'
    print '*******  Congrates!!! ', dName , ' Admin Password Changed Successfully  ********'
    print '++++++++++++ +++++++++++ +++++++++++ +++++++++++ +++++++++++ +++++++++++ +++++++++++'
    print ' '
    disconnect()
    print ' '
    print '####   Connecting Using New Credentials.....    ####'
    print ' '
    connect(aUser,nAdminPassword,aUrl+':'+aPort)
    print '####   Successfully Connected Using New Credentials !!!!    ####'
    print ' '
    domainRuntime()
    bean =getMBean('ServerRuntimes/' + mServerName)
    if bean:
       print ' '
       print 'Shutting down the Manage Server:  osb_server1'
       shutdown(mServerName,'Server')
    else:
       print ' '
       print 'Server ',mServerName,' is not running'
    print ' '
    disconnect()
    print ' '
    print '####   Changing Admin Password in boot.properties file   ####'
    print ' '
    text_file=open('/oraapp/config/domains/'+dName+'/servers/'+aServerName+'/security/boot.properties','w')
    text_file.write('#' + systime.strftime("%a")+' ' +systime.strftime("%b")+' '+systime.strftime("%d")+' '+systime.strftime("%H")+':'+systime.strftime("%M")+':'+systime.strftime("%S")+' '+systime.strftime("%Z")+' '+systime.strftime("%Y")+"\n")
    text_file.write("username" +"=" + aUser + "\n")
    text_file.write("password"+"=" + nAdminPassword)
    text_file.close()
    print ' '
    print '####  username and password updated in boot.properties   ####'
    print ' '
    shutil.copyfile('/oraapp/config/domains/'+dName+'/servers/'+aServerName+'/security/boot.properties','/oraapp/config/domains/'+dName+'/servers/'+mServerName+'/data/nodemanager/boot.properties')
    print ' '
    print 'Copied boot.properties to manage server location ManageServerHome/date/nodemanager'
    print ' '
    print '###  Calling stopWeblogic.sh file.....!!!!!! ######'
    print ' '
    os.system('.././config/domains/eapdomain/bin/stopWebLogic.sh')
    print ' '
    print 'Successfully changed weblogic password to',nAdminPassword
    print ' '
    print 'Test copy SCP function'
    print ' '
    string = 'scp /oraapp/config/domains/'+dName+'/servers/'+aServerName+'/security/test.properties'+' '+'s-fmwadmin@gaxgpoa163vd:/oraapp/config/domains/osbdomain1/servers/AdminServer/security'
    print 'string value is = ',string
    os.system('scp /oraapp/config/domains/'+dName+'/servers/'+aServerName+'/security/test.properties'+' '+'s-fmwadmin@gaxgpoa163vd:/oraapp/config/domains/osbdomain1/servers/AdminServer/security')
    print ' '
    print 'Copied successfully'


def envDetail(eName):
    propInputStream = FileInputStream(eName+'_'+'domainDetails.properties')
    configProps = Properties()
    configProps.load(propInputStream)
    domainName=configProps.get("domain.name")
    adminUrl = configProps.get("domain.admin.url")
    adminUser = configProps.get("domain.admin.username")
    adminPort = configProps.get("domain.admin.port")
    adminServerName = configProps.get("domain.adminServer.name")
    mgrServerName = configProps.get("domain.mgrServer.name")
    oldAdminPassword = configProps.get("domain.admin.OLD.password")
    newAdminPassword = configProps.get("domain.admin.NEW.password")
    fmw_home = configProps.get("domain.middlehome")              
    filename='/oraapp/backup/EAP_PRODUCT'+'_'+socket.gethostname()+'_'+systime.strftime("%d")+'-'+systime.strftime("%B")+'-'+systime.strftime("%Y")+'-'+systime.strftime("%T")+'.tar.gz'
    make_tarfile(filename,fmw_home)
    print 'Calling function weblogicPassChange()'
    print ' '
    weblogicPassChange(domainName,adminUrl,adminUser,adminServerName,mgrServerName,adminPort,oldAdminPassword,newAdminPassword)



def make_tarfile(output_filename, source_dir):
    tar = tarfile.open(output_filename, "w:gz")
    #tar.add(source_dir, arcname=os.path.basename(source_dir))
    tar.add(source_dir)
    tar.close()

print '#########################################################################################'
print '        Enter the name of the environment as given below to change the password :'
print '#########################################################################################'
print 'For EAPDOMAIN enter  EAP'
print 'For SOCODEV/QA enter QA'
print 'For SOCOUA/UAT enter UAT'
print 'For PRODUCTION enter PROD'
print '############################################################'
print ' '
env = raw_input('Please enter your name: ')
 if env == 'EAP':
    print 'EAPDOMAIN'
    print env
    envDetail(env)
elif env == 'QA':
    print 'SOCODEV'
elif env == 'UAT':
    print 'SOCOUA'
elif env == 'PROD':
    print 'PRODUCTION'
print ' '
print 'Kindly Restart the Admin and Manager servers of ',env,' Domain'

此致 TJ

1 个答案:

答案 0 :(得分:2)

我需要50点评论,所以我会留下答案:

您可以在脚本中添加以下行:

print sys.version

只是为了确保运行脚本的Python版本与您用作解释器的版本相同。就像人们所说的那样,环境变量可能会在某处被覆盖,导致错误版本的Python运行脚本。