How can I convert a package to an exe using pyinstaller? - python

I've got an open source python command line program that runs on Python 2.7, Python3+, and is cross platform.
I'm trying to package it up into an executable for my windows users more easily. The source for this package is up on Github here: https://github.com/stormpath/stormpath-cli
I'm trying to package my Python program up using pyinstaller, but am having issues.
I'm running the following commands from a Windows 8 box:
$ pyinstaller --onefile setup.py
This successfully generates an EXE file for me, but when I go to run it, I get the following errors:
Traceback (most recent call last):
File "setup.py", line 4, in <module>
File "c:\python27\Lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
exec(bytecode, module.__dict__)
File "site-packages\setuptools\__init__.py", line 160, in <module>
File "site-packages\setuptools\monkey.py", line 93, in patch_all
File "site-packages\setuptools\monkey.py", line 145, in patch_for_msvc_specialized_compiler
File "importlib\__init__.py", line 37, in import_module
ImportError: No module named msvc
Failed to execute script setup
For testing purposes, to help narrow the issue down, I created a test.py script that contains the following code:
print('hello, world!')
And then packaged that into an exe as well:
$ pyinstaller --onefile test.py
When I run this resulting exe, everything works great! Hello world is output as expected.
I believe what's happening is that I'm not telling pyinstaller how to properly 'detect' that my project is a python package, and not a single file script.
I've read through the docs a lot, and have googled around, but haven't found a way to specify a package for pyinstaller to analyze.
What am I missing?

While I think it is a perfectly reasonable thing to do, it looks like PyInstaller simply doesn't support building an application from a package (with __main__.py).
See https://github.com/pyinstaller/pyinstaller/issues/2560.
As a workaround, you can write a small stub (outside of the package) that does the same the same thing as your __main__.py. Then point PyInstaller at that.

I think you forgot "pyinstaller -w --onefile test.py". You forgot '-w'.

Firstly install pyinstaller
pip install pyinstaller
To create exe executable folder, just run the following command:
pyinstaller exam_browser.py
If you want single exe file with a logo run this command:
pyinstaller exam_browser.py --onefile -F --icon logo.ico

Related

Pyinstaller,how to solve "ModuleNotFoundError: No module named 'cogs'" error while trying to convert py file to.exe?

I've been trying to convert my main.py file to .exe so users don't have to install python
pyinstaller --onefile -w main.py
.After I create the .exe file,I try to run the exe and i get the above error.I've been using cogs for categorizing my commands.In the below photo,these modules are must be used in .exe version.How can i do that?Could someone help me?
The full error message:
Traceback (most recent call last):
File "main.py", line 212, in <module>
client.load_extension(f"cogs.{filename[:-3]}")
File "discord\ext\commands\bot.py", line 674, in load_extension
File "importlib\util.py", line 94, in find_spec
ModuleNotFoundError: No module named 'cogs'
The photo of main.py and other files:
main.py and other files
#Convert .py to .exe don't use python ver 3.9 will not work, recommend 3.8:
pip install pyinstaller
#to convert to a simple exe file the exe file will be in your dist folder
pyinstaller 'fileName.py'
#to convert to a onefile exe file the exe file will be in your dist folder
pyinstaller --onefile 'fileName.py'
#to convert to a onefile exe file and the python window will not appear
pyinstaller -w --onefile 'fileName.py'

Trouble Generating .exe File Using Pyinstaller [duplicate]

I am trying to wrap a Python script into an exe using PyInstaller (development version) for windows.
The script uses Pandas and I have been running into an error when running the exe.
Traceback (most recent call last): File "site-packages\pandas\__init__.py", line 26, in <module> File "C:\Users\Eddie\Anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__) File "site-packages\pandas\_libs\__init__.py", line 4, in <module> File "C:\Users\Eddie\Anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 714, in load_module
module = loader.load_module(fullname) File "pandas/_libs/tslib.pyx", line 1, in init pandas._libs.tslib ModuleNotFoundError: No module named 'pandas._libs.tslibs.timedeltas'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "G5k Version file Extract (with tkinter).py", line 15, in <module> File "C:\Users\Eddie\Anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__) File "site-packages\pandas\__init__.py", line 35, in <module> ImportError: C extension: No module named 'pandas._libs.tslibs.timedeltas' not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first.
I have tried doing this for programs without pandas and everything was fine.
This is very similar to another question already solved for Python 2, but I am using Python 3 and that solution does not apply the same way due to the changed .spec file format.
Python 3.6
PyInstaller - version 3.3
Pandas - version 0.20.3
PyInstaller 3.3, Pandas 0.21.0, Python 3.6.1.
I was able to solve this thanks to not-yet published/committed fix to PyInstaller, see this and this. AND keeping the ability to pack it into one executable file.
Basically:
Locate PyInstaller folder..\hooks, e.g. C:\Program Files\Python\Lib\site-packages\PyInstaller\hooks.
Create file hook-pandas.py with contents (or anything similar based on your error):
hiddenimports = ['pandas._libs.tslibs.timedeltas']
Save it + I deleted .spec file, build and dist folders just to be sure.
Run pyinstaller -F my_app.py.
This fix should work as long as you don't upgrade or reinstall PyInstaller. So you don't need to edit .spec file.
Maybe they will include the fix sooner for us! :)
I'm not sure it may help you but following the solution on the post you mention work for me with python 3.6 pyinstaller 3.3 and pandas 0.21.0 on windows 7.
So adding this to the spec file just after analysis :
def get_pandas_path():
import pandas
pandas_path = pandas.__path__[0]
return pandas_path
dict_tree = Tree(get_pandas_path(), prefix='pandas', excludes=["*.pyc"])
a.datas += dict_tree
a.binaries = filter(lambda x: 'pandas' not in x[0], a.binaries)
Also my spec file format is the same as the one in the post you mention.
I managed to solve this problem by using the "--hidden-import" flag. Hopefully this can be helpful to someone else that comes across this thread.
pyinstaller --onefile --hidden-import pandas._libs.tslibs.timedeltas myScript.py
If you are using Anaconda, it is highly likely that when you were trying to uninstall some package it has disrupted pandas dependency and unable to get the required script. If you just run conda install pandas you might end up with another error:
module 'pandas' has no attribute 'compat'.
So, try uninstalling and reinstalling pandas conda uninstall pandas, Install it again using conda install pandas this will solve the problem.
On the other hand, if you are not using Anaconda., try doing the same on Command prompt pointing to Python scripts folder pip uninstall pandas & pip install pandas.
Most of the times, this should solve the problem. Just to be cover all the possibilities, don't forget to Launch Spyder from Anaconda after installing pandas.

Compiling pandas with PyInstaller [duplicate]

I am trying to wrap a Python script into an exe using PyInstaller (development version) for windows.
The script uses Pandas and I have been running into an error when running the exe.
Traceback (most recent call last): File "site-packages\pandas\__init__.py", line 26, in <module> File "C:\Users\Eddie\Anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__) File "site-packages\pandas\_libs\__init__.py", line 4, in <module> File "C:\Users\Eddie\Anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 714, in load_module
module = loader.load_module(fullname) File "pandas/_libs/tslib.pyx", line 1, in init pandas._libs.tslib ModuleNotFoundError: No module named 'pandas._libs.tslibs.timedeltas'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "G5k Version file Extract (with tkinter).py", line 15, in <module> File "C:\Users\Eddie\Anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__) File "site-packages\pandas\__init__.py", line 35, in <module> ImportError: C extension: No module named 'pandas._libs.tslibs.timedeltas' not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first.
I have tried doing this for programs without pandas and everything was fine.
This is very similar to another question already solved for Python 2, but I am using Python 3 and that solution does not apply the same way due to the changed .spec file format.
Python 3.6
PyInstaller - version 3.3
Pandas - version 0.20.3
PyInstaller 3.3, Pandas 0.21.0, Python 3.6.1.
I was able to solve this thanks to not-yet published/committed fix to PyInstaller, see this and this. AND keeping the ability to pack it into one executable file.
Basically:
Locate PyInstaller folder..\hooks, e.g. C:\Program Files\Python\Lib\site-packages\PyInstaller\hooks.
Create file hook-pandas.py with contents (or anything similar based on your error):
hiddenimports = ['pandas._libs.tslibs.timedeltas']
Save it + I deleted .spec file, build and dist folders just to be sure.
Run pyinstaller -F my_app.py.
This fix should work as long as you don't upgrade or reinstall PyInstaller. So you don't need to edit .spec file.
Maybe they will include the fix sooner for us! :)
I'm not sure it may help you but following the solution on the post you mention work for me with python 3.6 pyinstaller 3.3 and pandas 0.21.0 on windows 7.
So adding this to the spec file just after analysis :
def get_pandas_path():
import pandas
pandas_path = pandas.__path__[0]
return pandas_path
dict_tree = Tree(get_pandas_path(), prefix='pandas', excludes=["*.pyc"])
a.datas += dict_tree
a.binaries = filter(lambda x: 'pandas' not in x[0], a.binaries)
Also my spec file format is the same as the one in the post you mention.
I managed to solve this problem by using the "--hidden-import" flag. Hopefully this can be helpful to someone else that comes across this thread.
pyinstaller --onefile --hidden-import pandas._libs.tslibs.timedeltas myScript.py
If you are using Anaconda, it is highly likely that when you were trying to uninstall some package it has disrupted pandas dependency and unable to get the required script. If you just run conda install pandas you might end up with another error:
module 'pandas' has no attribute 'compat'.
So, try uninstalling and reinstalling pandas conda uninstall pandas, Install it again using conda install pandas this will solve the problem.
On the other hand, if you are not using Anaconda., try doing the same on Command prompt pointing to Python scripts folder pip uninstall pandas & pip install pandas.
Most of the times, this should solve the problem. Just to be cover all the possibilities, don't forget to Launch Spyder from Anaconda after installing pandas.

Pushbullet on python throws import error due to python-magic unable to find libmagic?

I'm trying to use PushBullet.py which uses python-magic which in turn uses libmagic.
I have followed the dependencies instructions and installed Cygwin and copied the three files to Windows\system32 directory but still getting the following error:
Traceback (most recent call last):
File "C:\New Python ActiveX Scripting Engine.py", line 1, in <module>
from pushbullet import PushBullet
File "C:\Python27\lib\site-packages\pushbullet\__init__.py", line 2, in <module>
from .pushbullet import PushBullet
File "C:\Python27\lib\site-packages\pushbullet\pushbullet.py", line 4, in <module>
import magic
File "C:\Python27\lib\site-packages\magic.py", line 161, in <module>
raise ImportError('failed to find libmagic. Check your installation')
ImportError: failed to find libmagic. Check your installation
EDIT
If I put cygmagic-1.dll OR cygz.dll into C:\Python27\ and rename it to magic.dll I get the following error:
Traceback (most recent call last):
File "C:\New Python ActiveX Scripting Engine.py", line 1, in <module>
from pushbullet import PushBullet
File "C:\Python27\lib\site-packages\pushbullet\__init__.py", line 2, in <module>
from .pushbullet import PushBullet
File "C:\Python27\lib\site-packages\pushbullet\pushbullet.py", line 4, in <module>
import magic
File "C:\Python27\lib\site-packages\magic.py", line 143, in <module>
libmagic = ctypes.CDLL(dll)
File "C:\Python27\lib\ctypes\__init__.py", line 365, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] The specified module could not be found
If I put cygwin1.dll into C:\Python27\ and rename it to magic.dll I get the following error:
Traceback (most recent call last):
File "C:\New Python ActiveX Scripting Engine.py", line 1, in <module>
from pushbullet import PushBullet
File "C:\Python27\lib\site-packages\pushbullet\__init__.py", line 2, in <module>
from .pushbullet import PushBullet
File "C:\Python27\lib\site-packages\pushbullet\pushbullet.py", line 4, in <module>
import magic
File "C:\Python27\lib\site-packages\magic.py", line 185, in <module>
magic_open = libmagic.magic_open
File "C:\Python27\lib\ctypes\__init__.py", line 378, in __getattr__
func = self.__getitem__(name)
File "C:\Python27\lib\ctypes\__init__.py", line 383, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'magic_open' not found
I'm doing this on Windows 7 64bit running Python 32bit 2.7.8 (fresh install today to try and resolve this problem).
Does anyone know how to resolve the problem?
EDIT: Tried on a further 5 different windows computers and all 5 have the same problem(s).
I had the same problem with python-magic and solved it by fixing the following line in the magic.py file (C:\Python27\lib\site-packages\magic.py in my PC):
dll = ctypes.util.find_library('magic') or ctypes.util.find_library('magic1')
Since I've installed libmagic from Cygwin, the DLL was named cygmagic-1.dll. So I simply added another choice in the previous line:
dll = ctypes.util.find_library('magic') or ctypes.util.find_library('magic1') \
or ctypes.util.find_library('cygmagic-1')
Don't forget to add cygwin\bin to your PATH.
EDIT:
It seems that this issue has been addressed on the GitHub repository.
EDIT2:
These are the steps I followed to make PushBullet.py work on my machine:
Install PushBullet.py via pip (pip install PushBullet.py);
Install libmagic from Cygwin;
Add C:\cygwin\bin\ to the PATH environment variable;
Change the magic.py file as I've explained above. (In my case the error was on line 139)
From the Windows paths in your traceback listings I guess that you are trying to execute PushBullet script from Windows version of Python. Why did you install Cygwin if you are not using it? Python for Windows really won't use Cygwin's DLLs.
You have to execute PushBullet.py from Cygwin using a Python for Cygwin, not from Windows Console using a Python for Windows. That means execute for example bash (something like C:\cygwin64\bin\bash.exe --login -i) and from bash execute PushBullet script: python PushBullet.py.
It expects, that you have Python and python-magic for Cygwin (for Cygwin! not Windows version) already installed.
Copying DLLs to your system directories is nonsense, don't do it. Renaming them is uber-nonsense. How did you expect it could work? Each library is specific, you can't just rename it to change how it works internally.
I'm already using PushBullet for some time, so I was interested in this particular Python script. I'm also using the Cygwin for years. So I have installed PushBullet library:
pip install pushbullet.py
Then I have created very simple script test.py:
#!/usr/bin/python
from pushbullet import PushBullet
pb = PushBullet('my_access_token')
success, push = pb.push_note("Testing title", "Hello world!")
When I executed it using ./test.py I got an error message, that I'm missing magic library, so I installed python-magic library using Cygwin's setup utility.
I executed it again and voila - I have "Hello world!" message on my phone. Done.
Just a note again: I have executed it from the Cygwin's shell (zsh, but you'll have bash I guess), not from Windows Console. I also didn't use Python for Windows but Cygwin's version. Don't mix Windows and Cygwin executables!
So dumb-proof steps would be:
download and execute setup.exe from Cygwin.com
install python and python-magic
execute Cygwin's shell (you can execute "Cygwin Terminal" from your Start menu or just execute C:\cygwin64\bin\bash.exe --login -i for example from "Run" dialog or Windows Console)
install pip (see for example Pip install not functioning on windows 7 Cygwin install)
install PushBullet library: pip install pushbullet.py
prepare your testing script
execute it: python testing_script.py or just testing_script.py if it contains the shebang line and is executable
it should work, if not, post your errors, please
Thank you for a tip to useful library :)
You cannot mix Cygwin and MSVCRT binaries. Therefore, in order to use python-magic with the Windows Python, you must get a Windows-compiled libmagic and fix magic.py to find the libmagic DLL.
Otherwise, if you want to use Cygwin's python-magic as-is, you need to use Cygwin's python packages.

Python with MySQL on Windows: installation errors

I tried to run the following command, in the folder of my Django project:
$ python manage.py dbshell
It shows me this error:
$python manage.py dbshell
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_manager(settings)
File "C:\Python25\lib\site-packages\django\core\management\__init__.py", line
362, in execute_manager
utility.execute()
File "C:\Python25\lib\site-packages\django\core\management\__init__.py", line
303, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python25\lib\site-packages\django\core\management\base.py", line 195,
in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Python25\lib\site-packages\django\core\management\base.py", line 222,
in execute
output = self.handle(*args, **options)
File "C:\Python25\lib\site-packages\django\core\management\base.py", line 351,
in handle
return self.handle_noargs(**options)
File "C:\Python25\lib\site-packages\django\core\management\commands\dbshell.py
", line 9, in handle_noargs
from django.db import connection
File "C:\Python25\lib\site-packages\django\db\__init__.py", line 41, in <modul
e>
backend = load_backend(settings.DATABASE_ENGINE)
File "C:\Python25\lib\site-packages\django\db\__init__.py", line 17, in load_b
ackend
return import_module('.base', 'django.db.backends.%s' % backend_name)
File "C:\Python25\Lib\site-packages\django\utils\importlib.py", line 35, in im
port_module
__import__(name)
File "C:\Python25\lib\site-packages\django\db\backends\mysql\base.py", line 13
, in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No mo
dule named MySQLdb
First question is, why does Python not simply include this MySQLdb module?
OK, just fine, try to search to solve this message.
I have looked around stackoverflow.com for installing this module but did not have a good result.
Download this module at: http://sourceforge.net/projects/mysql-python/
Comes to the installation of this module on my Windows Vista system.
After the extraction of the package, I run the cmd prompt to continue with the installation:
$ python setup.py install
Again it showed me one other message:
D:\SOFTWARE\PROGRAMMING\MySQL-python-1.2.3c1>python setup.py install
Traceback (most recent call last):
File "setup.py", line 5, in <module>
from setuptools import setup, Extension
ImportError: No module named setuptools
Playing around with this error message, I know that there is the ez_setup.py within the package:
$python ez_setup.py
It seems that everything is OK:
D:\SOFTWARE\PROGRAMMING\MySQL-python-1.2.3c1>python ez_setup.py
Downloading http://pypi.python.org/packages/2.5/s/setuptools/setuptools-0.6c9-py
2.5.egg
Processing setuptools-0.6c9-py2.5.egg
Copying setuptools-0.6c9-py2.5.egg to c:\python25\lib\site-packages
Adding setuptools 0.6c9 to easy-install.pth file
Installing easy_install-script.py script to C:\Python25\Scripts
Installing easy_install.exe script to C:\Python25\Scripts
Installing easy_install-2.5-script.py script to C:\Python25\Scripts
Installing easy_install-2.5.exe script to C:\Python25\Scripts
Installed c:\python25\lib\site-packages\setuptools-0.6c9-py2.5.egg
Processing dependencies for setuptools==0.6c9
Finished processing dependencies for setuptools==0.6c9
Now comes back to the setup.py to install again:
$python setup.py install
It again gave me one other error message:
D:\SOFTWARE\PROGRAMMING\MySQL-python-1.2.3c1>python setup.py install
running install
running bdist_egg
....
copying MySQLdb\constants\CLIENT.py -> build\lib.win32-2.5\MySQLdb\constants
running build_ext
error: Python was built with Visual Studio 2003;
extensions must be built with a compiler than can generate compatible binaries.
Visual Studio 2003 was not found on this system. If you have Cygwin installed,
you can try compiling with MingW32, by passing "-c mingw32" to setup.py.
How can I fix this problem?
Did you try looking here: http://sourceforge.net/projects/mysql-python/files/
That is the download area of MySQLdb project, it has nothing to do with django, so your question is incorrect - django does not make switching database backends hard, you just change one line. And of course, your python installation should support that database first, so by downloading binary package for Windows from the link I gave above (chose correct version to match your version of python) you can avoid all the hassle of compiling the source release.
Most probably you need either MySQL-python-1.2.2.win32-py2.5.exe or MySQL-python-1.2.2.win32-py2.4.exe
Uh, this isn't Django, this is you downloading some unspecified Python environment and expecting it to magically do everything exactly the way you wanted it to. Find a good tutorial on this and follow the instructions.
BTW, this is a very helpful forum, but giving no specifics and then delivering a non-question with an attitude is not a good way to get people to feel helpful.
I once had the same problem running Python and MySQL on the same computer. Like the guys/gals said above, Python does not come with built-in support for MySQL, so you will need to download the connectors.
The link given above by #kibitzer will most likely not work on Windows successfully, so go here to download a copy of the connector that works with windows. It comes with installer and no need to run setup.py script manually.

Categories