I have successfully bundled several of my python scripts into exe files using pyinstaller. However, I have hit a problem with a different python script that uses local data files. Using this question and answer PyInstaller 2.0 bundle file as --onefile I have got the script using my local files however I get an error.
I have a master python script called "translate.py" in it I have several subprocesses that call a different python script call "loader.py" which takes a variety of arguments. So my normal subprocess looks like this
python.exe loader.py loader.config src_dir=data out_dir=sql tmp_dir=temp
In my pyinstaller version I have used the answer from the above question to have the following subprocess call
python.exe C:\Users\AppData\Local\Temp\_MEI70922\loader.py C:\Users\AppData\Local\Temp\_MEI70922\loader.config src_dir=dat
a out_dir=sql tmp_dir=temp
However this fails to run the subprocess and I get the following error
no module named site
So I am wondering if pyinstaller is not including all the modules I may need?
I have tweaked the spec file to analyse all the python scripts like this
a = Analysis(['c:\\temp\\translate.py','c:\\temp\\loader.py','c:\\temp\\prep.py','c:\\temp\\prep2.py'],
pathex=['C:\\Temp\\pyinstaller-2.0\\pyinstaller-2.0'],
hiddenimports=[],
hookspath=None)
But again I get the no module named site.
I know my code could be better but I am stuck as someone else wrote the loader.py and I wrote the translate.py and now I need to bundle it all into a simple exe file.
thanks for anyone's help
Related
I created a server and client file and I was about to convert it to exe when I realized that I also need to convert the module it uses to exe. What do I do about the module?
It looks like Auto PY to EXE uses PyInstaller to do the real work here:
A .py to .exe converter using a simple graphical interface and PyInstaller in Python.
PyInstaller will automatically bundle any dependencies it finds:
What other modules and libraries does your script need in order to run? (These are sometimes called its "dependencies".)
To find out, PyInstaller finds all the import statements in your script. It finds the imported modules and looks in them for import statements, and so on recursively, until it has a complete list of modules your script may use.
So as long as you import your dependencies somewhere reachable from your main script and your virtual environment is active, you should get the right behaviour automatically.
Note that you'll probably need to build two executables: one for your server and a separate one for your client.
I am trying to compile a python file with Nuitka in stead of Pyinstaller. Everything is going great except for the facts that I do not understand how I should add another data file to the python file.
The problem
If I want to add an image to my python file in Pyinstaller (in Windows) I would do:
wine /root/.wine/drive_c/Python27/pyinstaller.exe --add-data "/root/Downloads/car.jpg;." --onefile --noconsole --icon /root/Downloads/icon.ico pythonfile.py
Now if I would open this exe file I would run the python file and open the car.jpg file.
I want to do something similar using Nuitka. When I looked at the documentation of Nuitka I saw that I probably needed to use the --include-data-file=<source>=<target> argument.
I tried this and it gave no errors, but when I open the created exe file, it does not open the given file. All the other arguments worked as I wanted, so only the --include-data-file argument does not give the wanted result
This is the Nuitka command I tried:
.\python.exe -m nuitka --mingw64 .\pythonprogram.py --standalone --onefile --windows-icon-from-ico=pdf.ico --windows-disable-console --include-data-file=C:\Users\User\AppData\Local\Programs\Python\Python39\*.pdf=mypdf.pdf
My question(s):
Am I using the correct argument?
Is this even possible with Nuitka?
How would I fix my problem?
Thanks in advance!
First of all, when encountering unexpected behaviour in Nuitka (in this case, you'd expect a file to open, which it didn't) I highly recommend removing the --windows-disable-console argument and observing the terminal output; without it debugging the compiled program is virtually impossible. You can also get a lot of information by analyzing Nuitka's output during transpiling and compilation (for example, there may appear a warning informing you about including numpy's plugin in order to add support for the numpy module).
Now, moving on to your questions:
Am I using the correct argument?
Yes, argument --include-data-file=<source>=<target> is correct if what you want is to embed one file.
What is however very important when it comes to programs compiled with --onefile argument is how the files are loaded internally, in the .py files.
I feel like the best thing I can do is show an example of my own (I'm using pygame for my project, but this way of loading files should work in any circumstance):
import os
import pygame
def load_file(file_name: str) -> str:
return os.path.join(os.path.dirname(__file__), file_name)
image_1_sprite = pygame.image.load(load_file("textures/image_1.png"))
I would then use Nuitka as such: python -m nuitka --onefile --include-data-dir=../workingdir/textures=textures main.py or, if you want to include just one file, python -m nuitka --onefile --include-data-file=../workingdir/textures/image_1.png=textures/image_1.png main.py, where workingdir is the directory in which main.py resides (I recommend using relative paths to make the repository portable, because if someone were to download your source code and save it, for example, on the D:\ hard drive - the executed command would fail).
I have a question if I use Pyinstaller to convert python file to exe will convert the modules with or not? , because i have python file with a lot of modules and i want to convert it how i can do it and avoiding this issue,
Thank u.
import requests
error:
ImportError: No module named requests
.
After discussion in comments basically the error is occurring when you try to open the generated file in another computer, however your aren't using any virtual environment so you can install the requirements and try to rebuild again but rather you want a stanalone exe file.
For that use :
pyinstaller --onefile your-script.py
# or pyinstaller -F your-script.py
## this should generate a stand alone executable file located in the dist folder.
About your concerns on how pyinstaller works
Does pyinstaller make copies of modules when building ?
The answer is simply : yes , as mentioned in the docs here PyInstaller reads a Python script written by you. It analyzes your code to discover every other module and library your script needs in order to execute. Then it collects copies of all those files – including the active Python interpreter! – and puts them with your script in a single folder, or optionally in a single executable file.
However, the variations of Python and third-party libraries are endless and unpredictable, if something goes wrong you can learn how to fix those issues by reading this page on the docs here
What to generate ?
you can read more here
Create a one-folder bundle containing an executable (default), -D, --onedir
Create a one-file bundled executable. you need to use -F ore --onefile
Finally
I highly encourage you to use separate virtual environment for each project.
I have a python application that makes use of some libraries including python-docx. Inside the application I have a templates/*.docx directory that contains some docx files with empty tables that I copy and fill in the app logic. And this works fine when I run the program from the command line.
However when I use pyinstaller --onefile --add-data templates/*docx;. main.py (I'm on windows) then then pyinstaller creates an exe, but when executing that exe I get an error in the command line: Error: Package not found at 'medium_risk.docx'. Note this in a try/catch block where I print the exception. Tried a lot of different things but just can't get it to work.
Anyone has an idea? That would be great.
I am trying to add exactly eight .png files and one .json file to a .app I am trying to create using PyInstaller, or, if I have to, I can use Py2App. The program I am trying to turn to a .app is a tkinter GUI application, if that's relevant in any way.
With PyInstaller I can get the .app to form correctly by doing
pyi-makespec --windowed --icon myicon.icns myapp.py
then adding the files under the datas attribute in the .spec file, and then finally doing
pyinstaller myapp.spec
However, when I run the program its raising a python error saying it can't locate the files, even though I checked inside the .app and they are there. While troubleshooting, I tried doing print(os.getcwd()) and it said it was in /User/Robert, which I presume is the problem (the .app is not located in that directory).
As for using Py2App, I can't even figure out how to properly add the files. I tried
py2applet --make-setup --iconfile myicon.icns myapp.py
and then added all their paths within the generated DATA_FILES list, but I just get an error while it is forming the .app after executing
python3 setup.py py2app
So if anybody knows how I can form a .app file from a tkinter application that makes use of certain files, it would be greatly appreciated.
Thanks in advance.
PS: I am using python 3.5.1 (for both the .py file being converted and the conversion process) and my mac OS X version is 10.11, if that helps in any way.
PPS: I have been able to get this exact same program to work on windows in the form of a .exe (converted using PyInstaller).