Adding an individual file to PyInstaller - python

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.

Related

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

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

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.

FileNotFoundError Selenium Stealth Python

I am running selenium-stealth in Python 3.9.3 on MacOS.
When I build to an app using PyInstaller (
pyinstaller --noconfirm --onefile --console --hidden-import "cmath" "/Users/name/Downloads/AmazonSellerCentral/main.py") I get the following error:
Traceback (most recent call last):
File "main.py", line 141, in <module>
File "main.py", line 77, in create_driver
File "selenium_stealth/__init__.py", line 45, in stealth
File "selenium_stealth/utils.py", line 8, in with_utils
File "pathlib.py", line 1255, in read_text
File "pathlib.py", line 1241, in open
File "pathlib.py", line 1109, in _opener
FileNotFoundError: [Errno 2] No such file or directory: '/var/folders/7z/3spcsmdx5n344dh0fb5j0hl40000gn/T/_MEIqfmicz/selenium_stealth/js/utils.js'
.
However, simply running python3 main.py works fine.
Reinstalling selenium-stealth did nothing, I'm unsure what I can try to fix/change from here?
quick turnaround (for issue pls refer to my comment in comments section):
step 1: build the package , refer the command in code section)
step 2: create folder --> dist\your package(in my case slhpackage)\selenium_stealth\js
step 3- copy the js files from https://github.com/diprajpatra/selenium-stealth/tree/main/selenium_stealth/js and put it in folder created in step 2.
and now your executable should run fine.
pyinstaller slhpackage.py # build package (1)
#Python version ==> Python: 3.9.2 (conda)
I used Python 3.9 and encountered the same issue today. As #simpleApp said, I followed the same with least number of modifications and it works for me as of now.
Follow below given steps:
Make sure you that you copy all the files from this link.
Paste all the files in folder_name(might be anthing you wish)/selenium_stealth/js.
[Make sure you that you keep the names selenium_stealth/js with no modifications at any cost, else you'll face conflicts.]
Then run the executable command with the folder_name added.
For convenience, I'm adding it here so that it'd be easier not only for you, but for everyone who uses it.
pyinstaller --noconfirm --onefile --console --icon "dir:\path\to\icon.ico" --add-data "dir:\path\to\folder_name\mentioned\above;." --add-binary "dir:/path/to/chromedriver/chromedriver.exe;./selenium/webdriver" "dir:/path/to/your/python/file.py"
Copy the code, modify it based on your file paths (I've given generic names).
I'd recommend you to make a .bat file for paste the code in it for future use.
Here's the snippet of my code:
#ECHO OFF
color 09
echo Immediate direct Conversion of .py to .exe standalone by PiXinCreate
echo.
pyinstaller --noconfirm --onefile --console --icon "dir:\path\to\icon.ico" --add-data "dir:\path\to\folder_name\mentioned\above;." --add-binary "dir:/path/to/chromedriver/chromedriver.exe;./selenium/webdriver" "dir:/path/to/your/python/file.py"
echo.
echo .exe has been successfully created for .py
echo.
pause.
exit
My code actually creates a onefile, which is standalone executable, in the terminal window.
You can remove the icon part if not needed.
Feel free to revert back if anything goes wrong.
The error that you're getting indicates that maybe something is wrong with the path of the file.
Make sure that the file utils.js exists at the path where it's trying to be located.
If not you could try to download it externally and placing it inside that path. I don't think that would be a good recommendation though.
first remove --onefile from the command and build the app, after building you would find selenium-stealth folder inside of the folder that apeared after build,put this folder(js) inside of selenium-stealth and run the app.
i was looking for solution and it worked for me.

How to specify pyinstaller resource files correctlly?

I want to make a runnable .py application by double click in Linux.
First, I tried a simple example:
#!/usr/bin/env python3
import cv2
img= cv2.imread('/home/andrei/WTZ/code/Computer-Vision-with-Python/DATA/test_image.jpg')
cv2.imshow('It works?', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
It just opens an image, nothing special.
I installed pyinstaller and I ran the following command in the terminal:
pyinstaller --onefile --add-data="/home/andrei/WTZ/code/Computer-Vision-with-Python/DATA/test.jpg;/home/andrei/WTZ/code/Computer-Vision-with-Python/DATA/test.jpg" test.py
And I encounter the following error:
pyinstaller: error: argument --add-data: invalid add_data_or_binary value: '/home/andrei/WTZ/code/Computer-Vision-with-Python/DATA/test.jpg;/home/andrei/WTZ/code/Computer-Vision-with-Python/DATA/test.jpg'
What am I doing wrong?
Ran this on my machine
pyinstaller --add-binary C:\Users\jezequiel\Desktop\diagram_new.png;.\images d.py
and the .PNG file was copied to a subdirectory named images in the folder containing the d.exe file.
So recommended approach is the first make a main.spec file -
pyi-makespec --windowed main.py
you should then get a main.spec file that you can call pyinstaller with - so you don't need to pass in all resource files via command line.
Then, in your main.spec file (which is actually python code), you should have something like
a = Analysis(
...
)
Assuming your resources are in a resources directory in the root of your project, you can add them all to your build by adding a line that recursively walks through this directory and adds all files. Your app should then be able to reference these files.
a = Analysis(
...,
datas = [(os.path.join(dp, f), dp) for dp, dn, filenames in os.walk('./resources') for f in filenames],
)

Python/Pyinstaller - onefile executable

In order to create a python .exe file I have been using pyinstaller and this command:
pyinstaller --onefile -w -i favicon.ico "program.py"
This creates a /dist folder which contains the generated .exe file.
The problem is that I am not able to run this .exe file without including the following program files inside the .exe launching folder.
+ Dir
- favicon.ico
- logo.gif
- data.csv
- program.exe
How can I include the .ico, .gif and .csv INSIDE the .exe so it truly becomes "onefile"?
I'm quite new to python so I apologize in advance if the code is a little chaotic. I faced a similar problem with .csv files. I managed to pack them into .py files by once running this code:
import csv
myfinalvariable=[]
with open(PathToOriginalCsv + '\\' + 'NameOfCsv.csv', newline='') as csvfile:
myfirstvariable = csv.reader(csvfile, delimiter=',', quotechar='|')
for line in myfirstvariable:
myfinalvariable.append(' '.join(line).split())
pyfile=open('PathToDesiredFile\mynewfile.py', 'w')
pyfile.write('newcsv=%s' %(myfinalvariable))
pyfile.close
You can iterate this if you have multiple csv files. Now you have the py file with 'variables' and you can 'forget' about the csv files. Because if you put the created py file into your 'project folder' and put:
from mynewfile import newcsv, newcsv2, ...
into your code, you can modify your code to use the variables 'newcsv', 'newcsv2', etc. instead of loading the original csv files. When you use pyinstaller with the --onefile parameter, it packs the 'mynewfile.py' file into the created exe file. Pyinstaller 3.0 also packs the .ico file when using the parameter --icon=favicon.ico. Tested on Windows, Python3.4, Pyinstaller3.0. I understand this is an old question, I hope this helps someone who stumbles upon it.
By writing a shell script, that can be executed by powershell,
The file can be made and written as a .exe file
The other files can be moved into the new directory.
Now all that needs done is having the Powershell script to run.
You can package the files with pyinstaller's --add-data option. For example with your files you should try:
> pyinstaller --onefile -w -i favicon.ico "program.py" --add-data "favicon.ico:favicon.ico'\
--add-data "lgog.gif:logo.gif" --add-data "data.csv:data.csv"
On other OS it may be required to replace the \ with a ^ (or do it all on one line.)
This should package all the files into the exe.
If you want to access these files from the code you need to add a little bit extra, otherwise the program will not find them.
import os, sys
def resource(relative_path):
if getattr(sys, 'frozen', False):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath('.'), relative_path)
When pyinstaller compiles a script it sets the _MEIPASS variable to the temporary path of the created files at run-time. This script harnesses that to locate these file and defaults back to the ordinary path in un-compiled mode read more.
To access the files from the code, just replace all links to the files with resource('myfile.etc'). For example, using youe data.csv file
with open(resource('data.csv'), 'r') as csvfile:
# do stuff

Categories