2016-09-23 5 views
1

pythonスクリプトでtarfileモジュールを使用することができません。ImportErrorを解決する方法:tarfileという名前のモジュールがありません

私はエラー「はImportError:いいえモジュールという名前のtarファイル」取得していたスクリプトを実行私はそのはtarfile.openで失敗し、「インポートtarファイルを」削除する場合

を。 エラーは言う - NameError:tarファイル

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のバージョン#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'+' '+'[email protected]:/oraapp/config/domains/osbdomain1/servers/AdminServer/security' 
    print 'string value is = ',string 
    os.system('scp /oraapp/config/domains/'+dName+'/servers/'+aServerName+'/security/test.properties'+' '+'[email protected]:/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' 

よろしくPythonのコンソールまたはコマンドラインで働いていることに注意してください TJ

+0

スクリプトをどのように呼び出すのですか?インポートに失敗する前に 'print sys.path'を追加できますか? ( 'import sys'の前に) –

+0

' tarfile'モジュールはどこにインストールされていますか?使用しているのと同じバージョンのPython用にインストールされていますか?あなたのPYTHONPATHは何ですか? –

+0

@JohnGordon 'tarfile'と' subprocess'は組み込みです。 –

答えて

2

コメントするには50の評判が必要なので、代わりに答えを残しておきます:

ただ、スクリプトを実行しているPythonのバージョンは、あなたが通訳として使用したものと同じである絶対的に確認するために

print sys.version 

あなたはあなたのスクリプトに以下の行を追加することができます。人々が言っ​​たように、おそらく環境変数がどこかで上書きされ、間違ったバージョンのPythonがスクリプトを実行することがあります。

+0

私はあなたの権利を、私は印刷物sys.versionにその印刷2.2.1を与えると思います。これを解決するには?私はwlstインタラクションを使ってスクリプトを呼び出しています。このコードはjythonのバージョンを使用しています。これはjythonのベースとなるPhytonバージョンに相当します。WebLogic 12cではjython/pythonのバージョンは2.2.1です。バージョンを上書きする方法、または2.2.1のバージョンでtarコマンドを実行する方法 – Tarak

+0

私はWLSTについて知りませんが、[their docs](https://docs.oracle.com/cd/E24329_01/web.1211/)によれば、 e24491/wlst_faq.htm#WLSTG243)は、Jythonの固定バージョンを使用しています。 私はスタンドアロンのtarライブラリを検索しましたが、成功しませんでした。あなたができることは、 'tar'ユーティリティを実行してあなたが望むことをするためのOSコールを作ることです。おそらくそれはオプションですか? – Jasper

+0

ありがとう...私は提案に取り組んでいます。 – Tarak

関連する問題