Making a standalone python script that can be run by another software - python

I am currently interning at a place where they've asked me to make a standalone python program to do something (say X).
Now, that program is to be run by some commands sent by their proprietary software which is written in their proprietary language. Now the reason I'm saying proprietary so many times is because they aren't ready to take me anywhere near their code. I am just supposed to make a Python code that does X based on the input given by their software.
So is there a way I can make an API and wrap it around my code so as to let the software control it? Also I need to make the whole thing standalone (maybe an installer of some kind) so that they don't have to install Python and the accompanying modules (like opencv) just to run my script?
All I could get out of them was "there are dll files that will be calling your app and we want an executable"

Any programm can execute any other program (if it has the appropriate rights) so there is no real distinction between "python file" and "python executable" (that is because python is an interpreted language. The python source files and the "final python program" are "identical" (asuming cpython), in contrast to e.g. a C program where the source files and the executable are vastly different).
If you are on windows there is the additional problem that the user must have installed python to execute .py files. There are some ways to mitigate that problem - there are python libraries that "freeze" the python interpreter and your code into a single .exe file (from the comment by Bakuriu see e.g. freeze) . You could bundle the python interpreter with your code. You can just say your users to install python (if the amount of users is low that might be the good way).
"API" is just a fancy way of saying "this is how you communicate with my programm". This might be how you call a library (e.g. what functions a python module exports) or this might be an HTTP API or which command line arguments are passed or which protocoll over an TCP socket is spoken. Without knowing which API you are supposed to implement you cannot fulfill your job.
Without knowing further specifications (what inputdoes the other program give to yours, how does it call your programm?) it's very hard to say anything more helpful.

Related

Distribute a standalone Python software with Julia dependencies

I have a software that is mostly written in Python and, for now, I'm using PyInstaller to bundle and distribute the software in a user-friendly way (it's part of my CI pipeline, for Linux and Windows).
However, my performance is terrible and I want to rewrite some heavy parts in Julia while keeping the front-end in Python. I can use PyJulia to do this, but it means that the user has to install Julia manually in order to use my program.
Julia does have the equivalent of PyInstaller, which is PackageCompiler.jl, but I don't know how to call something compiled with PackageCompiler.jl from the Python side.
How can I make this work, so I can bundle and distribute an executable that has Python, Julia and everything it needs to run?
A little more details
My end user is someone (chemists and pharmacists) that have no idea what programming is. They don't have Python, Julia or Docker (and they don't even want to install it).
In my current approach, the software bundled with PyInstaller consists of a single executable with everything inside it (Python and everything it needs). What I really want is to keep the same user experience, but also with Julia running on the backstage.
I'll implement several functions in the Julia side, and I want (almost) the same level of integration as I get with PyJulia.
Maybe I'll go to Rust and just use the C interface, but I really would like to use Julia.
Thank y'all for your time.
Per here: https://julialang.github.io/PackageCompiler.jl/stable/apps.html#Creating-an-app-1 you can get what is basically an executable file and then you can just follow this post: Python Script execute commands in Terminal to execute the file you create.
You could try using JuliaWin. This provides a standalone Julia runtime that works fully self-contained.
We deployed a tool using PyInstaller with the JuliaWin included as one of the datas as PyInstaller calls them. Iirc it was not PyJulia but JuliaCall+PythonCall doing the interfacing in our case.
Unfortunately the startup was ridiculous (order of minutes). This is in part due to Julia's startup time, but largely due to the generated .exe first having to unpack the JuliaWin. So we are currently investigating using PackageCompiler, possibly also switching to providing the user with 2 files instead of 1.

Running a python program from a C++ program?

I've been messing around with Selenium in python, and I really want to have an existing C++ program run my python code.
Basically, my python code just finds a website, and downloads the file which afterwards my C++ program wants to open the file and do a bunch of operations on it. If I had a myPythonCode.py file, and my other C++ files (header.h, main.cpp, otherFunctions.cpp...) how would I go about running the python code from my C++ program?
Also both of my programs are console programs, and I was hoping that a user would have an uninterrupted experience running the program (for example, if the user wants to download a file while running the C++ program the terminal doesn't have to close, or open a different window to start the python program). Any help in going about this would be greatly appreciated!
It is operating system specific, and the C++11 standard does not define any functions for that (except system(3), which is in C99, and std::system in C++11). On Linux (and other POSIX systems), read Advanced Linux Programming and consider using system, or popen(3), or more probably the lower-level syscalls(2) like fork(2), execve(2), pipe(2), dup2(2), etc etc.... You may want some IPC and you may need to have some event loop around a multiplexing syscall like poll(2)
You could use some C++ frameworks like Qt or Poco (both have a process abstraction and are usable on several operating systems, even on proprietary ones from Microsoft)
If you want your C++ program to have a terminal interface on Linux, consider ncurses and/or readline
BTW, several C or C++ libraries for HTTP exist, e.g. libcurl for HTTP client side, and libonion for HTTP server side. So you might avoid your Python program and incorporate the downloading in your C++ application.
Check out the boost library which allows running python on C++ and using your C++ in python. https://www.boost.org/doc/libs/1_49_0/libs/python/doc/

Python - Compiling/ securing as standalone executable [duplicate]

This question already has answers here:
How do I protect Python code from being read by users?
(29 answers)
Closed 9 years ago.
I'm rather new to Python, though hope to use it for both programming and scripting. I've written basic scripts, and did some digging for compiling. I'm currently using py2exe (With a different setup.py script someone else made) so that it becomes ONE simple .exe, without dependencies (python DLL, etc.)
You're probably wondering what my problem is. Well, I decided to check the security of the executable, and view it in Resource Hacker. I was able to view all the parts of the script I DIDN'T want people to be able to find out. (Ex: Password inputs).
Can anyone give me a simple, working method, for converting PYTHON code to a STANDALONE executable that CANNOT allow viewing of the original python script via something like Resource Hacker?
I am not thoroughly knowledgeable in the field.. I'm also not developing commercially (Yet), I just want to make things for myself, that I may also make for other people.. Though I might freelance for random people online doing things. Anyways point being, if I had a script where it prompted you for a password, and if you got it correct it continued, else, it cancelled and exited.... then once I make it a .exe, opening in Resource Hacker, and viewing the "Python Script", I scroll to the bottom, and bam! It shows the passwords. Now when I say I'm new, I mean, really REALLY new. Anyways, if you don't mind explaining, "Encryptions", "Hash's", etc... I would prefer to be enlightened towards these subjects.'
Your help is appreciated.
Here are the simple steps (with freeze)-
You will need to use a python installation which has all its modules installed as shared libraries
freeze.py usually resides under <python_install>/Tools/freeze/freeze.py
e.g: Python-2.4.2/linux/Tools/freeze/freeze.py
Now to integrate freeze a very simple program, which does not have dependency on any custom python module you will just need to call freeze in this fashion:
e.g:
cat hello.py
#!/usr/bin/env python
print "Testing"
To Freeze:
a. Python-2.4.2/linux/Tools/freeze/freeze.py hello.py
b. make
you will see there is a executable hello.
file hello
hello: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.4.0, dynamically linked (uses shared libs), not stripped
that's it:
now invoke hello will produce:
[0:22:47]% ./hello
Testing

debugging info for Mac System Python - how to get line numbers

When I have a some crash report including some MacOSX library (in this case, I'm mostly interested in Python), how can I get more info about it? The library does not contain the debugging information, so the crash report lacks line numbers and other useful stuff. Can I get the debugging information elsewhere and reconstruct the line numbers?
You may want to look at the lldb debugger. It is scriptable in python and pretty easy to do things like symbolicating a crash report. There is even an included example python script that can symbolicate a standard Mac OS X crash report (assuming you have dSYMs for some of the frameworks) and provide file name & line number information.
See http://lldb.llvm.org/symbolication.html for more information about using this, or it is easy to write your own python scripts with lldb. You can make a python method that is called from an lldb session (like lldb.macosx.crashlog does), or you can write a standalone python script that loads lldb and does whatever you want. lldb is structured like a library (a framework on Mac OS X), the lldb command-line command is one possible client of LLDB.framework.

How to run a Python script in something other than cmd?

I have written a program. I don't know if it is important how it is written but you can find it here: http://pastebin.com/Z3ZvVPV8 Basically, it asks you to assign values to variables and will perform calculations depending on what variables you chose, and prints the answer.
I would like to know how I can make the program run in a window other than cmd (I am using Windows Vista 32bit). I don't need much at all in terms of GUI, just a window that is a bit more user friendly/easier to look at when they are using the program.
EDIT: To those suggesting using IDLE, while that would work for me, if others want to use the program they would have to download it, so I was hoping for a way for that not to happen.
Python comes with a sort of default GUI package TkInter you can use it.
Also there is a lot of other GUI packages available.
The Python standard library offers a lot of ways to implemt simple (but also rather complex) GUIs. I'd like to point you at the documentation of TK (tool kit for graphical interfaces) http://docs.python.org/library/tk.html where you will find also some useful example of use.
Py2Exe is a viable option if you really don't need a gui. This will make it run and look like a command prompt, but it will be an .exe file. Here is a quick quote from thier page: "py2exe is a Python Distutils extension which converts Python scripts into executable Windows programs, able to run without requiring a Python installation."
Another alternative is to get Portable Python. Here is a quote from thier webpage: "Portable Python is a Python® programming language preconfigured to run directly from any USB storage device, enabling you to have, at any time, a portable programming environment. Just download it, extract to your portable storage device or hard drive and in 10 minutes you are ready to create your next Python® application." After packaging the portable python and your .py or .pyc file then create a .bat file that runs the portable python "Python-Portable.exe" with the correct command line parameters for loading your script. Be sure to use relative paths in the batch file in case they are running it from a flash drive, or something other than the same location as you.
NOTE: This is really not a good way to do this as thier download page states: "Installed size: based on selected packages, between 49MB and 480MB". Also be sure to read the the current Python Software Foundation License, as that is what Portable Python is released under, and it may or may not be legal to package it in a closed source project. I haven't really looked at the license myself to be able to tell you. If you are releasing it as open source, then there would not be an issue though. As a quick side note, if you need that .bat file to be a .exe file then you can use a .bat to .exe converter battoexe.com is one. This is really going the long way about doing the whole thing, but it is an option.
Sources:
Working with Python on and off for 7 years now, a lot that using a portable version on a flash drive, and also dealing with Batch files much longer.

Categories

Resources