How to make my python script simple portable? or how to compile into binary files with all module dependencies?

Is there a way to compile a python script into a binary? I have one python script file that uses many modules. I would like to have a copy of it on other machines (freebsd), but without installing all the necessary modules on each host.

What are the possible solutions in such cases?

Thanks in advance!

+8
python binary freebsd
source share
5 answers

The programs that can fulfill your requests are as follows:

But, as already mentioned, you can also create a distribution package and have other packages as dependencies. Then you can use pip to install this package and it will install all the packages. However, you still need to install Python and pip.

+15
source share

cx_freeze will add your python scripts to the standalone Python loader and create a directory containing the program and dependencies on the shared library. Then you can copy the resulting distribution to other machines independent of Python or your modules.

 $ cat hello.py print "Hello, World!" $ ls dist/ datetime.so _heapq.so hello libpython2.6.so.1.0 readline.so $ cat hello.py print "Hello, World!" $ cxfreeze hello.py ... <snip> ... $ ls dist/ datetime.so _heapq.so hello libpython2.6.so.1.0 readline.so $ ./dist/hello Hello, World! 

The best answer would be to create a PIP package that identifies these third modules as dependencies, so installation can be as simple as pip install mypackage; .package

+3
source share

Python will also look for import modules in the current directory, so you do not need to install them in the python directory. Your distribution structure may look like this:

 main.py module1/ __init__.py, ... module2/ __init__.py, ... 

Where main.py has import module1, module2

+2
source share

You probably want to create a Python package from a script. As a result, you can make pip install mypackage on any host, and all the necessary modules will be downloaded and installed automatically.

Take a look at this question on how to create such a package .

+2
source share

I have a script that imports these modules: urllib, urllib2, cookielib, BaseHTTPServer, sys, tempfile, paramiko, logging, re, OptionParser, lxml.

You probably want to create a Python package from your script. In the end, you can configure pip mypackage on any host and all required modules will be downloaded and installed automatically.

  • suppose i have a script like python package
  • copied it to another host
  • start pip install mypackage

As I understand it, it will look for modules that should be imported and will download and install dependencies.

This is not a good solution in my case. Users of other hosts (~ 20) should be able to run the script without additional download / installation procedures.

Hope cx_freeze is what I need. Thank you for your responses.

0
source share

Source: https://habr.com/ru/post/650532/


All Articles