ModuleNotFoundError: no module named 'numpy'

72 views
Skip to first unread message

Marco Fioramanti

unread,
Jun 29, 2024, 5:21:37 AM6/29/24
to PyInstaller

Hi,

I have a multi-file/folder project developed with python 3.12/pycharm running on win11.

I build a onefile exe with pyinstaller with the following command (pyinstaller 6.8.0):

 

pyinstaller --onefile IO_Analysis.py --add-data "SIOT_2019.xlsx;." --add-data "Results;." --add-data "src;." --add-data "Temp_Results;." --add-data "Manual;."


If I run the exe from the original pc, either from the HD or from a USB drive, it works fine. If a move the set of files/folders in a different pc (win 10 without python/pycharm installation) either in the HD ot in the USB drive, the exe works only partially: it finds the pdf manual or the xls input file but gives the error in the object if I ask for some calculation. the call for 'numpy' is not in the main file (IO_Analysis.py) but in all the files in the 'src' folder.



To try to solve, I have tested the building of the exe with some additional variants: i) adding the import of numpy in the main file; ii) adding the ‘--hidden-import’ option; iii) adding the ‘--paths’ option; iv) updating to the latest version of python and modules and using a new virtual environment via a ‘requirements.txt’ file, but nothing changed.

 

In addition, even if the “warning” text file reports some missing modules, it does not mention ‘numpy’.

Any idea of the possible solution to fix the problem?

Thank you

 

 

Below the log of the building.

 



(myvenv) C:\Users\...omissis...\ITA_PIOA>pyinstaller --onefile IO_Analysis.py --add-data "SIOT_2019.xlsx;." --add-data "Results;." --add-data "src;." --add-data "Temp_Results;." --add-data "Manual;."
396 INFO: PyInstaller: 6.8.0, contrib hooks: 2024.7
396 INFO: Python: 3.12.0
427 INFO: Platform: Windows-11-10.0.22631-SP0
427 INFO: Python environment: C:\Users\...omissis...\ITA_PIOA\myvenv
427 INFO: wrote C:\Users\...omissis...\ITA_PIOA\IO_Analysis.spec
427 INFO: Module search paths (PYTHONPATH):
['C:\\Users\\...omissis...\\ITA_PIOA\\myvenv\\Scripts\\pyinstaller.exe',
'C:\\Users\\Fioramanti_Marco\\AppData\\Local\\Programs\\Python\\Python312\\python312.zip',
'C:\\Users\\Fioramanti_Marco\\AppData\\Local\\Programs\\Python\\Python312\\DLLs',
'C:\\Users\\Fioramanti_Marco\\AppData\\Local\\Programs\\Python\\Python312\\Lib',
'C:\\Users\\Fioramanti_Marco\\AppData\\Local\\Programs\\Python\\Python312',
'C:\\Users\\...omissis...\\ITA_PIOA\\myvenv',
'C:\\Users\\...omissis...\\ITA_PIOA\\myvenv\\Lib\\site-packages',
'C:\\Users\\...omissis...\\ITA_PIOA']
840 INFO: Appending 'datas' from .spec
845 INFO: checking Analysis
845 INFO: Building Analysis because Analysis-00.toc is non existent
845 INFO: Running Analysis Analysis-00.toc
845 INFO: Target bytecode optimization level: 0
845 INFO: Initializing module dependency graph...
846 INFO: Caching module graph hooks...
868 INFO: Analyzing base_library.zip ...
2455 INFO: Loading module hook 'hook-heapq.py' from 'C:\\...omissis...\\ITA_PIOA\\myvenv\\Lib\\site-packages\\PyInstaller\\hooks'...
2563 INFO: Loading module hook 'hook-encodings.py' from 'C:\\...omissis...\\ITA_PIOA\\myvenv\\Lib\\site-packages\\PyInstaller\\hooks'...
4822 INFO: Loading module hook 'hook-pickle.py' from 'C:\\...omissis...\\ITA_PIOA\\myvenv\\Lib\\site-packages\\PyInstaller\\hooks'...
6566 INFO: Caching module dependency graph...
6739 INFO: Looking for Python shared library...
6739 INFO: Using Python shared library: C:\Users\Fioramanti_Marco\AppData\Local\Programs\Python\Python312\python312.dll
6739 INFO: Analyzing C:\Users\...omissis...\ITA_PIOA\IO_Analysis.py
6896 INFO: Loading module hook 'hook-_tkinter.py' from 'C:\\...omissis...\\ITA_PIOA\\myvenv\\Lib\\site-packages\\PyInstaller\\hooks'...
6896 INFO: Processing module hooks...
6896 INFO: Loading module hook 'hook-_tkinter.py' from 'C:\\...omissis...\\ITA_PIOA\\myvenv\\Lib\\site-packages\\PyInstaller\\hooks'...
6896 INFO: checking Tree
6896 INFO: Building Tree because Tree-00.toc is non existent
6896 INFO: Building Tree Tree-00.toc
6975 INFO: checking Tree
6975 INFO: Building Tree because Tree-01.toc is non existent
6975 INFO: Building Tree Tree-01.toc
6990 INFO: checking Tree
6990 INFO: Building Tree because Tree-02.toc is non existent
6990 INFO: Building Tree Tree-02.toc
7006 INFO: Performing binary vs. data reclassification (942 entries)
7194 INFO: Looking for ctypes DLLs
7210 INFO: Analyzing run-time hooks ...
7210 INFO: Including run-time hook 'C:\\...omissis...\\ITA_PIOA\\myvenv\\Lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_inspect.py'
7210 INFO: Including run-time hook 'C:\\...omissis...\\ITA_PIOA\\myvenv\\Lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth__tkinter.py'
7241 INFO: Looking for dynamic libraries
7445 INFO: Extra DLL search directories (AddDllDirectory): []
7445 INFO: Extra DLL search directories (PATH): []
7649 INFO: Warnings written to C:\Users\...omissis...\ITA_PIOA\build\IO_Analysis\warn-IO_Analysis.txt
7666 INFO: Graph cross-reference written to C:\Users\...omissis...\ITA_PIOA\build\IO_Analysis\xref-IO_Analysis.html
7712 INFO: checking PYZ
7712 INFO: Building PYZ because PYZ-00.toc is non existent
7712 INFO: Building PYZ (ZlibArchive) C:\Users\...omissis...\ITA_PIOA\build\IO_Analysis\PYZ-00.pyz
8089 INFO: Building PYZ (ZlibArchive) C:\Users\...omissis...\ITA_PIOA\build\IO_Analysis\PYZ-00.pyz completed successfully.
8120 INFO: checking PKG
8120 INFO: Building PKG because PKG-00.toc is non existent
8120 INFO: Building PKG (CArchive) IO_Analysis.pkg
11732 INFO: Building PKG (CArchive) IO_Analysis.pkg completed successfully.
11773 INFO: Bootloader C:\Users\...omissis...\ITA_PIOA\myvenv\Lib\site-packages\PyInstaller\bootloader\Windows-64bit-intel\run.exe
11773 INFO: checking EXE
11773 INFO: Building EXE because EXE-00.toc is non existent
11773 INFO: Building EXE from EXE-00.toc
11773 INFO: Copying bootloader EXE to C:\Users...omissis...\ITA_PIOA\dist\IO_Analysis.exe
11794 INFO: Copying icon to EXE
11810 INFO: Copying 0 resources to EXE
11810 INFO: Embedding manifest in EXE
11826 INFO: Appending PKG archive to EXE
11857 INFO: Fixing EXE headers
12030 INFO: Building EXE from EXE-00.toc completed successfully.

bwoodsend

unread,
Jun 30, 2024, 12:39:59 PM6/30/24
to PyInstaller

If I’m reading this right, you’ve got raw .py scripts in the src folder and presumably you’re running them as subprocesses using something akin to subprocess.run(["python", "src/IO_Analysis.py"]) or possibly something involving exec()? So you’re giving code to PyInstaller as data files so it has no idea that it needs to scan them for dependencies and you’re invoking that code using a random Python interpreter instead of the one PyInstaller collected so even if PyInstaller did know to collect numpy, you wouldn’t be able to use it anyway.

I suggest that you stop trying to structure your code like a C++ project. Move everything out of src and into the top level of your project then use import IO_Analysis; IO_Analysis.do_something() instead of whatever you're using to invoke the other scripts. Or better yet, learn how to properly structure Python projects.

Marco Fioramanti

unread,
Jul 1, 2024, 10:11:54 AM7/1/24
to PyInstaller
Bwoodsend,
thank you for your reply.
I must confess that I’m a newbie/dummy in programming so if my project resembles you a C++ project is just by chance. Knowing the math behind the program, I asked ChatGPT to translate it into python code.
My goal is to distribute the final exe to colleagues who neither know python nor have python itself installed in their computer. Having different folder names serves the purpose of being as clear as possible with my colleagues.

The structure of the project is:
MyProjetcFolder
      |-----IO_Analysis.py
      |-----SIOT_2019.xlsx (input file)
      |-----Manual
      |             |---Manual.pdf
      |-----Results
      |             |---IO_ResultsBase.xlsx
      |             |---IO_ResultsDomImp.xlsx
      |             |---…
      |             |---…
      |-----src    
      |             |---Mod_Base.py
      |             |---Mod_DomImp.py
      |             |---…
      |             |---…
      |-----Temp_results
      |             |---key_value.txt
      |-----myvenv (virtual environment)

The original idea was to put the main script (IO_Analysis.py) in the src folder but I couldn’t manage to use input from and write results into same level (parallel?) folder, while if the main file is in the main folder everything seems to be easier.

The code in the main file is the following:

import tkinter as tk
import subprocess
import os

def run_python_file(file_path, root):
    # Run the specified Python file
    subprocess.Popen(['python', file_path], shell=True)
    # Close the current window
    root.destroy()

def open_excel_file():
    # Specify the path to the Excel file you want to open
    excel_file_path = "SIOT_2019.xlsx"

    # Check if the Excel file exists
    if os.path.isfile(excel_file_path):
        # Open the Excel file using the default program associated with .xlsx files
        subprocess.Popen([excel_file_path], shell=True)
        continue_program()
    else:
        # Display an error message if the Excel file does not exist
        error_message = "The specified Excel file does not exist."
        tk.messagebox.showerror("Error", error_message)

def continue_program():
    # Create a new Tkinter window for the second pop-up
    root2 = tk.Toplevel(root)
    root2.title("Select Python File")

    # Create a label with the question inside the first pop-up
    question_label = tk.Label(root2, text="Choose a model to run")
    question_label.pack()

    # Function to handle button clicks for Python files
    def button_click(file_path):
        run_python_file(file_path, root2)

    # Create six buttons for different Python files
    button1 = tk.Button(root2, text="Base Model", command=lambda: button_click("src\\\\\\\\Mod_Base.py"))
    button1.pack()

    button2 = tk.Button(root2, text="Domestic/Import Model", command=lambda: button_click("src\\\\\\\\Mod_DomImp.py"))
    button2.pack()

    button3 = tk.Button(root2, text="Employment Model", command=lambda: button_click("src\\\\\\\\Mod_Empl.py"))
    button3.pack()

    button4 = tk.Button(root2, text="Value Added Model", command=lambda: button_click("src\\\\\\\\Mod_VA.py"))
    button4.pack()

    button5 = tk.Button(root2, text="Endogenous Household Model", command=lambda: button_click("src\\\\\\\\Mod_EndHH.py"))
    button5.pack()

    button6 = tk.Button(root2, text="Cost/Price Model", command=lambda: button_click('src\\\\\\\\Mod_CostPrice_D.py'))
    button6.pack()

    # Run the Tkinter event loop for the second pop-up window
    root2.mainloop()

# Create the Tkinter window for the main background
root = tk.Tk()
root.title("Main Window")
root.attributes('-fullscreen', True)  # Set to fullscreen mode
root.configure(bg='blue')

def exit_fullscreen(event=None):
    root.attributes('-fullscreen', False)
    root.geometry("800x600")  # Optional: set to a reasonable size after exiting fullscreen

# Bind the Escape key to exit fullscreen mode
root.bind("<Escape>", exit_fullscreen)

# Function to minimize the window
def minimize_window():
    root.iconify()

# Function to toggle fullscreen mode
def toggle_fullscreen():
    if root.attributes('-fullscreen'):
        root.attributes('-fullscreen', False)
        root.geometry("800x600")
    else:
        root.attributes('-fullscreen', True)

# Function to close the window
def close_window():
    root.destroy()

# Create a frame for the custom title bar
title_bar = tk.Frame(root, bg='blue', relief='raised', bd=2)
title_bar.pack(side='top', fill='x')

# Add close button
close_button = tk.Button(title_bar, text='x', command=close_window, bg='light grey', fg='black', relief='flat')
close_button.pack(side='right', padx=5)

# Add maximize button
max_button = tk.Button(title_bar, text='⬜', command=toggle_fullscreen, bg='light grey', fg='black', relief='flat')
max_button.pack(side='right', padx=5)

# Add minimize button
min_button = tk.Button(title_bar, text='-', command=minimize_window, bg='light grey', fg='black', relief='flat')
min_button.pack(side='right', padx=5)

# Create a label for Input/Output Analysis title
title_label = tk.Label(root, text="Input/Output Analysis", bg='blue', fg='white', font=("Helvetica", 40))
title_label.pack(pady=(200, 10))  # Aumenta il valore del pady per spostare la label più in basso

# Create a frame for the central content
content_frame = tk.Frame(root, bg='blue')
content_frame.pack(expand=True)

# Create a label with the question inside the first pop-up
question_label = tk.Label(content_frame, text="Do you want to open the Excel Input file?", bg='blue', fg='white', font=("Helvetica", 16))
question_label.pack(pady=20)

# Create a frame for the buttons to be centered
button_frame = tk.Frame(content_frame, bg='blue')
button_frame.pack()

# Create the "Yes" button to open the Excel file with larger size
yes_button = tk.Button(button_frame, text="Yes", command=open_excel_file, font=("Helvetica", 16), width=10, height=2)
yes_button.pack(side="left", padx=10)

# Create the "No" button to continue the program with larger size
no_button = tk.Button(button_frame, text="No", command=continue_program, font=("Helvetica", 16), width=10, height=2)
no_button.pack(side="left", padx=10)

# Create the "Exit" button to close the main window
exit_button = tk.Button(button_frame, text="Exit", command=close_window, font=("Helvetica", 16), width=10, height=2)
exit_button.pack(side="left", padx=10)

# Funzione per aprire il file di help
def open_help_file():
    # Specifica il percorso del file di help
    help_file_path = "Manual\\\\\\\\IO_Analysis_Manual.pdf"  

    # Controlla se il file di help esiste
    if os.path.isfile(help_file_path):
        # Apri il file di help usando il programma predefinito associato ai file PDF
        subprocess.Popen([help_file_path], shell=True)
    else:
        # Mostra un messaggio di errore se il file di help non esiste
            error_message = "Il file di help specificato non esiste."
            tk.messagebox.showerror("Errore", error_message)

    # Creare il pulsante "Go to manual"
manual_button = tk.Button(button_frame, text="Go to Manual", command=open_help_file, font=("Helvetica", 16), width=15, height=2)
manual_button.pack(side="right", padx=100)

    # Run the Tkinter event loop for the main window
root.mainloop()


My question: are you suggesting me to move all the .py files in the main folder and then change the 5th line of the code (comments excluded)
subprocess.Popen(['python', file_path], shell=True)
with    
import IO_Analysis; IO_Analysis.file_path()

Thank you for any additional help you can give me.

M

bwoodsend

unread,
Jul 1, 2024, 1:29:28 PM7/1/24
to PyInstaller
If you're using ChatGPT at all then I'd ask to you stay off these support channels. No one wants to spend their time sifting through the utter nonsense that AI calls code.

Chris Barker

unread,
Jul 1, 2024, 1:36:41 PM7/1/24
to pyins...@googlegroups.com
On Mon, Jul 1, 2024 at 7:12 AM Marco Fioramanti <marco.fi...@gmail.com> wrote:
 Knowing the math behind the program, I asked ChatGPT to translate it into python code.

Well, there's your problem :-(

ChatGPT has written some horrible code for you.

All the Popen calls are interesting, and well,  totally unnecessary . What's interesting (to me) is that I've seen ChatGPT code like that, but in that case, it was asked to translate a shell scripts into python,  so the Popen stuff made sense, but in this case -- WTF?

I guess we programmers are not out of job just yet :-)

My advice is to try to make this the proper Python. With luck the TkInter code and the math code are OK, and all you need to do is replace all the Open calls with plain Python calling of functions, but ?????

Not sure what to tell you other that you'll need to learn some Python -- and/or get a "naturally intelligent" person to help.

-CHB





 
--
You received this message because you are subscribed to the Google Groups "PyInstaller" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyinstaller...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyinstaller/ee0037ed-4bec-4e68-bb2e-1b5f6770707dn%40googlegroups.com.


--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris....@noaa.gov

Marco Fioramanti

unread,
Jul 1, 2024, 3:17:20 PM7/1/24
to PyInstaller

I’m an economist not a programmer and I’m perfectly aware that my code is far from being optimized. I had some python courses online but they rarely go beyond the “Hello world” stuff. On the other hand, with hundreds of trial and error I was able to get a quite complex (for me) project which does exactly what it was supposed to do. What I can’t manage is to create a python free exe with pyinstaller. In addition I see that the “ModuleNotFoundError” is quite common…not only among economists.

@bwoodsend If you have the authority to throw me out of the forum then do it, otherwise I suggest you to not waste your time just ignoring my questions.

@Chris Barker the reason I joined the forum was indeed to look for help from human intelligence, not to stimulate the acrimony against the AI which is, at the end of the day, a product of programmers.

Any additional hint will be appreciated.

M

Chris Barker

unread,
Jul 1, 2024, 7:26:40 PM7/1/24
to pyins...@googlegroups.com
The issue here is that this forum is about PyInstaller, and your problem is more core Python than that.

What you need to do, as Bob suggested is make your code into a "proper" python application / package:

https://packaging.python.org/en/latest/tutorials/packaging-projects/

The challenge is that those docs are oriented towards making a package that one might publish -- which is not what you want to do, but the core concepts are the same -- you want a package for your code, and one top-level script to run the application.

As for help from human intelligence, you might try:

https://discuss.python.org/

But my suggestion is that you have the approach of:

"I want help learning how to make a Python application"

rather than

"How can I get this working ASAP without having to learn too much?"

The latter is what paid consultants are for.

Good luck.

-CHB


Reply all
Reply to author
Forward
0 new messages