Tarak
Tarak

Reputation: 65

How to resolve ImportError: no module named tarfile

I am not able to use tarfile module in my python script.

When i run the script i am getting error "ImportError: no module named tarfile"

if i remove "import tarfile" then its failing at tarfile.open; Error says--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()

The same thing happening for subprocess module also. I have verified the modules and they exists. Note that all the modules are working in python console or at command line

Python Version#2.6

==code==

!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'

Regards, TJ

Upvotes: 0

Views: 3839

Answers (1)

Jasper
Jasper

Reputation: 114

I need 50 reputation to comment, so I'll leave an answer instead:

Could you add the following line to your script:

print sys.version

Just to make absolutely sure the Python version running the script is the same as the one you use as an interpreter. Like people said, maybe an environment variable is overwritten somewhere, causing a wrong version of Python to run the script.

Upvotes: 2

Related Questions