2

I love Python for everything that it is except the fact that I can't ship a single binary file of my project to someone and expect them to be able to run it.

I'm not asking for a simple Python script case. Say, I have a python script that imports several modules (which were pip installed on my local system). I'd like to turn it into a single file or directory that I can ship to a remote server and just run it. No pip install blah should be needed there.

Is it possible?

EDIT1: I’m only concerned about distributing my script along with its dependancies to a remote x86_64 GNULinux server. Py2exe doesn’t help. I’m looking for its equivalent in the Linux world.

I have used virtualenv to install all modules in the same directory, but don't know if there is a way to ship just that directory and expect it to "just work" without pip installs there.

I'm at the verge of giving up on my years of Python practice and switching to a different language like Nim or Go just for this reason.

  • Why not give them a bash that sets the virtualenv for them? – Adirio Oct 18 '17 at 14:13
  • 2
    Possible duplicate of [How to compile python script to binary executable](https://stackoverflow.com/questions/12339671/how-to-compile-python-script-to-binary-executable) – Souradeep Nanda Oct 18 '17 at 14:15
  • 1
    @souradeep I missed to specify that my concern is restricted to remote unix/GNULinux servers I can SSH to. In that context it is not a duplicate to the linked question. – Aravindh Sathish Oct 18 '17 at 14:24
  • @Adrio, do you mean a bash script that installs the dependancies in the remote machine? I do not want to pip install anything on the remote machine. The remote machine may not even have access to pypi repository if it is behind a firewall. – Aravindh Sathish Oct 18 '17 at 14:28
  • If you're prepared to recreate the virtual environment on the other side, it would only take a few lines: on source machine run `pip freeze`, then on target machine run `virtualenv ...` then `pip install -r requirements.txt`. Of course your server would need access to the internet (or to a local devpi server). – Arthur Tacca Oct 18 '17 at 14:32
  • BTW pyinstaller works fine on Linux e.g. it's what Docker Compose uses. – Arthur Tacca Oct 18 '17 at 14:33

2 Answers2

3

I think PyOxidizer is what you're looking for.

Turtles Are Cute
  • 3,200
  • 6
  • 30
  • 38
  • 1
    I guess this got downvoted due to its brevity, but PyOxidizer is in fact an excellent option with some more advanced features than pyinstaller. See answer here: https://stackoverflow.com/questions/12339671/how-to-compile-python-script-to-binary-executable/65401152#65401152 – jotap Dec 21 '20 at 23:06
1

There's options like py2exe that just package the scripts into a Microsoft executable (files can exist inside Microsoft executables), or pyinstaller. However, Python's runtime and language specification is not designed to be converted to machine code. If it's not okay to distribute your program as a pyinstaller/py2exe package, then you should look towards compiled programming languages.

Previously asked here How to compile python script to binary executable

boxmein
  • 865
  • 7
  • 19
  • 1
    Thanks for your response. I missed to mention that my need was specific to GNU/Linux servers. Py2exe is not helpful here. I will check out pyinstaller. – Aravindh Sathish Oct 18 '17 at 14:31
  • Note that as long as you run on hardware you control, Python may be good enough. – boxmein Oct 18 '17 at 16:26