gpt4 book ai didi

python - 如何解决导入错误: no module named tarfile

转载 作者:行者123 更新时间:2023-12-01 03:40:43 24 4
gpt4 key购买 nike

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

当我运行脚本时,我收到错误“ImportError:没有名为 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版本运行脚本。

关于python - 如何解决导入错误: no module named tarfile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39667732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com