How to add missing .dll files while preparing .exe file with pyinstaller - python

I have just prepared simple script importing some module and printing something:
from clicknium import clicknium as cc
print(cc.edge.browsers)
So I have created the venv, installed clicknium (pip3 install clicknium==0.1.9).
After that I have prepared spec file:
pyi-makespec spec_file_name script_name.py
After running the command with created .spec file:
pyinstaller spec_file_name.spec
The pyinstaller is creating the .exe file.
After running the .exe I got an error:
System.IO.FileNotFoundException: Unable to find assembly 'C:\Users\user_1\AppData\Local\Temp\_MEI197042\clicknium\.lib\automation\ClickniumJavaBridge-32.dll'
Of course I understand the error but I'm not sure how to fix it.
When I has some problems with missing files I have added it by using --add-data while making the spec file. But It's not working for me with .dll files.
clicknium==0.1.9
pyinstaller==5.4.1
Update
Right now I'm using velow command to create .spec file:
pyi-makespec --onefile --add-data="C:\Users\...\project_name\venv\Lib\site-packages\clicknium\.lib\automation\*;clicknium\.lib\automation" --name app app.py
The error above is fixed but there is something new.
The code below causes the error:
clr.AddReference(Apath)
System.BadImageFormatException: Could not load file or assembly 'file:///C:\Users\user_1\AppData\Local\Temp\_MEIxxxxxx\clicknium\.lib\automation\ClickniumJavaBridge-32.dll' or one of its dependencies. The module was expected to contain an assembly manifest.
So as I understand the .dll file is still not visible there and clicknium is still looking for the dll files in Temp files.

clicknium supplied package project/folder function, can generate the exe.
you can refer to this: https://www.clicknium.com/documents/tutorial/vscode/project_management
first, in vscode, run command "Clicknium: Create Project", you can select the current folder;
then, run command "Clicknium: Package Project", it will generate the exe file

Sometimes you have to add *.dll files to your build process in the *.spec file
Look here, there is a lot of discussion about it:
Bundling data files with PyInstaller (--onefile)

If it not work for you try using other options like one file:
pyi-makespec -F script_name.py spec_file_name
also the order need to be the script first
pyi-makespec script_name.py -n spec_file_name

Related

PyAutoit : OSError: Cannot load AutoItX from path: C:\Users\User\AppData\Local\Temp\_MEI152482\autoit\lib\AutoItX3_x64.dll

I been trying to compile this program for quite some time but can't seem to figure out what is going on. From just reading the error code I'm assuming pyinstaller cannot or does not know where the needed AutoitX3_64.dll file. I've read other questions but cant seem to find a detailed answer explain how to tell my installer where the files are located.
I've tried:
pyinstaller --onefile main.py (Fail)
pyinstaller -w --onefile --add-data C:\Users\User\AppData\Local\Programs\Python\Python310\Lib\site-packages\autoit\lib\AutoItX3_x64.dll;autoit\lib main.py (Fail)
I've also dumped autoit folder in my build / dist with no success.
Any help would be greatly apriciated!
[error1][error2]
After searching this site and countless others i managed to figure out how to fix the error:
OSError: Cannot load AutoItX from path:
C:\Users\myusername\AppData\Local\Temp\_MEI152482\autoit\lib\AutoItX3_x64.dll
The answer would be to make a spec file and tell it where the autoit files are located. Here is how!
When you run pyinstaller command it will automatically generate a filename.spec in your home dir. For example:
pyinstaller -w --onefile main.py
-w is no console --onefile is to compress all files into one executable. main.py is the file you are going to make into exe. Now you will see a filename.spec in your home dir. Edit that with the following datas and hiddenimports
datas=
[["C:\\Users\\myusername\\AppData\\Local\\Programs\\Python\\Python310\\Lib\\site-packages\\autoit\\lib\\AutoItX3_x64.dll", "autoit\\lib"]],
hiddenimports=["autoit.init", "autoit.autoit", "autoit.control", "autoit.process", "autoit.win" ],
Here is an example and don't forget to copy and paste the autoit\lib into your dist folder.

Adding an individual file to PyInstaller

I'm attempting to use PyInstaller with pyarmor. I'm using pyarmors "super mode", which creates pytransform.pyd at the root of my module.
For the record, I'm not using a spec file, instead I'm doing it python with PyInstaller.__main__.run(args). Not sure if that makes any difference.
Directory structure:
c:/dist/
module/
*.py # Obfuscated code
data/
stuff.json
pytransform.pyd
I'm already using --add-data C:/dist/module;module and --add-data C:/dist/data;data to include most of the necessary files, however I'm struggling with pytransform.pyd.
If I use --add-data C:/dist/pytransform.pyd;pytransform.pyd, I get this error when running the built executable. It looks like it's assuming it's a directory.
pytransform.pyd\pytransform.pyd could not be extracted!
fopen: No such file or directory
If I use --add-binary C:/dist/pytransform.pyd;pytransform.pyd, I get this error when building:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Peter\\AppData\\Roaming\\pyinstaller\\bincache00_py37_64bit\\pytransform.pyd\\pytransform.pyd'
I checked the bincache folder, and it actually contains pytransform.pyd, so it's halfway there.
How can I add just a single file to the executable?
It worked when adding it as a hidden import, using --hidden-import=pytransform.
I would still be interested knowing why the --add-data and --add-binary didn't work though.

Generate an executable from python project

I need to generate an executable from a python project containing multiple folders and files.
I tried to work with library cx_Freeze, but only worked for a single file project.
Can you tell me how to do please?
Running pyinstaller on your "main" python file should work, as PyInstaller automatically imports any dependencies (such as other python files) that you use.
use pyinstaller. just run
pip install pyinstaller and then open the folder the file is located in with shell and run pyinstaller --onefile FILE.py where file is the name of the python file that should be run when the exe is run
Here is what you have to do:
Create a build.py file with instructions like this:
import cx_Freeze
executables = [cx_Freeze.Executable("main.py")] # The main script
cx_Freeze.setup(
name="Platform Designer", # The name of the exe
options={"build_exe": {
"packages": ["pygame"], # Packages used inside your exe
"include_files": ["data", "instructions.md"], # Files in the exe's directory
"bin_path_includes": ["__custom_modules__"]}}, # Files to include inside the exe
executables=executables
)
Run in command prompt:
python build.py build to build a exe
python build.py bdist_msi to build an msi installer
Make sure to remove the build and dist folders whenever you're updating your exe or msi.

No such file or directory error using pyinstaller and scrapy

I have a python script that uses scrapy and I want to make it into an exe file using pyinstaller. The exe file is generated without any error but when I open it an error occurs.
FileNotFoundError: [Errno 2] No such file or directory: '...\\scrapy\\VERSION'
I have tried reinstalling scrapy but that did not help. I am using windows 10 with python3
Full Disclosure: This is a repost of my answer to a duplicate, similar question. I am just putting it here for visiblity. This genuinely answers the question asked; so it is relevant.
You did not use Pyinstaller properly when you had built your stand-alone program. Here is a short, layman's description of how Pyinstaller works: Pyinstaller bundles the Python interpreter, necessary DLLs (for Windows), your project's source code, and all the modules it can find into a folder or self-extracting executable. Pyinstaller does not include modules or files it cannot find in the final .exe (Windows), .app (macOS), folder, etc. that results when you run Pyinstaller.
So, here is what happened:
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/_MEIbxALM3/scrapy/VERSION'
You ran your frozen/stand-alone program. As soon as you did this, your program was 'extracted' to a new, temporary folder on your computer /temp/_MEIbxALM3/. This folder contains the Python interpreter, your program's source code, and the modules Pyinstaller managed to find (plus a couple other necessary files).
The Scrapy module is more than just a module. It is an entire framework. It has its own plain text files (besides Python files) that it uses. And, it imports a lot of modules itself.
The Scrapy framework especially does not get along with Pyinstaller because it uses many methods to import modules that Pyinstaller cannot 'see'. Also, Pyinstaller basically makes no attempt to include files in the final build that are not .py files unless you tell it to.
So, what really happened?
The text file 'VERSION' that exists in the 'normal' scrapy module on your computer (that you had installed with pip or pipenv) was not included in the copycat scrapy module in the build of your program. Scrapy needs this file; Python is giving you the FileNotFoundError because it simply was never included. So, you have to include the file in the build of your program with Pyinstaller.
How do you tell Pyinstaller where to find modules and files?
This guy says to just copy the missing files from where they are installed on your computer into your build folder spit out from Pyinstaller. This does work. But, there is a better way and Pyinstaller can do more of the work for you (preventing further ImportErrors and FileNotFoundErrors you may get). See below:
build.spec Files are Your Friend
spec files are just Python files that Pyinstaller uses like a configuration file to tell it how to build your program. Read more about them here. Below is an example of a real build.spec file I used recently to build a Scrapy program with a GUI for Windows (my project's name is B.O.T. Bot):
import gooey
gooey_root = os.path.dirname(gooey.__file__)
gooey_languages = Tree(os.path.join(gooey_root, 'languages'), prefix = 'gooey/languages')
gooey_images = Tree(os.path.join(gooey_root, 'images'), prefix = 'gooey/images')
a = Analysis(['botbotgui.py'],
pathex=['C:\\Users\\Colton\\.virtualenvs\\bot-bot-JBkeVQQB\\Scripts', 'C:\\Program Files (x86)\\Windows Kits\\10\\Redist\\ucrt\\DLLs\\x86'],
hiddenimports=['botbot.spiders.spider'],
hookspath=['.\\hooks\\'],
runtime_hooks=None,
datas=[('.\\spiders\\','.\\spiders\\'), ('.\\settings.py','.'),
('.\\scrapy.cfg','.'), ('.\\items.py','.'), ('.\\itemloaders.py','.'),
('.\\middlewares.py','.'), ('.\\pipelines.py','.')
]
)
pyz = PYZ(a.pure)
options = [('u', None, 'OPTION'), ('u', None, 'OPTION'), ('u', None, 'OPTION')]
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
options,
gooey_languages, # Add them in to collected files
gooey_images, # Same here.
name='BOT_Bot_GUI',
debug=False,
strip=None,
upx=True,
console=False,
windowed=True,
icon=os.path.join(gooey_root, 'images', 'program_icon.ico'))
#coll = COLLECT(exe,
#a.binaries,
#a.zipfiles,
#a.datas,
#options,
#gooey_languages, # Add them in to collected files
#gooey_images, # Same here.
#name='BOT_Bot_GUI',
#debug=False,
#strip=False,
#upx=True,
#console=False,
#windowed=True,
#icon=os.path.join(gooey_root, 'images', 'program_icon.ico'))
Uncomment the last region if you want to build a folder instead of a stand-alone .exe. This is a configuration file specific to my computer and project structure. So in your file, you would have to change a few things (for example pathex to tell Pyinstaller where to find DLLs on Windows 10. But, the premise is the same.
My project directory looks like this:
botbotgui.py botbot.py hooks images __init__.py itemloaders.py items.py middlewares.py pipelines.py __pycache__ scrapy.cfg settings.py spiders
Pay special attention to the hooks/ directory. Using hooks will save you from a lot of headaches down the road. Read more about Pyinstaller's hooks feature here. In the hooks/ directory there is a hook file for Scrapy. This will tell Pyinstaller to include many modules and files it would have otherwise missed if you did not use a .spec file. This is the most important thing I have wrote here so far. If you do not do this step, you will keep getting ImportErrors every time you try to run a Scrapy program built using Pyinstaller. Scrapy imports MANY modules that Pyinstaller misses.
hook-scrapy.py (Note: Your hook file must be named just like this.):
from PyInstaller.utils.hooks import collect_submodules, collect_data_files
# This collects all dynamically imported scrapy modules and data files.
hiddenimports = (collect_submodules('scrapy') +
collect_submodules('scrapy.pipelines') +
collect_submodules('scrapy.extensions') +
collect_submodules('scrapy.utils')
)
datas = collect_data_files('scrapy')
After you finished writing a proper build.spec file, all you need to do is run Pyinstaller like this in your shell prompt:
pyinstaller build.spec
Pyinstaller should then spit out a proper build of your program that should work. Problem solved.
You can find that file in scrapy package. Go to this Path: Python/Lib/site-packages/scrapy, and you will find that file. Here are the steps that you are gonna do next:
Go to the directory where your exe file is, and create a folder named scrapy.
Copy VERSION and mime.types from the Path above. (If you don't copy mime.types, there will just be another no such file error. I'm just saving your time)
Paste those two files to the scrapy folder you created in step 1.
Now open your exe file. If No Module Error shows up, you just need to import the corresponding module in the file to which you want to use Pyinstaller.
Good luck. :)

Py2exe Error creating exe - Directory

So I'm just trying to create an exe from a simple hello world program using py2exe. I have created the setup file (setup.py) with the following code:
from distutils.core import setup
import py2exe
setup(console=['hello.py'])
(I have also tried setup(console=[{"script":'hello.py'}]) as per another suggestion )
I run cmd as an administrator and type C:\python27\setup.py py2exe
It then says:
error: hello.py: No such file or directory
I have the hello.py file in the same folder as the setup file. Looked at many different forums and py2exe tutorials and none seem to have answers, any help would be greatly appreciated.
You have to first navigate to that directory and then make the python call:
C:>cd C:\python27
C:\python27>python setup.py py2exe
Note: your hello.py script should also be in C:\python27 directory.

Categories