How to resolve ImportError: no module named tarfile

How to resolve ImportError: no module named tarfile

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

当我 运行 脚本时出现错误“导入错误:没有名为 tarfile 的模块”

如果我删除“导入 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/bin/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

我需要 50 声望才能发表评论,所以我会留下一个答案:

您能否将以下行添加到您的脚本中:

print sys.version

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