DEV Community

Cover image for How to convert .py to .exe? Step by step guide.
Eshleron
Eshleron

Posted on • Updated on

Python to .exe How to convert .py to .exe? Step by step guide.

Auto PY to EXE

The only tool that we are gonna be using is Auto PY to EXE!

Auto PY to EXE is an amazing application for making .exe file out of your project whether it is one .py file or any number of them.
The application has a nice gui and looks like this:

alt text

How to start

Step 1. Installation

Installing using PyPI:

To install the application run this line in cmd:

pip install auto-py-to-exe

To open the application run this line in cmd:

auto-py-to-exe

Note: if you have any problems installing this way or you want to install it from GitHub go to the main page or watch this instructional video by the developer of "Auto PY to EXE" himself.

For more additional information use this

"Issues When Using auto-py-to-exe"

Step 2. Converting

There are few main options you need to choose:

  1. Pick your .py file
  2. Pick "One Directory" or "One File" option
  3. Pick additional files

1. Pick your .py file

If you have multiple files choose one that starts the program.

2.1. "One Directory" option

alt text

Pretty simple. When choosing "One Directory" option "Auto PY to EXE" will put all dependencies in one folder. You can choose Output directory in "Advanced" menu. If you have media files like icons and backgrounds you shouldn't have any problems using them inside your .exe if you place media files/folders in the Output directory.
Something like this:

alt text

2.2. "One File" option

alt text

When choosing "One File" option "Auto PY to EXE" will create one .exe file containing all dependencies but NOT MEDIA FILES. If your program has only default Windows gui with no icons, backgrounds, media files or you are OK with placing media folder near .exe file feel free to skip the following explanation. For those who want to pack media files into .exe file itself read paragraph 3.

3. Pick additional files

There is a menu in "Auto PY to EXE" called "Additional Files" that lets you add files of your choice. There is a catch though. "Auto PY to EXE" uses pyinstaller which unpacks the data into a temporary folder and stores this directory path in the _MEIPASS environment variable. Your project won't find necessary files because the path changed and it won't see the new path either. In other words, if option "One File" is chosen picked files in the "Additional Files" menu will not be added to .exe file. To work around this you should use this code provided by developer of Auto PY to EXE here

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
try:
    # PyInstaller creates a temp folder and stores path in _MEIPASS
    base_path = sys._MEIPASS
except Exception:
    base_path = os.path.abspath(".")

return os.path.join(base_path, relative_path)
Enter fullscreen mode Exit fullscreen mode

To use this code in your project replace the link to the media file you have now
For example:

setWindowIcon(QIcon('media\icons\logo.png'))
Enter fullscreen mode Exit fullscreen mode

with

setWindowIcon(QIcon(resource_path('logo.png'))
Enter fullscreen mode Exit fullscreen mode

Now the link will be referenced correctly and chosen files successfully packed into .exe file.

For comparison:
Possible link before

"C:\Users\User\PycharmProjects\media\icons\logo.png"
Enter fullscreen mode Exit fullscreen mode

Possible link after

"C:\Users\User\AppData\Local\Temp\\_MEI34121\logo.png"
Enter fullscreen mode Exit fullscreen mode

Press CONVERT .PY TO .EXE

alt text

Wait

alt text

Step 3. Run your program!

Now everything is done!

Run it. Test it. See what`s up.

Make sure everything works well.

You made One Directory

Every file you need should be in the single directory.

You made One File

This way you should have single .exe file. If you had a need and if done correctly your .exe file will be packed with all media inside it. You will not need any media files/folders present with .exe file for it to display them properly.


P.S.

If you have any feedback or suggestions on what important information should be added feel free to let me know!
This guide is not a description of every possible option done every possible way.
I hope you found that information useful!
Good luck with your projects!

Latest comments (104)

Collapse
 
guneet999 profile image
Guneet Singh

I want to convert complete Django project in exe, I did that using pyinstaller and dist containing the exe file I am running batch file, working fine, but transferring to another person, its not working.

Collapse
 
antojeffry profile image
antojeffry

Image description

hey i am facing this problem.What might be the fix

Collapse
 
eshleron profile image
Eshleron

Hi. Look into the google-cloud-core. It is used in someway in your code directly or as a dependency.

Collapse
 
bouazizaitd profile image
Bouaziz-aitd

I am fairly new to Python. I use spyder IDE and have crated miniconda3 environments. I have two environments (base) that came with the installation and one (GIS) I created. I installed additional packages in the latter as being related to my projects. I installed the auto-py-to-exe using pip as the conda did not recognize it. I compiled a simple plot program that uses matplotlib and when running the onefile exe I get an error that matplotlib is not defined.
I got the following in the output window from auto-py-to-exe :
Running auto-py-to-exe v2.32.0
Building directory: C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad
Provided command: pyinstaller --noconfirm --onefile --windowed "C:/Users/baitdriss/Desktop/Temporaire/Python Learning/test.py"
Recursion Limit is set to 5000
Executing: pyinstaller --noconfirm --onefile --windowed C:/Users/baitdriss/Desktop/Temporaire/Python Learning/test.py --distpath C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad\application --workpath C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad\build --specpath C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad

319792 INFO: PyInstaller: 5.9.0
319812 INFO: Python: 3.9.16 (conda)
319859 INFO: Platform: Windows-10-10.0.19044-SP0
319869 INFO: wrote C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad\test.spec
319921 INFO: UPX is not available.
319962 INFO: Extending PYTHONPATH with paths
['C:\Users\baitdriss\Desktop\Temporaire\Python Learning']
321311 INFO: checking Analysis
321343 INFO: Building Analysis because Analysis-00.toc is non existent
321359 INFO: Initializing module dependency graph...
321411 INFO: Caching module graph hooks...
321498 INFO: Analyzing base_library.zip ...
328990 INFO: Loading module hook 'hook-encodings.py' from 'C:\Users\baitdriss\AppData\Roaming\Python\Python39\site-packages\PyInstaller\hooks'...
334212 INFO: Loading module hook 'hook-pickle.py' from 'C:\Users\baitdriss\AppData\Roaming\Python\Python39\site-packages\PyInstaller\hooks'...
336908 INFO: Loading module hook 'hook-heapq.py' from 'C:\Users\baitdriss\AppData\Roaming\Python\Python39\site-packages\PyInstaller\hooks'...
339809 INFO: Caching module dependency graph...
340211 INFO: running Analysis Analysis-00.toc
340333 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by C:\ProgramData\miniconda3\python.exe
342737 INFO: Analyzing C:\Users\baitdriss\Desktop\Temporaire\Python Learning\test.py
342760 INFO: Processing module hooks...
342841 INFO: Looking for ctypes DLLs
342857 INFO: Analyzing run-time hooks ...
342899 INFO: Looking for dynamic libraries
346010 INFO: Looking for eggs
346020 INFO: Using Python library C:\ProgramData\miniconda3\python39.dll
346028 INFO: Found binding redirects:
[]
346045 INFO: Warnings written to C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad\build\test\warn-test.txt
346142 INFO: Graph cross-reference written to C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad\build\test\xref-test.html
346311 INFO: checking PYZ
346326 INFO: Building PYZ because PYZ-00.toc is non existent
346334 INFO: Building PYZ (ZlibArchive) C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad\build\test\PYZ-00.pyz
346998 INFO: Building PYZ (ZlibArchive) C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad\build\test\PYZ-00.pyz completed successfully.
347049 INFO: checking PKG
347055 INFO: Building PKG because PKG-00.toc is non existent
347077 INFO: Building PKG (CArchive) test.pkg
354176 INFO: Building PKG (CArchive) test.pkg completed successfully.
354208 INFO: Bootloader C:\Users\baitdriss\AppData\Roaming\Python\Python39\site-packages\PyInstaller\bootloader\Windows-64bit-intel\runw.exe
354223 INFO: checking EXE
354255 INFO: Building EXE because EXE-00.toc is non existent
354286 INFO: Building EXE from EXE-00.toc
354297 INFO: Copying bootloader EXE to C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad\application\test.exe.notanexecutable
354823 INFO: Copying icon to EXE
354872 INFO: Copying icons from ['C:\Users\baitdriss\AppData\Roaming\Python\Python39\site-packages\PyInstaller\bootloader\images\icon-windowed.ico']
354880 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
354910 INFO: Writing RT_ICON 1 resource with 3752 bytes
354920 INFO: Writing RT_ICON 2 resource with 2216 bytes
354934 INFO: Writing RT_ICON 3 resource with 1384 bytes
354949 INFO: Writing RT_ICON 4 resource with 38188 bytes
354959 INFO: Writing RT_ICON 5 resource with 9640 bytes
354981 INFO: Writing RT_ICON 6 resource with 4264 bytes
355013 INFO: Writing RT_ICON 7 resource with 1128 bytes
355212 INFO: Copying 0 resources to EXE
355222 INFO: Embedding manifest in EXE
355233 INFO: Updating manifest in C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad\application\test.exe.notanexecutable
355248 INFO: Updating resource type 24 name 1 language 0
355444 INFO: Appending PKG archive to EXE
355498 INFO: Fixing EXE headers
356262 INFO: Building EXE from EXE-00.toc completed successfully.

Moving project to: C:\Users\baitdriss\Desktop\Temporaire\Python Learning\dist
Complete.

I got also this message on the CMD prompt:
(base) PS C:\WINDOWS\system32> auto-py-to-exe
1383 INFO: Extra DLL search directories (AddDllDirectory): []
1383 INFO: Extra DLL search directories (PATH): ['C:\ProgramData\miniconda3', 'C:\ProgramData\miniconda3\Library\mingw-w64\bin', 'C:\ProgramData\miniconda3\Library\usr\bin', 'C:\ProgramData\miniconda3\Library\bin', 'C:\ProgramData\miniconda3\Scripts', 'C:\ProgramData\miniconda3\bin', 'C:\ProgramData\miniconda3\condabin', 'C:\Program Files\Common Files\Oracle\Java\javapath', 'C:\ProgramData\Oracle\Java\javapath', 'C:\WINDOWS\system32', 'C:\WINDOWS', 'C:\WINDOWS\System32\Wbem', 'C:\WINDOWS\System32\WindowsPowerShell\v1.0', 'C:\WINDOWS\System32\OpenSSH', 'c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn', 'c:\Program Files\Microsoft SQL Server\100\Tools\Binn', 'c:\Program Files\Microsoft SQL Server\100\DTS\Binn', 'C:\Program Files (x86)\QuickTime\QTSystem', 'C:\Program Files\dotnet', 'C:\Program Files (x86)\Box\Box Edit', 'C:\Users\baitdriss\AppData\Roaming\Python\Python39\Scripts', '.']

Any help willl be highly appreciated.

Collapse
 
eshleron profile image
Eshleron

Hi! It is hard to say for sure what is the issue in your case since you have a number of environments. I would suggest looking into installing auto-py-to-exe not with pip in this case. But with something more easily recognizable by conda(depends if conda is set in PATH and as a default interpreter). If nothing that you do works, try using a separate IDE like PyCharm to test the project and also the auto-py-to-exe settings that you use to exclude that as a potential problem.

Collapse
 
khanalim profile image
khanalim

Hello, I have made exe file of a python script. My text editor is PyCharm. Exe file shows unix installer. Everything works on my Mac, but whenever, I transfer my exe files or folder to another pc or Mac, that computer does not recognize the exe file.

I am totally lost. Anyone has any suggestions.

Collapse
 
eshleron profile image
Eshleron

Hi! If you're using any paths to directories or files do check that. Also use try/except clause and run stuff in cmd/terminal to see the errors if you don't have a GUI.

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
eshleron profile image
Eshleron

Hi! Check if your program has any threads that need closing.

Collapse
 
doniamariadenny profile image
doniamariadenny

after converting .py to .exe how do i send it to someone through gmail??
pls help

Collapse
 
aadarsh_af profile image
Aadarsh

Hello @eshleron First of all thanks for creating this wonderful GUI App which would help developers convert their scripts, which again propose a GUI App, into a user friendly app!
I've made so many apps using pyinstaller and cx_freeze whichever whenever needed. They're truly a blessing!
Now basically I've created a tkinter script which shows the user latest News and Weather updates using API_KEYS(one for news and the other for weather)... And now when I'm packaging it (by adding all the extra scripts and files it'd need) into an exe file using pyinstaller or cx_freeze... Both the exe files show errors related to the API_KEY of weather app while the script works perfectly fine! I'm attaching images of the errors from both packagers

Error from the Pyinstaller Exe file
The above image is of the error from the Pyinstaller Exe file

Error from the Cx_Freeze exe file
The above image is of the error from the Cx_Freeze Exe file (KeyError 'aadarsh' means that the name I gave to my api_key in the config file)

I have gone through most of the google advices which were from Github, StackOverflow, and many likely sites... I've added snippets to my requests library files and also in the pyinstaller hook folder as per the advices but no change could be found in every other exe file created.... (well the hook advice was helpful earlier when i worked with pydocx library and pyinstaller)

Also please tell me how to contact you by messaging... I need help regarding other programming stuffs too! This one is urgent though

Collapse
 
eshleron profile image
Eshleron

Hi!
Thank you for the kind words regarding the app. Unfortunately for me I am not the author but I completely share your excitement and gratitude towards one.

As for the errors. I think the path solution is worthy of trying first (you can find it in this article under "one file" section)

Try it and see where it leeds.

Collapse
 
how2code profile image
Info Comment hidden by post author - thread only accessible via permalink
How2Code

Well, 1st this isn't a .py into a .exe file, it's a .py into a .pyc so this tutorial is automaticly fake, but no matter is it a .exe or a .pyc file, it does work properly, so I don't really care.

Collapse
 
carolinatc profile image
CarolinaTC

Hi all!
Can anyone help me? I try to create the exe and its ok, but when i try open it, doesn't work...
Anyone have this issue?
Thanks

Collapse
 
eshleron profile image
Eshleron

Please provide snippets of code or a screenshot of an error message

Collapse
 
sanchit1219 profile image
sanchit Kedia

Hi, i am reading data from an excel using pandas in the code. How should i write the path for the excel such that when i send the "one dir" file to another user it doesn't give the error "no such file or directory".

Collapse
 
eshleron profile image
Eshleron

Hi!
I think you just have to make sure when you open the file in your app that the file is there. So you should send a copy to other user so the original stays there or you delete this one and create a new one with the same name.
I think if the file is not present in the directory when you try to open it it will trigger the exception, but otherwise it shouldn't.

Collapse
 
qfactorin profile image
qFactorin

Unable to create exe from my .py file... could you help me out, I am relatively new to python or any other programming language for that matter.

Collapse
 
eshleron profile image
Eshleron

Hi!
Please provide screenshots of errors or some code so I can help.

Collapse
 
qfactorin profile image
qFactorin

Hi, thank you for the quick response. I have attached the screenshots, attaching them again....

Thread Thread
 
qfactorin profile image
qFactorin

pasting the output for your reference:

Running auto-py-to-exe v2.7.11
Building directory: C:\Users\Srini\AppData\Local\Temp\tmpp_j94zui
Provided command: pyinstaller --noconfirm --onefile --console "C:/Users/Srini/AppData/Local/qFactor/.spyproject/qFactor_finalVersion-working-beforemoving.py"
Recursion Limit is set to 5000
Executing: pyinstaller --noconfirm --onefile --console C:/Users/Srini/AppData/Local/qFactor/.spyproject/qFactor_finalVersion-working-beforemoving.py --distpath C:\Users\Srini\AppData\Local\Temp\tmpp_j94zui\application --workpath C:\Users\Srini\AppData\Local\Temp\tmpp_j94zui\build --specpath C:\Users\Srini\AppData\Local\Temp\tmpp_j94zui

22730 INFO: PyInstaller: 4.1
22735 INFO: Python: 3.7.9
22739 INFO: Platform: Windows-10-10.0.18362-SP0
22749 INFO: wrote C:\Users\Srini\AppData\Local\Temp\tmpp_j94zui\qFactor_finalVersion-working-beforemoving.spec
22761 INFO: UPX is not available.
22769 INFO: Extending PYTHONPATH with paths
['C:\Users\Srini\AppData\Local\qFactor\.spyproject',
'C:\Users\Srini\AppData\Local\Temp\tmpp_j94zui']
22815 INFO: checking Analysis
22820 INFO: Building Analysis because Analysis-00.toc is non existent
22825 INFO: Initializing module dependency graph...
22838 INFO: Caching module graph hooks...
22877 INFO: Analyzing base_library.zip ...
26058 INFO: Processing pre-find module path hook distutils from 'C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\hooks\pre_find_module_path\hook-distutils.py'.
26063 INFO: distutils: retargeting to non-venv dir 'C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib'
27755 INFO: Caching module dependency graph...
27864 INFO: running Analysis Analysis-00.toc
27903 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by C:\Users\Srini\AppData\Local\Programs\Python\Python37\python.exe
28052 INFO: Analyzing C:\Users\Srini\AppData\Local\qFactor.spyproject\qFactor_finalVersion-working-beforemoving.py
28558 INFO: Processing pre-safe import module hook six.moves from 'C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\hooks\pre_safe_import_module\hook-six.moves.py'.
30204 INFO: Processing pre-safe import module hook urllib3.packages.six.moves from 'C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\hooks\pre_safe_import_module\hook-urllib3.packages.six.moves.py'.
An error occurred while packaging
Traceback (most recent call last):
File "C:\Users\Srini\Desktop\auto-py-to-exe-master\auto_py_to_exe\packaging.py", line 131, in package
run_pyinstaller()
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller_main.py", line 114, in run
run_build(pyi_config, spec_file, **vars(args))
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller__main
.py", line 65, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\building\build_main.py", line 720, in main
build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\building\build_main.py", line 667, in build
exec(code, spec_namespace)
File "C:\Users\Srini\AppData\Local\Temp\tmpp_j94zui\qFactor_finalVersion-working-beforemoving.spec", line 17, in
noarchive=False)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\building\build_main.py", line 242, in __init
_
self.postinit()
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\building\datastruct.py", line 160, in postinit
self.assemble()
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\building\build_main.py", line 413, in assemble
priority_scripts.append(self.graph.run_script(script))
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\depend\analysis.py", line 304, in run_script
pathname)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1424, in run_script
self._scan_code(m, co, co_ast)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2658, in _scan_code
self._process_imports(module)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2857, in _process_imports
target_module = self._safe_import_hook(*import_info, **kwargs)[0]
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2313, in _safe_import_hook
target_attr_names=None, level=level, edge_attr=edge_attr)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1497, in import_hook
source_package, target_module_partname, level)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1654, in _find_head_package
target_module_headname, target_package_name, source_package)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\depend\analysis.py", line 442, in _safe_import_module
module_basename, module_name, parent_package)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2066, in _safe_import_module
module = self._load_module(module_name, pathname, loader)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2174, in _load_module
self._scan_code(m, co, co_ast)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2658, in _scan_code
self._process_imports(module)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2857, in _process_imports
target_module = self._safe_import_hook(*import_info, **kwargs)[0]
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2313, in _safe_import_hook
target_attr_names=None, level=level, edge_attr=edge_attr)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1498, in import_hook
target_module = self._load_tail(target_package, target_module_partname)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1717, in _load_tail
submodule = self._safe_import_module(head, mname, submodule)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\depend\analysis.py", line 442, in _safe_import_module
module_basename, module_name, parent_package)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2066, in _safe_import_module
module = self._load_module(module_name, pathname, loader)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2174, in _load_module
self._scan_code(m, co, co_ast)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2658, in _scan_code
self._process_imports(module)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2857, in _process_imports
target_module = self._safe_import_hook(*import_info, **kwargs)[0]
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2313, in _safe_import_hook
target_attr_names=None, level=level, edge_attr=edge_attr)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1497, in import_hook
source_package, target_module_partname, level)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1654, in _find_head_package
target_module_headname, target_package_name, source_package)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\depend\analysis.py", line 442, in _safe_import_module
module_basename, module_name, parent_package)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2066, in _safe_import_module
module = self._load_module(module_name, pathname, loader)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2174, in _load_module
self._scan_code(m, co, co_ast)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2658, in _scan_code
self._process_imports(module)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2857, in _process_imports
target_module = self._safe_import_hook(*import_info, **kwargs)[0]
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2313, in _safe_import_hook
target_attr_names=None, level=level, edge_attr=edge_attr)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1497, in import_hook
source_package, target_module_partname, level)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1654, in _find_head_package
target_module_headname, target_package_name, source_package)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\depend\analysis.py", line 442, in _safe_import_module
module_basename, module_name, parent_package)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2066, in _safe_import_module
module = self._load_module(module_name, pathname, loader)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2174, in _load_module
self._scan_code(m, co, co_ast)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2658, in _scan_code
self._process_imports(module)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2857, in _process_imports
target_module = self._safe_import_hook(*import_info, **kwargs)[0]
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2523, in _safe_import_hook
edge_attr=edge_attr)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1514, in import_hook
target_module, target_attr_names):
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1808, in _import_importable_package_submodules
attr_name, submodule_name, package)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\depend\analysis.py", line 442, in _safe_import_module
module_basename, module_name, parent_package)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2066, in _safe_import_module
module = self._load_module(module_name, pathname, loader)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2174, in _load_module
self._scan_code(m, co, co_ast)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2658, in _scan_code
self._process_imports(module)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2857, in _process_imports
target_module = self._safe_import_hook(*import_info, **kwargs)[0]
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 2313, in _safe_import_hook
target_attr_names=None, level=level, edge_attr=edge_attr)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1498, in import_hook
target_module = self._load_tail(target_package, target_module_partname)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1717, in _load_tail
submodule = self._safe_import_module(head, mname, submodule)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\depend\analysis.py", line 431, in _safe_import_module
hook_module.pre_safe_import_module(hook_api)
File "C:\Users\Srini\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\hooks\pre_safe_import_module\hook-urllib3.packages.six.moves.py", line 33, in pre_safe_import_module
raise SystemExit("pre-safe-import-module hook failed, needs fixing.")
SystemExit: pre-safe-import-module hook failed, needs fixing.

Project output will not be moved to output folder
Complete.

Thread Thread
 
eshleron profile image
Eshleron

Thanks.
I have not encountered this one myself, but here is what I found

readthedocs.org/projects/pyinstall...

Search for "pre_safe_import_module". There is a description of the method that might help you.
If that doesn't help I would personally look for libs that may be broken or have a potential of being incorrectly imported.

Thread Thread
 
qfactorin profile image
qFactorin

Hi Eshleron, thank you for taking time and looking in to it. I managed to resolve the issue.
I was unsure of the conflict so have installed python from scratch on a new virtual machine and tried creating exe without installing any packages.

  1. Installed packages that were only part of error logs.
  2. finally found out that reason for the failure was related to hidden import "babel.numbers".
  3. included that as parameter in hidden imports in auto-py-to-exe, and boom everything worked........
Thread Thread
 
eshleron profile image
Eshleron

Glad you got it fixed!

Thread Thread
 
eshleron profile image
Eshleron • Edited

So now I have encountered "SystemExit: pre-safe-import-module hook failed, needs fixing." error myself. I've tried "babel.numbers" solution, didn't help. What helped me solve it was a combination of google search and intuition.

My error was something like this: "PyInstaller\hooks\pre_safe_import_module\hook-urllib3.packages.six.moves.py", line 33, in pre_safe_import_module"

  1. First, as the internet suggested, I uninstalled "usual" Pyinstaller and installed Pyinstaller dev with:
    pip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip.
    That took care of this problem.

  2. But then the second problem emerged: "ImportError: Module 'pywintypes' isn't in frozen sys.path". That was solved by excluding pywin32 from imports in Auto-py-to-exe (Pyinstaller).

I hope this helps someone sometime!
Good luck coding!

Collapse
 
adorabletrooper profile image
Adorable trooper

I just saw your post and then I applied it to my personal project. But .exe file was showing error "Fatal Error!" "Failed to run script". I am having files which include .py files , .png files and .db files. Please find an attachment for an img showing all the files in the directory. I haven't done any thing with my app script yet. Please give me detailed directions to get out from this Error.

Collapse
 
eshleron profile image
Eshleron

Hi!
Please provide screenshots of errors or some code so I can help.

Collapse
 
armanniko profile image
ArmanNiko

Hey there!

I'm programming with pygame. I have done whatever was in the post but it still gives me fatal error when i run the .exe file. I'm using one file and windowed, and I've written the resource_path function in my code. and I've used it like this:
image1 = pygame.image.load(resource_path('filename.png'))
I wanted to ask is this correct? and if it is then why am i having trouble running the .exe file. and if it's not, then what else should i do?

Collapse
 
eshleron profile image
Eshleron

Hi!
I've never used pygame, so regarding that I'm not competent to give any advice.

But what I think might be happening:

  1. Image format(.png) What I found on pygame.org
    "pygame may not always be built to support all image formats. At minimum it will support uncompressed BMP. If pygame.image.get_extended() returns 'True', you should be able to load most images (including PNG, JPG and GIF)."
    I'd check that first.

  2. The way the image is loaded. Kind of through another function. I'd separate the code, meaning creating image first(path to image or you can try creating image object), than loading it, checking if everything is OK.

Anyway, without any extended code or error messages I can do more than guess.

Collapse
 
hj1105 profile image
hj1105

I have a question.
In my python code, I use pyqt5, openpyxl, pandas.
And I load some data from excel and take picture using cam and save the picture in same path.
My code works well in my spyder, but it doesn't work when I made exe file.
Should I have to add more options?

Collapse
 
eshleron profile image
Eshleron • Edited

Hi! What exactly doesn't work when you run it as .exe?

Collapse
 
hj1105 profile image
hj1105

More explain about excel file, I need to read and write the excel file.

Collapse
 
rlp81 profile image
rlp81

Ok, so I don't know why but Windows power shell doesn't recognize the command even though it's downloaded I've even found Auto-py-to-exe in my python packages.

Collapse
 
eshleron profile image
Eshleron

Hi! Check PATH variables

Some comments may only be visible to logged-in visitors. Sign in to view all comments. Some comments have been hidden by the post's author - find out more