@ikarugaaaaaa

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Pythonで作成した.exeファイルが作動しない

Convert_WAV.py

##ライブラリインポート
import os
import sys
from tkinter import*
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
from pydub import AudioSegment


def caw(input_folder,output_folder,target_format):
    for filename in os.listdir(input_folder):
        if filename.endswith(".mp3"):
            filepath = os.path.join(input_folder,filename)        
            audio = AudioSegment.from_file(filepath, format="mp3")
            output_filepath = os.path.join(output_folder,
                                           os.path.splitext(filename)[0]+'.'+target_format)
            audio.export(output_filepath,
                         format=target_format)
            print(f"変換完了: {filename} -> {output_filepath}")  
            
        elif filename.endswith(".wav"):
            filepath = os.path.join(input_folder,filename)            
            audio = AudioSegment.from_file(filepath, format="wav")
            output_filepath = os.path.join(output_folder,
                                           os.path.splitext(filename)[0]+'.'+target_format)
            audio.export(output_filepath,
                         format=target_format)
            print(f"変換完了: {filename} -> {output_filepath}")  


        elif filename.endswith(".wma"):
            filepath = os.path.join(input_folder,filename)
            audio = AudioSegment.from_file(filepath, format="asf")      
            output_filepath = os.path.join(output_folder,
                                           os.path.splitext(filename)[0]+'.'+target_format)
            audio.export(output_filepath,
                         format=target_format)
            print(f"変換完了: {filename} -> {output_filepath}")  

            
        elif filename.endswith(".m4a"):
            filepath = os.path.join(input_folder,filename)
            audio = AudioSegment.from_file(filepath, format="m4a") 
            output_filepath = os.path.join(output_folder,
                                           os.path.splitext(filename)[0]+'.'+target_format)
            audio.export(output_filepath,
                         format=target_format)
            print(f"変換完了: {filename} -> {output_filepath}")  
            
        elif filename.endswith(".flac"):
            filepath = os.path.join(input_folder,filename)
            audio = AudioSegment.from_file(filepath, format="flac")  
            output_filepath = os.path.join(output_folder,
                                           os.path.splitext(filename)[0]+'.'+target_format)
            audio.export(output_filepath,
                         format=target_format)
            print(f"変換完了: {filename} -> {output_filepath}")  
        
        elif filename.endswith(".ogg"):
            filepath = os.path.join(input_folder,filename)
            audio = AudioSegment.from_file(filepath, format="ogg")  
            output_filepath = os.path.join(output_folder,
                                           os.path.splitext(filename)[0]+'.'+target_format)
            audio.export(output_filepath,
                         format=target_format)
            print(f"変換完了: {filename} -> {output_filepath}")  
    

    # inputfolder指定の関数
def dirdialog_clicked1():
    iDir = os.path.abspath(os.path.dirname(__file__))
    iDirPath = filedialog.askdirectory(initialdir = iDir)
    entry1.set(iDirPath)

    # output_folder指定の関数
def dirdialog_clicked2():
    iDir1 = os.path.abspath(os.path.dirname(__file__))
    iDirPath1 = filedialog.askdirectory(initialdir = iDir1)
    entry2.set(iDirPath1)

def selected():
    select = listbox.curselection()
    if select:
        target_format = os.path.join(listbox.get(select))
        entry3.set(target_format)
    else :
        entry3.set("wav")
    
    
    # 実行ボタン押下時の実行関数
def conductMain():
    input_folder = entry1.get()
    output_folder = entry2.get()
    target_format = entry3.get()
    text = ""
    try:
        caw(input_folder,output_folder,target_format)
    except:
       messagebox.showerror("error", "いずれかのパスの指定がありません。")
    else:
        text += "変換元:" + input_folder + "\n出力先:" + output_folder+ "\nフォーマット:" + target_format
        messagebox.showinfo("Success!", text)
        
        
if __name__ == "__main__":
    # rootの作成
    root = Tk()
    root.title("Audio_Converter")
    root.geometry("400x150")
    root.resizable(False, False)

    # Frame1の作成
    frame1 = ttk.Frame(root, padding=10)
    frame1.grid(row=0, column=1, sticky=E)
    # 「変換元」ラベルの作成
    IDirlabel = ttk.Label(frame1, text="変換元フォルダ>>", padding=(5, 2))
    IDirlabel.pack(side=LEFT)
    
    # 「フォルダ参照」エントリーの作成
    entry1 = StringVar()
    IDirEntry = ttk.Entry(frame1, textvariable=entry1, width=30)
    IDirEntry.pack(side=LEFT)
    
    # 「フォルダ参照」ボタンの作成
    IDirButton = ttk.Button(frame1, text="参照", command=dirdialog_clicked1)
    IDirButton.pack(side=LEFT)
    
    # Frame2の作成
    frame2 = ttk.Frame(root, padding=10)
    frame2.grid(row=2, column=1, sticky=E)

    # 「出力先」ラベルの作成
    IFileLabel = ttk.Label(frame2, text="出力先フォルダ>>", padding=(5, 2))
    IFileLabel.pack(side=LEFT)
    
    # 「ファイル参照」エントリーの作成
    entry2 = StringVar()
    IFileEntry = ttk.Entry(frame2, textvariable=entry2, width=30)
    IFileEntry.pack(side=LEFT)
    
    # 「ファイル参照」ボタンの作成
    IFileButton = ttk.Button(frame2, text="参照", command=dirdialog_clicked2)
    IFileButton.pack(side=LEFT)


    # Frame3の作成
    frame3 = ttk.Frame(root, padding=10)
    frame3.grid(row=6,column=1,sticky=W)
    
    # 実行ボタンの設置
    button1 = ttk.Button(frame3, text="変換", command=conductMain)
    button1.pack(fill = "x", padx=30, side = "left")
    
    # キャンセルボタンの設置
    button2 = ttk.Button(frame3, text="Cancel", command=lambda: root.destroy())
    button2.pack(fill = "x", padx=30, side = "right")
    
    root.mainloop()

このコードをpyinstaller -F -w --cleanでexe化して実行したところ,
以下のエラーを吐きました

Traceback (most recent call last):
  File "pydub\utils.py", line 14, in <module>
ModuleNotFoundError: No module named 'audioop'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "Convert_WAV.py", line 16, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 450, in exec_module
  File "pydub\__init__.py", line 1, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 450, in exec_module
  File "pydub\audio_segment.py", line 11, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 450, in exec_module
  File "pydub\utils.py", line 16, in <module>
ModuleNotFoundError: No module named 'pyaudioop'

ffmpegは\User\username下にあります.

実行環境は以下の通りです

* Spyder version: 5.5.1  (conda)
* Python version: 3.12.7 64-bit
* Qt version: 5.15.2
* PyQt5 version: 5.15.10
* Operating System: Windows-11-10.0.26100-SP0

コマンドプロンプトで.exe化する場合

Python 3.13.3
pyinstaller 6.12.0

Anacona Promptで.exe化する場合

          conda version : 24.11.3
    conda-build version : 24.9.0
         python version : 3.12.7.final.0

このようにエラーを吐きます.
コマンドプロンプト上でも,Anaconda Prompt上でやっても実行後は同様のエラーを吐きました.
この場合の対処法をご教授願いたいです.

0 likes

2Answer

Traceback (most recent call last):
  File "pydub\utils.py", line 14, in <module>
ModuleNotFoundError: No module named 'audioop'

pydubの下をhiddenimportsにするか、パスを追加する形にすれば解決しないでしょうか
同じパッケージではないですが、私も以前pyinstallerで苦労したのでー

1Like

audioopはpython 3.13で廃止されたようですので、pyinstallerの仮想環境をpython3.13から3.12に変更すると良いのではないでしょうか?
audioopの代替としてaudioop-ltsを使うというケースもあるらしいです。
pip install audioop-lts
こちらはpython 3.13以降で動作するとのこと

python 3.12の環境でも同じエラーが出る場合はimport audioopの文を冒頭に入れてみるともしかしたら改善するかもしれないです。

1Like

Your answer might help someone💌