How to compile C/C++ when installing a Python Package - python

So i'm trying to build a python package for my dissertation as a Comp-sci student, and the idea is quite similar to something like what DIPY does, but with multiple codes that other students from my university have developed during the past years. The thing is, some of these codes are written in C++, instead of Python. All of the C++ codes, have a python script running subprocess.call or subprocess.run to run the executable files, but i'd have to compile the .cpp files to even have a executable in the first place.
I've created some test packages with some of the codes developed in python, but my question is: How can i compile the codes written in C++ on the pip installation? The idea is that whenever someone installs the package from PyPI, this happens so that the user can just begin running the code afterwards.
How can i do it? Do i have any other alternatives?
Thanks in advance!

Related

Embedded python not running on Windows laptop

I am trying to run an embedded python application which runs well on desktop computers but on a laptop it is giving errors.
Initial error was:
The program can't start because api-ms-win-core-timezone-l1-1-0.dll is
missing from your computer.
On installing above, it gives error that api-ms-win-core-file-l2-1-0.dll is missing.
How far this will go? What is the problem and how can this be solved? Thanks for your help.
The problem is that the developers have used some version of C++ to create their programs and the programs require some runtime files (Dynamic Linked Libraries) to be present in order to install/run and the developers do not include those files with their installation (why not?) and the websites for the programs often do not list the prerequisites and requirements of what you need to have installed for their programs to work.
Read more here:
https://answers.microsoft.com/en-us/windows/forum/windows_7-performance/missing-api-ms-win-core-timezone-i1-1-0dll/3754703c-241c-451a-a9b6-e690399fc83e
Try installing the missing files.
https://www.microsoft.com/en-us/download/details.aspx?id=48145
https://support.microsoft.com/en-us/help/2999226/update-for-universal-c-runtime-in-windows

How to convieve of a cross platform python script

I have made a python interactive script project containing a few directories with project files and a main python script.
The script does the work of batch processing scientific images for biological systematics.
I wrote os agnostic code, but I was thinking about trying to package/freeze the script as a cli utility that a (more or less)lay person could download and use.
I have been reading about packaging and freezing techniques in python and the more I read the more I feel I'm confused. (I'm linux user)
Am I conceiving of this script as a utility correctly? Is is worth it/possible to pass command line args to an .exe, and should I package/freeze the files for this kind of interactive cli script?
I don't have a lot of experience with windows. I'm looking for advice/pointer where to look next/search.
You are on the correct path, but you need to understand a little more about python packaging to know how to proceed.
You are right that you need to package your code with setup tools. This will make it cake to install the package for others and will let python decide where to properly put things. -including script utilities/non-code files-
Setuptools has special support built in JUST FOR SCRIPTS! that will let you pass arguments, ect..
However, in packaging the code, you must understand that what you are really doing is making it a library that can be installed with pip. Therefore, that python script utility that you wrote will be treated as a library by setuptools. Setuptools will have you make a separate script file to import your new library and call the script main() function.
I know that you were already using this manual, and that's fantastic. Everything you need to first set up and understand a python package is in there. RTFM up a storm.
As I understand it, you can then look more into freezing the package (and register it with Pypi) for other systems once you have a handle on packaging.
note: use scripts keyword in setup tools to specify your script

Deploying C++ program with python embedded

I have a Qt C++ project in which, for some functionality,I have added Python. The C++ function calls python script and returns the values.Like the below Example.
PyObject *pName,*pModule,*pFunc;
PyObject *pArgs,*pValue;
const char *module="getBeamData";
pName=PyUnicode_FromString(module);
pModule=PyImport_Import(pName);
if(pModule!=NULL){
pFunc=PyObject_GetAttrString(pModule,"getBeamDose");
if(pFunc&&PyCallable_Check(pFunc)){
pArgs=PyTuple_New(2);
PyTuple_SetItem(pArgs,0,PyBytes_FromString(path.toStdString().c_str()));
PyTuple_SetItem(pArgs,1,PyLong_FromLong(fraction));
pValue=PyObject_CallObject(pFunc,pArgs);
It calls the script getBeamdata.py. Functionality works fine. Do I have to install python and libraries(like numpy) for all customer and ship script ? Or is there any other way without sending script.
Without statically compiling your Python script you will need to distribute:
requirements.txt (for all libraries/modules required by your script).
details on the correct version of Python required by the script (2.7/3 etc).
Alternatively you could look into creating an executable using py2exe and distribute that instead, although you'll need to modify the way your program interacts with the Python script.
Some further details on distributing Python can be found here.
If I were you, I'd do it with some sort of installer.
Now in that installer, you can of course embed the wheel version of your script, which is in addition going to be installed using setuptools.
In that setuptools script you can mention dependencies, but that means that your client should have either Python installed on his/her computer, or you should ship your own version of Python embedded with the software.
Such functionality can be also be achieved using Boost.Python

Compile python virtual environment

I created a Python script for a Freelance job and I can't find how to compile/build/package it for easy sharing. The person for which I created it is not a technical one, so I can't explain him how to activate a virtualenv, install requirements and so on.
What is the easiest way for him to run the project right after downloading it?
Can the whole virtualenv be compiled into an .exe? If yes, can this be done inside a macOS system?
Yes you can package your python programs and it's dependencies with
Cx_Freeze
There are other python modules that do the same, personally i prefer cx_Freeze because it's cross platform and was the only one that worked out of the box for me.
By default cx_Freeze automatically discovers modules and adds them to the exe to be generated. but sometimes this doesn't work and you might have to add them by yourself
To create a simple executable from a file. you can just use the bundled cxfreeze script
cxfreeze hello.py --target-dir dist
but more for more complex applications that have different files you'll have to create a distutils setup script.
Note that cx_freeze doesn't compile your code like a traditional compiler. it simply zips your python files and all it's dependencies ( as byte code) together, and includes the python interpreter to make your program run. So your code can be disassembled. (if anyone wants to) and you'll also notice that the size of your program would be larger than it was initially (Because of the extra files included)
I ended up using PyInstaller as this worked out of the box for me.

How to package/ distribute python applications

I've spent countless hours trying to understand this and unfortunately I haven't gotten to an answer yet. Or at least I don't think I have.
First up I should say that I am a Java Developer. I've only recently started working with Python and the build-process is a bit...odd for me.
In my mind I write an application, I compile it to run and I package it into a .jar for other people to use. Either as a library or for end-users to execute and have fun with it. (ignoring stuff like maven or gradle...)
I wrote a little CLT in python that consists of ~6 files and I wanted to distribute it. From what I could find I was supposed to write a setup.py and I found some guides on how to do that but ... to be honest I'm not even sure what that did. I could get my source code bundled into a tar.gz with some other meta data or it would create some weird files that I don't know what to do with.
Then I found PyInstaller and it worked great to package everything into a binary. However I've run into some problems trying to create a Debian package and it has made me re-assess and question the fact that there doesn't seem to be something in Python (without having to use an external tool) that lets me package/ bundle/ whatever my application into a single file to be run.
And that's something I can't get my head around. I mean...before there were tools like PyInstaller and P2Exe and what not, how did people distribute their applications? Am I expected to write a C application, somehow include the python code in there and compile that? Sorry if this seems like a stupid question but I'm really asking. I've googled around so much and spent so much time on it and haven't found a satisfactory answer so I hope someone here can help me with this! Thanks.
If you package your Python code for pip, you can include some executable scripts that start your program. I don't know how the situation was 5 years ago when this question got asked, but nowadays pip is pretty much integrated with Python, to the point that there's a standard library module to bootstrap pip in case it's missing:
https://docs.python.org/3/library/ensurepip.html
The situation is different if you want to package an application for some other package manager, like Anaconda or the package managers of various Linux distributions, or as a Windows installer. Obviously, you'll have to create a separate package for each package manager or installation technique you want to support.

Categories

Resources