1

My python script runs with several imports. On some systems where it needs to run, some of those modules may not be installed. Is there a way to distribute a standalone script that will automagically work? Perhaps by just including all of those imports in the script itself?

appsecguy
  • 1,019
  • 3
  • 19
  • 36
  • 1
    This is usually done with pip and a [requirements.txt](https://pip.readthedocs.org/en/1.1/requirements.html) file. Does that not meet your needs? – Andy Jul 24 '14 at 17:00
  • It rather depends on your target platform - for example `py2exe` is an option if you're distributing only to windows. – roippi Jul 24 '14 at 17:01
  • 1
    if you have installed pip you can use some similar to this http://stackoverflow.com/questions/17271444/how-to-install-a-missing-python-package-from-inside-the-script-that-needs-it – JuanPablo Jul 25 '14 at 21:12

2 Answers2

0

Including all necessary modules in a standalone script is probably extremely tricky and not nice. However, you can distribute modules along with your script (by distributing an archive for example).

Most modules will work if they are just installed in the same folder as your script instead of the usual site-packages. According to the sys.path order, the system's module will be loaded in preference to the one you ship, but if it doesn't exist the later will be imported transparently.

You can also bundle the dependencies in a zip and add that zip to the path, if you think that approach is cleaner.

However, some modules cannot be that flexible. One example are extensions that must first be compiled (like C extensions) and are thus bound to the platform.

IMHO, the cleanest solution is still to package your script properly using distutils and proper dependency definition and write some installation routine that installs missing dependencies from your bundle or using pip.

Cilyan
  • 7,883
  • 1
  • 29
  • 37
-1

You can take a look at python Eggs http://mrtopf.de/blog/en/a-small-introduction-to-python-eggs/

Teixi
  • 1,077
  • 8
  • 21