Hi I am building a simple application that will have a combo box to select python scripts that are in a directory called scripts to create reports on a database. I was going to use py2exe to build the program so that the users don't have to have python and submoduals installed.
so how would i go about having the program run these scripts using the py2exe dist?
i thought about using system('command') and copying the python.exe from my install directory to just run system(os.curdir+'python.exe ' + script_to_run) the python.exe would then use the local copy of the python.dll and the libs that it needs to run which would just be reportlab and pyobdc
would that work or is there a better way?
(i also wouldn't mind building it in ironpython if that would be easier)
Micheal Foord has the following example for Embedding Ironpython from within Ironpython
The basic steps are to create your shell application. For a real lightweight GUI there is EasyWPF. Use pyc to compile your script to an exe and the standard library into a DLL. Depending on if you need to capture stdout from the scripts or pass variables in information into them, things can get more complicated as indicated in the article. A basic example is listed below.
import clr
clr.AddReference('IronPython')
clr.AddReference('System')
clr.AddReference('mscorlib')
clr.AddReference('Microsoft.Scripting')
clr.AddReference('MyStandardLib')
#Now you can reference things like 'import lxml as L .....
from IronPython.Hosting import Python
from Microsoft.Scripting import SourceCodeKind
spath = "C:/fred.py" # Path to script
engine = Python.CreateEngine()
#Now add your assemblies (like the standard library) to the engine
for assembly in clr.References:
runtime.LoadAssembly(assembly)
source = engine.CreateScriptSourceFromFile(spath, SourceCodeKind.Statements)
mod = engine.CreateScope()
runtime = engine.Runtime
source.Execute(mod)
Related
Apologies if I am missing something obvious here. I have a PyQt5 app that I've frozen using the awesome fbs package. Within the app, a Python script is called via a PyQt subprocess, i.e. like this:
command = "python LaunchPPTKwin.py"
self.child = QProcess()
self.child.start("cmd.exe /C python LaunchPPTKwin.py")
self.child.waitForFinished(-1)
This works fine when the app is run on the machine on which the app was built. When I bring it to another machine, however, the app runs but the LaunchPPTKwin.py script is never executed. I assume this is because the other machine does not have python installed and/or does not have the LaunchPPTKwin.py script locally. My goal is to create an app so that this will work without the user needing to separately download python or the script, i.e. to make the app totally self contained. Is this possible using fbs?
P.S. Both machines are using Windows 10.
Either python is not installed or python is not in the search path and you had to locate it.
If python were installed, but not in the search path following code should work.
self.child.start("cmd.exe /C start LaunchPPTKwin.py")
If python is not installed at all you could cheat with more effort.
you had to make sure, that your frozen application can handle a command line parameter that allows it to start another script.
It would take the python file.
add the base directory of that file to sys.path strip of the .py suffix of the basename and import it and call it's main function. THis works however only if the script had following lines at its end
def main():
the_code_you_want_to_start
...
if __name__ == "__main__":
main()
It would further only work if all modules, that this file imports are already used by your frozen app.
So you see it's a little complicated, but I did something similiar with a py2exe application and with a pyinstaller application
Figured it out: I just compiled the Python script being called into an executable using Pyinstaller, and then called that executable. So my subprocess call above turned into:
command = "cmd.exe /C LaunchPPTKwin.exe"
self.child = QProcess()
self.child.start(command)
self.child.waitForFinished(-1)
I was playing around with the eel module for Python - gives an opportunity to run HTML + CSS/JS with the python functionality.
Made a simple program, tried to make an executable out of it via PyInstaller.
No errors whatsoever, but when running this executable it crashes on the first line - import eel, telling - there is no module called 'bottle-websocket'.
I checked pip: eel, bottle-websocket are installed. Can't figure out what's the problem. Attachments:
main.py:
import eel
from os import path
eel.init(path.dirname(__file__) + "/web")
eel.start('main.html', block=False)
while True:
eel.sleep(10)
Error:
Picture with the error while I try to start the exe
EDIT:
PyInstaller Log
I was also having this same issue, but I finally fixed it, it was actually very very easy, first of all make sure you are using auto-py-to-exe to package your app.
After inserting necessary details (script file,, extra files etc), you would see an advanced options tab probably below, click on it to expand it, look for the hidden imports label and insert "bottle-websocket" in the input field representing the hidden imports, that's basically all you need to do
I HOPE THIS HELPS
Took me the whole day figuring out the solution, but finally, here it is:
Copy the plugin.py, server.py files from C:\Users\YOUR_USERNAME\AppData\Local\Programs\Python\Python36-32\lib\site-packages\bottle_websocket to C:\Users*YOUR_USERNAME*\AppData\Local\Programs\Python\Python36-32\Lib
Make sure you have this line as follows in your *.spec file generated by PyInstaller (FOR PYTHON 3.6 32bit):
datas=[('C:\Users\YOUR_USERNAME\AppData\Local\Programs\Python\Python36-32\lib\site-packages\eel\eel.js', 'eel'), ('PATH_TO_YOUR_WEB_FOLDER', 'YOUR_WEB_FOLDER_NAME')]
3)Run this command in cmd: python C:\Users\YOUR_USERNAME\AppData\Local\Programs\Python\Python36-32\lib\site-packages\eel\main.py HERE_SHOULD_BE_THE_PATH_TO_YOUR_WEB_FOLDER
this command will get the 'bottle-websocket' work and will make sure that it complies with the web folder and files.
I've developed a few Qt projects in C++ using Qt Creator in the past, but now I want to experiment with the Python implementation of Qt. I discovered that Qt Creator 2.8 and higher support Python, but I haven't been able to figure out how to create a Qt application in Python with it so far. Online documentation about it appears to be scarce.
How do I set up such a project in Qt Creator? Ideally I'm looking for a simple "Hello World" project that I can open in Qt Creator and use that as a starting point to build something.
Currently, Qt Creator allows you to create Python files (not projects) and run them. It also has syntax highlighting, but it lacks more complex features such as autocomplete.
Running scripts requires some configuration (I used this tutorial). Open Qt Creator and go to Tools->Options->Environment->External Tools. Click Add->Add category and create a new category (for example, Python). Then, select the created category and click Add->Add Tool to create a new tool - RunPy for example. Select the created tool and fill the fields on the right:
Description - any value
Executable - path to python.exe
Arguments - %{CurrentDocument:FilePath}
Working directory - %{CurrentDocument:Path}
Environment - QT_LOGGING_TO_CONSOLE=1
You get something like this:
Now, go to File->New File or Project->Python and select Python source file. To run the created script: Tools->External->Python->RunPy.
You can also add pyuic to it the same way:
Click again on the Add->Add Tool button to create a new tool - PyUic now. Select it again and fill the fields on the right:
Description - any value
Executable - path to pyuic5
Arguments - -o UI%{CurrentDocument:FileBaseName}.py -x %{CurrentDocument:FilePath}
Working directory - %{CurrentDocument:Path}
Environment - QT_LOGGING_TO_CONSOLE=1
Then you should have PyUic connected as well.
Thanks for this, it helped enormously.
I set up a build and run section of a new kit for python, using your instructions, which seems to work quite well.
Here are the build settings:
Here are the run settings:
note that I have /usr/bin/python as a link to /usr/bin/python3.6
Here are the project file settings:
The only thing that is necessary is to go into tools -> options -> build and run and unselect 'always build project before deploying it' and 'always deploy project before running it'.
Once you have designed a form, you can then click build to create the UI.py file and run the currently selected python source file by clicking run.
For PyQt noobs like myself, I found the following resource to be particularly helpful at getting started... (although I am on linux rather than windows)...
http://projects.skylogic.ca/blog/how-to-install-pyqt5-and-build-your-first-gui-in-python-3-4/
edit.
I also added pdb - the python debugger
which you can then select by clicking the button above the run button:
before clicking run. You can set breakpoints in your code using the following snippet, where I have added DEBUG = 1 to the system environment in the run settings of the pdb run and DEBUG = 0 to the run python env:
if (QtCore.QProcessEnvironment.systemEnvironment().value("DEBUG") == "1"):
import pdb; QtCore.pyqtRemoveInputHook(); pdb.set_trace()
I've been trying to port a Maya based python project over to PyCharm but I'm having trouble running unit tests.
Maya provides its own python interpreter (mayapy.exe) with a zipped version of the python stdlib (in this case, 'Python27.zip') AFAIK there's nothing special about the stdlib here, but to run the native maya functions you have to use MayaPy rather than a generic python.
The problem appears to be that the jetBrains test runner (utRunner.py) wants to get os.system and it's barfing because it uses a specific import routine that doesn't allow for zip files. It tries this:
def import_system_module(name):
if sys.platform == "cli": # hack for the ironpython
return __import__(name)
f, filename, desc = imp.find_module(name)
return imp.load_module('pycharm_' + name, f, filename, desc)
and fails with this error:
ImportError: No module named os
I think because this is bypassing the zip import hook.
There's one solution posted here, which is basically to unzip the standard library zip. I'm reluctant to do that because I might need to run the tests on machines where I don't have admin rights. I'm also reluctant to patch the code above since I'm not clear how it fits in to the whole test process.
So: how to run tests with a zipped standardlib using PyCharm, without unzipping the library or tweaking the PyCharm install too much?
For lurkers: I was unable to find a better solution than the one linked above, so it was necessary to unzip the 2.7 standard libary into a loose folder. Inelegant, but it works,.
There was a further wrinkle that maya users need to watch out for: PyCharm does not like tests which run Maya.standalone -- the standalone session did not exit properly, so when running tests (in onr ore more files) that called
import maya.standalone
maya.standalone.initialize()
The pycharm test runner would hang on completion. After much frustration I found that adding an atexit handler to the test code would allow the standalone to exit in a way that PyCharm could tolerate:
def get_out_of_maya():
try:
import maya.commands as cmds
cmds.file(new=True, force=True)
except:
pass
os._exit(0) # note underscore
import atexit
atexit.register(get_out_of_maya)
This pre-empts the atexit hook in Maya and allows the tests to complete to the satisfaction of the Pycharm runner. FWIW, it also helps if you are running MayaPy.exe from a subprocess and executing your tests that way.
I ended up just editing Pycharm's utrunner.py file. It already imports os at the top of the file, so I'm not sure why it calls import_system_module. The import command automatically handles zip files. Also if you put the maya.standalone in the runner file, you don't need to call it in any of your test files.
#os = import_system_module("os")
#re = import_system_module("re")
import re
try:
import maya.standalone
maya.standalone.initialize()
except ImportError:
pass
I'm using Pycharm 5.0.1.
I am developing a simple standalone, graphical application in python. My development has been done on linux but I would like to distribute the application cross-platform.
I have a launcher script which checks a bunch of environment variables and then sets various configuration options, and then calls the application with what amounts to python main.py (specifically os.system('python main.py %s'% (arg1, arg2...)) )
On OS X (without X11), the launcher script crashed with an error like Could not run application, need access to screen. A very quick google search later, the script was working locally by replacing python main.py with pythonw main.py.
My question is, what is the best way to write the launcher script so that it can do the right thing across platforms and not crash? Note that this question is not asking how to determine what platform I am on. The solution "check to see if I am on OS X, and if so invoke pythonw instead" is what I have done for now, but it seems like a somewhat hacky fix because it depends on understanding the details of the windowing system (which could easily break sometime in the future) and I wonder if there is a cleaner way.
This question does not yet have a satisfactory answer.
If you save the file as main.pyw, it should run the script without opening up a new cmd/terminal.
Then you can run it as python main.pyw
Firstly, you should always use .pyw for GUIs.
Secondly, you could convert it to .exe if you want people without python to be able to use your program. The process is simple. The hardest part is downloading one of these:
for python 2.x: p2exe
for python 3.x: cx_Freeze
You can simply google instructions on how to use them if you decide to go down that path.
Also, if you're using messageboxes in your GUI, it won't work. You will have to create windows/toplevels instead.