How to include a module dependency as a part of py2exe script - python

I got a windows installer my_script.exe (generated out of my_script.py using py2exe) which is dependent on "PyDes" module,
when I execute my_script.exe it throws an error saying ,
Import error : pyDes module
How to include the pyDes module as a dependency to my_script.py
I am using py2exe to create windows installer.
python setup.py py2exe

Try adding 'includes': ['PyDes'], to the relevant spot in your setup.py file
setup(
# ...
options = {
'includes': ['PyDes'],
},
# ...
)
assuming that PyDes is in your sys.path
N.B. If py2exe hasn't picked up the PyDes dependency, chances are it hasn't picked up anything that PyDes depends on so be prepared for more ImportError's! Just keep adding them to the 'include' until it all works out.

Related

Py2App error: ModuleNotFoundError: No module named 'cmath' when using Pandas

I am trying to build a standalone app that utilises Pandas. This is my setup.py file:
from setuptools import setup
APP = ['MyApp.py']
DATA_FILES = ['full path to/chromedriver']
PKGS = ['pandas','matplotlib','selenium','xlrd']
OPTIONS = {'packages': PKGS, 'iconfile': 'MyApp_icon.icns'}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app','pandas','matplotlib','selenium','xlrd'],
)
The making of the *.app file goes smoothly, but when I try to run it, it gives me the following error:
...
import pandas._libs.testing as _testing
File "pandas/_libs/testing.pyx", line 1, in init pandas._libs.testing
ModuleNotFoundError: No module named 'cmath'
I tried to include ‘cmath’ in my list of PKGS and in setup_requires in the setup.py file, but when I tried to build the app using py2app it gave me the error:
distutils.errors.DistutilsError: Could not find suitable distribution for Requirement.parse('cmath')
I am stuck. I couldn't find anything useful online. cmath should be automatically included from what I have been reading. Any ideas on where is the problem and how can I fix it?
I think I have found a solution: downgrade to Python version 3.6.6.
See: python 3.6 module 'cmath' is not found
To uninstall Python I followed this process: https://www.macupdate.com/app/mac/5880/python/uninstall
Then I installed Python 3.6.6: https://www.python.org/downloads/release/python-366/
With Python 3.6.6, Py2App seem to work no problem and includes Pandas smoothly.
It seems that for some reasons cmath is not included in the latest versions of Python? I might be wrong. Please let me know what you think and if you have any questions.
P.S.: I am using MacOS (Mojave 10.14.6) and PyCharm.
I had a similar issue with py2app and cmath. I solved this by adding import cmath into the main script. (MyApp.py in your case) I think doing so may have the modulegraph to add the cmath library files.

How to create setup.py with library file as stand alone

I have a problem, i develop an application with python and i use some libraries like flask, sqlalchemy, etc...
The problem is that i have a define version of each library, and I want to deploy this python application in another computer without internet,
can I create a package or use setup.py and include the other package with path ?
I've already try this code, but the library aren't imported they said that :
ModuleNotFoundError: No module named 'cx_Oracle'
My code is:
from distutils.core import setup
setup(
# Application name:
name="MyApplication",
# Version number (initial):
version="0.1.0",
# Packages
packages=["App","App/service"],
include_package_data=True,
install_requires=[
"flask","cx_Oracle","pandas","sqlalchemy"
],
)
install_requires is a setuptools setup.py keyword that should be used to specify what a project minimally needs to run correctly.
It won’t install those libraries.
Maybe you should try pyinstaller (https://www.pyinstaller.org) to make ready runnable file to run on the other computer.

Python Flask - Building the Server as Executable (Py2exe)

I have a flask project, everything appears to be working fine. When using py2exe to build the package, (target server is a windows server ugh), the executable can execute, but leaves me with a ImportError: No module named 'jinja2.ext'
I have the module, and the website works fine with no ImportError when not executing from the .exe
I am pretty new at packaging and delivering, and not sure whats wrong with the setup that is causing the break from .py -> .exe conversion.
Setup.py
from setuptools import setup, find_packages
import py2exe
NAME = "WorkgroupDashboard"
VERSION = "1.0"
setup(
name=NAME,
version=VERSION,
description="Provides real time ISIS connection data",
long_description="",
# Get strings from http://www.python.org/pypi?%3Aaction=list_classifiers
classifiers=[],
author="Test User",
author_email='',
url='',
license='Free',
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
include_package_data=True,
zip_safe=False,
install_requires=[
'flask >= 0.10.1',
'SQLAlchemy>=0.6'
],
console=['DashboardBack.py']
)
The idea is in order to turn the server on, just execute the .exe. Server will not have python on it. I am using Python 3.4 64 bit.
Edit: build cmd = python setup.py py2exe
Figured it out it seems, Setup commands go inside the py2exe optuons=[]
Working Setup.py
__author__ = ''
import sys
from glob import glob # glob will help us search for files based on their extension or filename.
from distutils.core import setup # distutils sends the data py2exe uses to know which file compile
import py2exe
data_files = []
setup(
name='WorkgroupDashboard',
console=['DashboardBack.py'], # 'windows' means it's a GUI, 'console' It's a console program, 'service' a Windows' service, 'com_server' is for a COM server
# You can add more and py2exe will compile them separately.
options={ # This is the list of options each module has, for example py2exe, but for example, PyQt or django could also contain specific options
'py2exe': {
'packages':['jinja2'],
'dist_dir': 'dist/test', # The output folder
'compressed': True, # If you want the program to be compressed to be as small as possible
'includes':['os', 'logging', 'yaml', 'flask', 'sqlalchemy'], # All the modules you need to be included, because py2exe guesses which modules are being used by the file we want to compile, but not the imports
}
},
data_files=data_files # Finally, pass the
)
I'd convert mine to EXE and deploy is over a host. Just get "Auto Py To Exe" application. Download it locally. Open the app, insert your project location, do some setup if you want, and then click "Convert". There you go. Flask in EXE format. Plug-in anywhere that you like without the need to have Python Interpreter.

py2app ImportError with watchdog

I am attempting to use py2app to bundle a small Python app that I've made in Python 2.7 on Mac. My app uses the Watchdog library, which is imported at the top of my main file:
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
When running my program, these import statements work just fine, and the program works as expected. However, after running py2app, launching the bundled application generates the following error:
ImportError: No module named watchdog.observers
At first I thought it was something to do with the observers module being nested inside watchdog, but to test that, I added the line
import watchdog
to the top of my program, and then upon running the app, got the error
ImportError: No module named watchdog
so it seems that it actually can't find the watchdog package, for some reason.
I tried manually adding the watchdog package using py2app's --packages option:
$ python setup.py py2app --packages watchdog
but it had no effect.
My unbundled Python program runs just fine from the command line; other downloaded modules I've imported are giving no errors; and I have successfully bundled a simple "Hello World!" app using py2app, so I believe my setup is correct.
But I'm kind of out of ideas for how to get py2app to find the watchdog package. Any ideas or help would be greatly appreciated.
Edit: Here is the text of my setup.py, as generated by py2applet. I haven't modified it.
from setuptools import setup
APP = ['watcher.py']
DATA_FILES = []
OPTIONS = {'argv_emulation': True}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
Try manually including the desired packages in the setup.py file:
from setuptools import setup
APP = ['watcher.py']
DATA_FILES = []
PKGS = ['watchdog', /*whatever other packages you want to include*/]
OPTIONS = {
'argv_emulation': True,
'packages' : PKGS,
}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
I had installed watchdog 0.5.4, a very old version as it turns out, and got the same error. The error was fixed after upgrading it to 0.8.3:
pip install watchdog --upgrade
Your problem generally indicates that the package (in your case "watchdog", or one of its dependencies) isn't installed, or at least not in a location that py2app expects to find packages.
Do you use the same python command for running py2app as for running the script from the command-line? What is the message of the ImportError you're getting (both when importing "watchdog" and importing "watchdog.observers"?
The (way too long) output of py2app should also mention that it cannot find some packages, and which ones.
As alluded to in one of the answers py2app does not seem to search the same set of paths that are used by the python interpreter, so you need to copy the python library to one of those locations.
For example I've got the MacPorts version of Python installed and found that when I had a module installed in /Library/Python/2.7/site-packages/ py2app wasn't finding it, but it would find it when I copied it into /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages. So to copy it over run :
sudo cp /Library/Python/2.7/site-packages/thatmodule.so /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/
Then run the py2applet script again and build the app to check. If it's elsewhere you can do a search for all site-packages locations using Spotlight's command line interface:
mdfind -name site-packages

How to package Twisted program with py2exe?

I tried to package a Twisted program with py2exe, but once I run the exe file I built, I got a "No module named resource" error.
And I found the py2exe said:
The following modules appear to be missing ['FCNTL', 'OpenSSL', 'email.Generator', 'email.Iterators', 'email.Utils', 'pkg_resources', 'pywintypes', 'resource', 'win32api', 'win32con', 'win32event', 'win32file', 'win32pipe', 'win32process', 'win32security']
So how do I solve this problem?
Thanks.
I've seen this before... py2exe, for some reason, is not detecting that these modules are needed inside the ZIP archive and is leaving them out.
You can explicitly specify modules to include on the py2exe command line:
python setup.py py2exe -p win32com -i twisted.web.resource
Something like that. Read up on the options and experiment.
Had same issue with email module. I got it working by explicitly including modules in setup.py:
OLD setup.py:
setup(console = ['main.py'])
New setup.py:
setup(console = ['main.py'],
options={"py2exe":{"includes":["email.mime.multipart","email.mime.text"]}})

Categories

Resources