DEV Community

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

Posted on • Edited 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!

Top comments (104)

Collapse
 
joehobot profile image
Joe Hobot


mv somefile.py somefile.exe

😛

Collapse
 
eshleron profile image
Eshleron

😛

Collapse
 
r4nta profile image
r4nta

Great post!

I load variables from a config.py file. How would I enable users to modify this file OUTSIDE the .exe?

Collapse
 
eshleron profile image
Eshleron

Hi!
Don't pack it into .exe file. Leave it outside .exe. It should work that way.

Collapse
 
r4nta profile image
r4nta

Thanks for your quick response!

How can I leave the config.py file outside? I feel that PyInstaller loads the config.py file, compiles it into the exe and then has this fixed state in the program. I need to be able to modify the config file, after compiling.

How do I instruct PyInstaller to leave the config.py file separate?

Thread Thread
 
eshleron profile image
Eshleron

You are right about fixed state. What PyInstaller remembers is the name of the file. So if you paste the same file with different content(some .txt maybe) it will work with it just fine because it thinks it is the same file.

Thread Thread
 
r4nta profile image
r4nta

That would be amazing and a super easy solve. Problem: I cannot find the config.py in the folder that was created by PyInstaller. Actually I can't find any of the modules I import. Are they merged into the .exe file?

Thread Thread
 
eshleron profile image
Eshleron

You should add files manually sometimes. Try copying config there. It should work.

Thread Thread
 
r4nta profile image
r4nta

It does not work. When I manually add the config.py, the file is added - yes. But when I change variables in the file, nothing changes in the program. The .exe does not load data from the config.py

Thread Thread
 
eshleron profile image
Eshleron

Make sure you added the fie to Auto PY to EXE additional files menu for it to be compilated with .exe.

Thread Thread
 
r4nta profile image
r4nta

I changed my config file to .json and now it works. Even with adding the file manually, it does NOT work when the file is .py (the file was in the folder, but changing settings did not reflect in the script).

Thread Thread
 
qwerty_plm profile image
SwethaVSarma

Can you please suggest on how to do this? I have converted the python codes to exe using pyinstaller. I kept the input python file wthout converting to .exe so that the main exe can read from it. But when i change the values in the input python file I have to again use pyinstaller and do the convertion of main python file.

Collapse
 
elputojay profile image
𝕛𝕒𝕪 🇪🇸

Hey buddy, thanks for creating this. I have a problem though: in my python script I call several files that are in the current working directory, based on relative paths to simplify it. Once it's compiled to the exe, it seems that the program goes to the root folder of the computer as an absolute route. This kind of defeats the object since my program has to run in a folder with other files being added and removed.

What can I do to force the .exe to work on relative paths exactly the same way I have my script?

thanks

Collapse
 
eshleron profile image
Eshleron • Edited

Hi!
1.What I usually do is get an absolute path so the script\scripts always know where they are

import os
dir = os.path.dirname(__file__)
abs_path = os.path.join(dir, DB_DOCS_NAME)
variable = open(abs_path)
Enter fullscreen mode Exit fullscreen mode

2.If the main script is looking in the wrong place for other scripts then probably your imports are not correct.
Try to use this resource
realpython.com/absolute-vs-relativ...

Hope that I helped in some way

Collapse
 
prohashim profile image
ProHashim • Edited

Hi there! Thanks a lot for this beautiful article.
But when I try to run auto-py-to-exe I am getting this error:

Traceback (most recent call last):
File "C:\Users\Muhammad Hashim\AppData\Local\Programs\Python\Python38\Scripts\auto-py-to-exe-script.py", line 6, in
from pkg_resources import load_entry_point
File "C:\Users\Muhammad Hashim\AppData\Local\Programs\Python\Python38\lib\site-packages\pkg_resources_init.py", line 3251, in
def _initialize_master_working_set():
File "C:\Users\Muhammad Hashim\AppData\Local\Programs\Python\Python38\lib\site-packages\pkg_resources__init
.py", line 3234, in _call_aside
f(*args, **kwargs)
File "C:\Users\Muhammad Hashim\AppData\Local\Programs\Python\Python38\lib\site-packages\pkg_resources__init
.py", line 3263, in _initialize_master_working_set
working_set = WorkingSet._build_master()
File "C:\Users\Muhammad Hashim\AppData\Local\Programs\Python\Python38\lib\site-packages\pkg_resources__init
.py", line 583, in _build_master
ws.require(
requires)
File "C:\Users\Muhammad Hashim\AppData\Local\Programs\Python\Python38\lib\site-packages\pkg_resources__init
.py", line 900, in require
needed = self.resolve(parse_requirements(requirements))
File "C:\Users\Muhammad Hashim\AppData\Local\Programs\Python\Python38\lib\site-packages\pkg_resources__init
_.py", line 786, in resolve
raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'Eel==0.11.0' distribution was not found and is required by auto-py-to-exe

Collapse
 
eshleron profile image
Eshleron • Edited

Hi!
It says you need Eel 0.11.0. Try to install/reinstall Eel and launch auto-py-to-exe again.

Collapse
 
prohashim profile image
ProHashim

Thanks a lot !!!

Collapse
 
calebwin profile image
Caleb Winston

This looks great! I do want to give a quick shout-out to Nim

If you're looking to build performant executables and Python is what you're really comfortable, I think you'll find Nim's syntax pleasingly similar; and the performance improvements are real 😀

Collapse
 
bauripalash profile image
Palash Bauri 👻

GUI Wrapper for PyInstaller! Gonna try

Collapse
 
rjkitto profile image
Rick Kitto

I love the idea of the program - thanks!
Everything seemed to go great - testing with just one file - moved it to my Windows 10 PC, and got the error message shown in the screen grab.
I hope this is fixable and that there's something simple I didn't do!

The image didn't seem to want to load. Here's what it said ...

"The program or feature cannot start due to incompatibnility
with 64-bit versions of Windows. Please contact the software vendor
to ask if a 64-bit Windows compatible version is available."

Collapse
 
eshleron profile image
Eshleron

Hi!
Probably you used Python x32 or some library that is x32. Check for that, then recompile.

Collapse
 
rjkitto profile image
Rick Kitto

Excellent - thanks for the suggestion - I'll get to work!
cheers, Rick

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
 
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
 
drunkcodes profile image
Aneke Emmanuel Chidubem

Hello,
I wrote an automation PY script. The script is working fine when tested on my IDE.

I created .exe file through your guide.
I lunched the application it opened fine. Everything was complete.

The problem is that when I tried to run automation it will open the web browser but will not automate anything.

Is they a way you can help me out

Collapse
 
eshleron profile image
Eshleron

Hi!

Send me an error code if sometime there will be one.
List all libs you use. I think one of them works that way.
And try to launch your app from cmd, see if there is anything there.

Collapse
 
drunkcodes profile image
Aneke Emmanuel Chidubem

I have fixed it using Auto py to exe.
I had to add some codes and move some files. It works now. But when I install it I can't find the software lunch icon on Desktop . Had to always go to my folder to lunch it. Is they a way to have it on my Desktop

Thread Thread
 
eshleron profile image
Eshleron

Glad you fixed it!
I dont remember this option being there. I think by default it saves the result near initial script location.

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