pyinstaller and moviepy, ffmpeg works from terminal but not from finder - python

I am packaging python using pyinstaller 2.1 on OSX Mavericks. I have done this successfully in the past, but this is my first package that uses moviepy and ffmpeg. I use the following import:
from moviepy.video.io import ffmpeg_reader
Without this line in the code, everything works fine and I can launch my final package from its icon in finder. With the moviepy import, it will work if I launch from the terminal like this:
open ./myapp.app
but it will not open if I click on the icon from finder (opens quickly and crashes). I am assuming this has something to do with paths or environment variables that are set in terminal, but are not transferred to my packaged app. I have tried various hidden imports in pyinstaller for moviepy and its dependencies, but nothing seems to work. --debug mode hasn't provided much info to track it down. Any other ideas?
Thanks!

There are a few problems with moviepy and pyinstaller.
First, try writing the error to a text file
try:
from moviepy.video.io import ffmpeg_reader
except Exception as e:
with open('/absolute/path/to/error.txt',mode="w+") as f:
f.write(str(e))
1) You might need to modify these two files to remove the "exec" import statements
moviepy/audio/fx/all/__init__.py,
moviepy/video/fx/all/__init__.py
see here:
https://github.com/pratikone/videoVenom/blob/master/moviepy/audio/fx/all/__init__.py
https://github.com/pratikone/videoVenom/blob/master/moviepy/video/fx/all/__init__.py
2) You might need to have this statement imageio.plugins.ffmpeg.download(), so that ffmpeg is downloaded if not found.

Related

Python code wont work outside IDE because of libraries

I have a problem with some libraries. My python code works just fine in the IDE (spyder) but when I try to run it outside the IDE and whenever I import some libraries (especially numpy,pandas), it wont run. It runs well when I import usual libraries like Tkinter, but when I import other ones the screen just flashes and closes. I have been able to capture a screenshot of the issue:
I have installed the MKL service package as specified but nothing changed.
Try this method:
Download msvcp71.dll and msvcr71.dll from the web.
Save them to your C:\Windows\System32 folder.
Save them to your C:\Windows\SysWOW64 folder (if you are using a 64-bit OS)
Then try running your code again.
I recommend using a virtual environment to manage your dependencies.
Personally I use pipenv but there are other options.
Do you use spyder in combination with anaconda?

Pyinstaller does not create functional executable with pdfrw

I'm trying to create an executable file using pyinstaller, but it crashes immediately upon launching that executable. It should be noted that the program works perfectly fine inside of PyCharm. I've narrowed it down to only one line of code that causes it to error using another python file - if the file contains
'''import pdfrw'''
it will crash immediately upon launching the executable file created by pyinstaller.
I've tried the following commands from a command prompt running in administrator mode, all of which give that same result (this is after changing the directory to where the .py file is located):
pyinstaller "compiling test.py"
pyinstaller --onefile "compiling test.py"
pyinstaller --onedir "compiling test.py"
Using a try/except clause around the import statement, I'm able to see the error message before it closes the executable, and it says:
Cannot load native module 'Crypto.Cipher._raw_ecb': Trying '_raw_ecb.cp36-win_amd64.pyd': cannot load library 'C:\Users\KYLE~1.AFF\AppData\Local\Temp_MEI534042\Crypto\Util..\Cipher_raw_ecb.cp36-win_amd64.pyd': error 0x7e. Additionally, ctypes.util.find_library() did not manage to locate a library called 'C:\Users\KYLE~1.AFF\AppData\Local\Temp\_MEI534042\Crypto\Util\..\Cipher\_raw_ecb.cp36-win_amd64.pyd', Trying '_raw_ecb.pyd': cannot load library 'C:\Users\KYLE~1.AFF\AppData\Local\Temp_MEI534042\Crypto\Util..\Cipher_raw_ecb.pyd': error 0x7e. Additionally, ctypes.util.find_library() did not manage to locate a library called 'C:\Users\KYLE~1.AFF\AppData\Local\Temp\_MEI534042\Crypto\Util\..\Cipher\_raw_ecb.pyd'
Is there a way I can modify the spec file to make the file correctly (or another solution)? I found something similar when I needed to make an executable with python-docx (and I will most likely need to do this for this program as well, as the original python program I'm trying to make an executable uses python-docx as well as pdfrw): PyInstaller and python-docx module do not work together
Thanks for your help in advance.
So I just found a solution to this! It tipped me off when it was going down a user path named \KYLE~1.AFF\ because that user shouldn't exist, though somehow it got created. After removing that user file, I upgraded pyinstaller via pip install --upgrade pyinstaller, and it uninstalled version 3.3.1, and installed version 3.6, and now it works fine. So possibly it was the new version, but I'm guessing it was more likely the fresh install after I removed the user directory that shouldn't have existed. Not sure why it only errored out with that one library though.

I'm getting this error when I'm trying to make an .exe file through pyinstaller - "ModuleNotFoundError: No module named 'acrcloud_extr_tool'"

Im using ACRCloud to recognize an audio file, I've build a GUI using tkinter and I'm freezing the code as an .exe file using PyInstaller. However, I'm getting this error when I'm running the .exe file:
ModuleNotFoundError: No module named 'acrcloud_extr_tool'
If I run it directly from the script, there's no error and it runs fine. Some help, please? I'm just starting out.
From my experience using pyinstaller, I need to add a parameter to the build command, so that pyinstaller knows where to look for modules. If you are building from a command prompt, the line may read something like this:
pyinstaller "yourFileName.py"
However, you can add other commands to this that specify how the exe is built - whether it has a custom icon, is console based or the console is hidden, etc. Additionally, you can add a list of paths, telling pyinstaller where to look for your modules and that's done like this:
pyinstaller -p C:\theFolderWhereYourCustomModulesAreSaved:C:\Users\yourName\AppData\Local\Programs\Python\Python36-32\Lib\site-packages "yourFileName.py"
Notice there are no quotation marks around these file paths AND that they are separated by a colon. The path to your Python site packages may be a bit different than mine, but I left in all the same path info with the exception of my username, so edit that as needed for your own machine. Also, the first 'fake' path I have shown in the example would be if you have written some of your own modules and are importing them into your project. For example, if your main project is saved in C:\myProject but you have modules you've written that are imported into your program like this:
import myCustomModule
and THOSE modules are saved in C:\myProject\myModules, then you would change that command to look like this:
pyinstaller -p C:\myProject\myModules:C:\Users\yourName\AppData\Local\Programs\Python\Python36-32\Lib\site-packages "yourFileName.py"
Hopefully this solves your problem.
I solved it. Turns out due to it being a binary file (of .pyd extension), it had to be added explicitly in the .spec file (read the pyinstaller documentation). I did it and it ran like a charm.

Suddenly .py files can't import modules but still works with CMD

I have a weird issue.
Main issue:
My .py files that used to work fine like 3 hours ago now can't import any external modules. I can still run them from Spyder (similar to a PyCharm editor) and from CMD with python run.py. However when clicked on I get the error ModuleNotFoundError: No module named ModuleName. However the module is found when running through everything else, the module is there in the Anaconda libs; the folder doesn't have any permisson restrictions, and it's not just one file it's any .py file that imports an external module.
At first I thought this may be a pip issue as I had just update to pip 18, but even when retracting to pip 10.0.1 the issue remains.
[EDIT]: I've tried making a PyInstaller .exe and that still works as intended, however the app still doesn't work with cx_freeze even though it used to a fez hours ago.
Backstory:
I was playing around with PyInstaller and Cx_Freeze to turn my app into an executable.
I have my working .py file that I edit and test inside of the Anaconda's Spyder app.
And so I'm testing the executables, and they work fine, just like my python code. The Pyinstaller standalone and the cx_freeze app work as intended.
So I change a few things in the main .py file (nothing crazy just removed a print('')), reuse cx_freeze and then at some point I start working on a setup wizard for my cx_freezed app.
It's all good except that when running the app, the cmd prompt just closes.
I think 'huh weird', I test the .py file in Spyder it works fine, so I screenshot what's written on the cmd : ModuleNotFoundError: No module named ModuleName, so I think it's an issue with the Wizard installer, so I try the original .exe file, same error. So I try the .py file and to my desmise, same error. I double check the modules, reinstall them succesfully, error persists.
And so I try to run a backup I know for sure worked and in which I haven't edited anything, and now same error.
This is really anoying as I want to make a .exe of the app, managed to and now nothing works anymore
Here are some things you can try.
Add this code to get a print out of the system path.
import sys
from pprint import pprint
pprint(sys.path)
That should tell you all the paths where modules can be loaded from. If your file is not in one of the paths it won't be loaded.
For a bit more info you can run python with the -v flag and it will verbosely let you know what is going on as python starts as well as when you attempt to load modules. You may be able to glean information about what is going wrong that way.
I think you used the wrong version when you converted from a .py to .exe. Generally the CMD uses whatever's in the ENV vars, so just make sure all version numbers are the same.

Windows- Pyinstaller Error "failed to execute script " When App Clicked

I am having a tough time overcoming this error, I have searched everywhere for that error message and nothing seems relevant to my situation:
"failed to execute script new-app"
new-app is my python GUI program. When I run pyinstaller using this command:
pyinstaller.exe --onedir --hidden-import FileDialog --windowed --noupx new-app.py
It does work smoothly. In addition, when I execute the command line to run the gui program, it works perfectly and the GUI is generated using this command:
.\dist\new-app\new-app.exe
But when I go to that file hopefully to be able to click the app to get the GUI, it gives me the error said above. Why is that?
I am using python2.7 and the OS is Windows 7 Enterprise.
Any inputs will be appreciated and thanks a lot in advance.
Well I guess I have found the solution for my own question, here is how I did it:
Eventhough I was being able to successfully run the program using normal python command as well as successfully run pyinstaller and be able to execute the app "new_app.exe" using the command line mentioned in the question which in both cases display the GUI with no problem at all. However, only when I click the application it won't allow to display the GUI and no error is generated.
So, What I did is I added an extra parameter --debug in the pyinstaller command and removing the --windowed parameter so that I can see what is actually happening when the app is clicked and I found out there was an error which made a lot of sense when I trace it, it basically complained that "some_image.jpg" no such file or directory.
The reason why it complains and didn't complain when I ran the script from the first place or even using the command line "./" is because the file image existed in the same path as the script located but when pyinstaller created "dist" directory which has the app product it makes a perfect sense that the image file is not there and so I basically moved it to that dist directory where the clickable app is there!
So The Simple answer is to place all the media files or folders which were used by code in the directory where exe file is there.
Second method is to add "--add-data <path to file/folder>"(this can be used multiple times to add different files) option in pyinstaller command this will automatically put the given file or folder into the exe folder.
In my case i have a main.py that have dependencies with other files. After I build that app with py installer using this command:
pyinstaller --onefile --windowed main.py
I got the main.exe inside dist folder. I double clicked on this file, and I raised the error mentioned above.
To fix this, I just copy the main.exe from dist directory to previous directory, which is the root directory of my main.py and the dependency files, and I got no error after run the main.exe.
Add this function at the beginning of your script :
import sys, os
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
Refer to your data files by calling the function resource_path(), like this:
resource_path('myimage.gif')
Then use this command:
pyinstaller --onefile --windowed --add-data todo.ico;. script.py
For more information visit this documentation page.
In case anyone doesn't get results from the other answers, I fixed a similar problem by:
adding --hidden-import flags as needed for any missing modules
cleaning up the associated folders and spec files:
rmdir /s /q dist
rmdir /s /q build
del /s /q my_service.spec
Running the commands for installation as Administrator
I was getting this error for a different reason than those listed here, and could not find the solution easily, so I figured I would post here.
Hopefully this is helpful to someone.
My issue was with referencing files in the program. It was not able to find the file listed, because when I was coding it I had the file I wanted to reference in the top level directory and just called
"my_file.png"
when I was calling the files.
pyinstaller did not like this, because even when I was running it from the same folder, it was expecting a full path:
"C:\Files\my_file.png"
Once I changed all of my paths, to the full version of their path, it fixed this issue.
I got the same error and figured out that i wrote my script using Anaconda but pyinstaller tries to pack script on pure python. So, modules not exist in pythons library folder cause this problem.
That error is due to missing of modules in pyinstaller. You can find the missing modules by running script in executable command line, i.e., by removing '-w' from the command. Once you created the command line executable file then in command line it will show the missing modules. By finding those missing modules you can add this to your command :
" --hidden-import = missingmodule "
I solved my problem through this.
I had a similar problem, this was due to the fact that I am using anaconda and not installing the dependencies in pip but in anaconda. What helped me was to install the dependencies in pip.
I found a similar issue but none of the answers up above helped. I found a solution to my problem activating the base environment. Trying once more what I was doing without base I got my GUI.exe executed.
As stated by #Shyrtle, given that once solved my initial problem I wanted to add a background image, I had to pass the entire path of the image even if the file.py and the image itself were in the same directory.
In my case (level noob) I forgot to install library "matplotlib". Program worked in Pycharm, but not when I tried open from terminal. After installed library in Main directory all was ok.

Categories

Resources