40

This might sound like a very open ended question, but I am a python enthusiast, and pretty new to this world of development. I have developed a python script which takes in an input and gives an output based on the algorithm I have put in place in the script. I want to use this script and package it in a way that it can be used by end users having no technical mindset and are only concerned with input they give and the output they get.

I have used Jupyter Notebook to develop this complex code and I want to know a way about how I can package or deploy this code so that the end user can use it without seeing how it works as it might be overwhelming for them.

Can anyone help me with the idea on how to do it? Something which is Opensource would be preferred.

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Kshitij Yadav
  • 1,357
  • 1
  • 15
  • 35
  • It sounds like you need a frontend of sorts. What kind of user input will you be dealing with? – MTTI Sep 05 '18 at 14:27
  • Just an id such as this: 123ABC0456K – Kshitij Yadav Sep 05 '18 at 14:27
  • Be aware that your someone else running your script may need their own Python installation/environment etc. this may be a consideration on how best to approach this as to whether you want users to be able to run locally or whether a remote processing is best. – tda Sep 05 '18 at 14:31
  • 2
    @TD-Asker I did not think of that. Any way of surpassing this ? My end user is a business type person having no knowledge about coding. He wouldnt even know that there is something called python lol. So how can i surpass this ? Will using batch help? – Kshitij Yadav Sep 05 '18 at 14:33
  • Batch files will still require Python to be installed on the client'a machine as its attempting to execute Python locally. You could always add the relevant installation scripts as part of the `.bat` file or send an `.exe` that prepares the environment for the client. Other than that I'd have to refer to someone else's expertise. Failing that, host remotely and give the client a log in and instructions on how to change/run `.bat` files etc. – tda Sep 05 '18 at 14:35
  • If you can't rely on there even being a python enviroment on the machine you want to run your code on, maybe it's just not the right tool for the task. Have you considered rebuilding your app in something more ubiquitous such as Java, JS or even an excel macro? – MTTI Sep 05 '18 at 14:39
  • You can host Jupyter Notebooks remotely fairly easily - see https://stackoverflow.com/q/49568183/7019148 – tda Sep 05 '18 at 14:41
  • Distributing a script for a given purpose and hosting a webservice are two quite different things. If the aim is to distribute a script, then hosting a remote Jupyter Notebook isn't a solution. – MTTI Sep 05 '18 at 14:46
  • One possibility for distribution would be to put everything onto a flash drive so they do not have to install Python on their machine. I have not done this, but while investigating I ran across this list of potential solutions to portable Python environments : http://portablepython.com/ (portable python is no longer available, but they list several other solutions) – Mike from PSG Sep 05 '18 at 18:53
  • Thanks guys. I am thinking of using it on a central server which has python installed and then using a batch file or pysimpleGUI. – Kshitij Yadav Sep 05 '18 at 20:54

5 Answers5

30

I want to round up this question by suggesting a couple of ways to deploy Python Script or Models:

  • Docker: This is one of the most popular ways of hosting scripts and deploying Machine Learning Models online. You can use Docker to containerize the code and host it as a microservice using different apps.
  • PEX: PEX is a clever tool being developed at Twitter that allows Python code to be shipped as executable zip files.
  • AWS: Using AWS, you can create a free account and get started with hosting/deployment. A lot of resources are available online.
  • PYSimple, Pyinstaller, and Sparrow: As mentioned in the answers above, we can use these packages also to do the task. Please read above.
  • Flask App: If you don't want to use Docker, using a simple flask app, you will be able to host your script online. But there will be a lot of issues as it is not containerized. Best to use Docker.
  • py2exe: If you are looking to convert the Python file into windows executable, Click here
  • cx_Freeze: Similar to the py2exe, you can use this also

I will keep adding more content as I find them, but I think the best way to do it using Docker. Hope this helps.

Troll
  • 1,895
  • 3
  • 15
  • 34
Kshitij Yadav
  • 1,357
  • 1
  • 15
  • 35
5

PyInstaller is a powerful opensource tool for cross-platform deployment/distribution of python project.

*Note: Although I have no experience on distributing Jupyter Notebook with PyInstaller, but it seems to be plausible with this guide.

Install through pypi:

pip install pyinstaller

Distribution of code without console can be as simple as:

pyinstaller --noconsole script.py

Then you can find your .exe(Window) under dist folder of your working directory.

imjp94
  • 96
  • 4
2

Assuming you have no GUI, I would use a batch (.bat) file to call the script with the provided inputs.

your_script.bat:

set VAR_1=this
set VAR_2=that

python your_script.py %VAR_1% %VAR_2%

Then retrieve these arguments in the python script via

sys.argv[1]  sys.argv[2]

For final users it's just a double click!

I hope you got the idea

[EDIT: sys.argv[0] is the name of your script]

hjuste
  • 130
  • 1
  • 12
  • 4
    Be aware though, that this requires any user of the .bat script to have the correct Python environment installed locally. – tda Sep 05 '18 at 14:38
2

For the sake of completeness, dh-virtualenv by Spotify should be mentioned which enables packaging python code as a debian-package.

As a starting point, one could use this Blogpost: How We Deploy Python Code

hamnur
  • 21
  • 2
1

Use Sparrow to deliver your script to end users. It works nice with Python scripts too.

Basically you just declare your requirements as requirements.txt file and name your script as task.py, you can handle input parameters inside a script as:

  from sparrow6lib import *

    foo = config()['foo']
    bar = config()['bar']

Then you upload your script to SparrowHub:

$ nano sparrow.json
{
    "name": "my-script",
    "version": "0.1.0",
    "description" : "this is my script",
}

$ ls -1
task.py
requirements.txt
sparrow.json

$ s6 --upload

Finally end users just use you script as:

$ s6 --install my-script
$ s6 --plg-run my-script@foo=1,bar=2

PS. disclosure I am the tool author. Let me know if you need any help with packaging your script.

PS2 I've changed the original answer as I recently ported Sparrow to Raku

Alexey Melezhik
  • 962
  • 9
  • 27
  • Sparrow is now ported to Raku as Sparrow6, use the same feature from there - https://github.com/melezhik/Sparrow6/blob/master/documentation/development.md#package-managers – Alexey Melezhik Apr 08 '20 at 19:00