1

I was curious if you made an application in python how do you ship it? If you made it in a virtual enviroment can you just give that out for download? Do you need some kind of install process?

(In my case it would be a webapp made with flask intended to run on a linux server)

Palomar
  • 51
  • 7

5 Answers5

3

There are several ways to ship a python application but the most straight-forward method is to either package it (as a pip package) or pip freeze it's dependencies and share the code. Read here for more

pip freeze will produce a similar list of the installed packages, but the output uses the format that pip install expects. A common convention is to put this list in a requirements.txt file:

(tutorial-env) $ pip freeze > requirements.txt
(tutorial-env) $ cat requirements.txt
novas==3.1.1.3
numpy==1.9.2
requests==2.7.0

The requirements.txt can then be committed to version control and shipped as part of an application. Users can then install all the necessary packages with install -r:

(tutorial-env) $ python -m pip install -r requirements.txt
Collecting novas==3.1.1.3 (from -r requirements.txt (line 1))
 ...
Collecting numpy==1.9.2 (from -r requirements.txt (line 2))
 ...
Collecting requests==2.7.0 (from -r requirements.txt (line 3))
 ...
Installing collected packages: novas, numpy, requests
 Running setup.py install for novas
Successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0

One alternative worth mentioning is to containerize your application (using docker or similar technology) and share it. This would help address the common "works on my machine" issue.

Containerized Python Development

Tibebes. M
  • 6,940
  • 5
  • 15
  • 36
2

Typically you would compile it as an exe or app. Some common utilities for this are pyinstaller, py2exe, and py2app.

Dan Curry
  • 149
  • 8
  • wouldn't that go against python being platform agnostic? – Tibebes. M Jul 28 '21 at 16:57
  • @Tibebes.M no. Python isn't "platform-agnostic" anyways. You can't use a Python program utilizing `ctypes` and Windows DLLs on Linux, for example. – MattDMo Jul 28 '21 at 17:00
  • @Tibebes.M IDK... presumably the OP knows the environment into which the script would be deployed. In any case, at least one of the options I listed allows for cross-compilation. Gotta start somewhere! – Dan Curry Jul 28 '21 at 17:02
  • @MattDMo the packages might be platform specific but the language as a whole is independent AFAIU – Tibebes. M Jul 28 '21 at 17:04
  • @Tibebes.M that is correct, but there is no requirement to make your programs as platform-agnostic as possible. Single-platform executable makers like `py2exe` and `py2app` are not looked down upon at all, as your comment would seem to suggest. – MattDMo Jul 28 '21 at 17:18
2

You can dockerize it, so it can be run simply with:

docker-compose up --build

See more details in https://docs.docker.com/language/python/build-images/

cavalcantelucas
  • 1,362
  • 3
  • 12
  • 34
2

You can just upload it to a code repository like GitHub and people can download it. Or you can just create an executable from the project.

enzo
  • 9,861
  • 3
  • 15
  • 38
0

In your case I'd heavily suggest you build a docker container and archive the code via git. In my experience, just doing pip freeze > requirements.txt does not suffice when the intended audience is not python literate. Especially issues with venv and conda setups have cost me way to many hours of my life.

AlexNe
  • 926
  • 6
  • 22