Python app made with py2app doesn't work - python

I want to make a game called "dodgeball". I have a main script, a setup.py script and an image called ball.bmp. In my setup.py script I have the following code:
from setuptools import setup
APP = ['dodgeball.py']
DATA_FILES = ["ball.bmp"]
OPTIONS = {'argv_emulation': True}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
When I run
sudo python setup.py py2app
Everything goes perfectly, except when I try to open Dodgeball.app it gives me this error (In a pop-up error window):
dodgeball Error
Open Console
Terminate
And it only works in alias mode:
sudo python setup.py py2app -A
How should I fix this?
I'm on Mac OS X El Capitan (10.11). I'm aware the new Mac update broke a few stuff.

import pygame._view worked.
:D

Related

App crashes on launch after compiled by py2app if 'requests' is included

My .app launches if i don't have the requests module imported into my app, but if it's included it crashes when trying to launch the .app compiled by py2app. I've tried to include requests into Setup.py and it still crashes.
i have py2app-0.21 installed.Running Python 3.6.4, MacOs Catalina 10.15.14
error log from console:
(org.pythonmac.unspecified.testapp.2888[62145]): Service exited with abnormal code: 255
Setup.py
from setuptools import setup
APP = ['test.py']
DATA_FILES = []
OPTIONS = {
'argv_emulation': True,
'includes': ['requests'],
'packages': ['requests', 'rumps']
}
setup(
app=APP,
name='TestApp',
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
The unspecified.testapp part of your error log tells me that you need to tell python where your app file actually is (relative path? absolute path? that will depend on your usecase)
Use some pathname manipulation to point to test.py

Setup.py with py2app including pip packages

I built a keylogger in python and I used py2exe (version 0.13) to convert the python file to an exe. It worked perfectly, it included all the packages and everything.
I want my keylogger to come to mac also, so I tried to use py2app. I followed TheBlackBerryChannels tutorial. It was pretty good explaining everything but it didn't show how to install external packages that are installed from pip. Becuase I use the from pynput.keyboard import Key, Listener
import os
import datetime
from pathlib import Path
packages from pip.
In the full setup.py file this is what I have:
from setuptools import setup
APP = ['KeyLoggingMac.py']
OPTIONS = {
# 'iconfile':'logoapp.icns',
'argv_emulation': True,
'packages': ['pynput']
}
setup(
app=APP,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
What am I doing wrong? It doesn't include the pynput package, I figured that it would auto-include the os, datetime, and pathLib modules but why doesn't it include the pynput?
For simplicity sake in your main file import it like this:
from pynput import keyboard
Then in your setup file use 'includes' in OPTIONS, note that I also added the data file test.txt to write the key presses to a file:
from setuptools import setup
APP = ['TEST.py']
DATA_FILES = ['test.txt']
OPTIONS = {'includes': ['pynput']}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
Then from your venv run py2app with the terminal command:
python setup.py py2app
You can also use py2applet to generate setup.py files but I find it never works properly.
py2applet --make-setup FILENAME.py
You will also need to give the app permission to access your global keyboard events. This is done in Preferences > Security & Privacy > Privacy
Up to Mojave you want the Accessibility tab then add your app in there, on Catalina you want Input Monitoring.
This works for me.

py2App working in alias mode and not working in the final distribution file

So I finally have an app working while in py2app alias mode. I can open the file and it works well. After I remove the build & dist files and run python setup.py py2app, the package receives an error. Why would it be doing this?
magnolia.py:
from splinter import Browser
import webbrowser
with Browser() as browser:
# Visit URL
url = "http://magnolia.msstate.edu/k12/login.asp"
browser.visit(url)
browser.fill('username', 'magn0897')
# Find and click the 'search' button
button = browser.find_by_name('btnG')
# Interact with elements
browser.find_by_xpath('//html/body/div[3]/div[1]/div/section/form/input').click()
#Open browser in new tab to keep browser open
webbrowser.open_new_tab('http://magnolia.msstate.edu/k12/elementary.asp')
#source: https://splinter.readthedocs.io/en/latest/mouse-interaction.html
#Instructions:
# Activate: go into bin.... $source activate
# To Update Requirements File: $pip freeze > requirements.txt
#http://www.marinamele.com/from-a-python-script-to-a-portable-mac-application-with-py2app
setup.py:
from setuptools import setup
APP = ['magnolia.py']
DATA_FILES = []
OPTIONS = {'argv_emulation': True,'iconfile': 'robot.icns'}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
After $open magnolia.app in terminal:
You didn't give your os version so I will just give you the information that worked for me on MacOS X 10.10 Yosemite:
$pip3 virtualenv
$cd target/appsource/folder
$virtualenv env
$. env/bin/activate
$pip install py2app==0.11
$py2applet --make-setup appName.py
edit your settings to include any info you want
$python setup.py py2app -A
test everything to make sure your options are set up proper and everything is working as expected
$rm -rf build dist
$python setup.py py2app
$deactivate
If you are running Yosemite, you should be off to the races. If you are rocking something newer, there may be a better version of py2app for you to use.

Using py2applet directly instead of setup.py py2app

I am trying to create a py2app application on a Mac OS X 10.6 Server. I downloaded and installed py2app, but I can't get it to work (I get a "is a directory error"). The same exact code and setup.py file work perfectly with py2app on Mac OS X 10.9.
After some research I learned that Mac OS X 10.6 ships with py2applet and that's the one that works. I am trying to run py2applet directly to generate the app.
Here is my setup.py file:
from setuptools import setup
APP = ['app.py']
DATA_FILES = ['resource']
OPTIONS = {'argv_emulation': True, 'includes': ['wx', 'wx.wizard', 'wx.lib.mixins.listctrl']}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
Here is the py2applet command that I am running:
It seems to complete, but the app crashes right away, so I feel like my parameters are wrong.
py2applet -i wx -i wx.wizard -i wx.lib.mixins.listctrl resource app.py
Thank you!

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

Categories

Resources