1

I have a python script where I am including a third party library:

from docx import Document.

Now, I need to run this script in an environment where bare-bones python is present but not this library.

Installing this library in the target environment is beyond my scope and I tried using distutils, but couldn't go far with it. The target environment just need to run the script, not install a package.

I am from Java background and in Java I would have just exported and created a jar file which would have included all the libraries I needed. I need to do similar with python.

Edit: With distutils, I tried creating a setup.py:

from distutils.core import setup
import docx
setup(name='mymodule',
     version='1.0',
     py_modules=['mymodule', docx]
     )

But I am not sure this works.

Himanshu
  • 39
  • 8
  • If this is a mandatory part of the script, then you *should* want to install it before it's used...does the script have a fallback option if this isn't present? – Makoto Apr 25 '15 at 19:43
  • @Makoto This is a mandatory part of the script. There is no fallback. – Himanshu Apr 25 '15 at 19:44
  • If you don't have the library on the target machine, you can't run the script. This is true in Java as well. Ignoring the library is a **bad idea** if there are no fallbacks. – Makoto Apr 25 '15 at 19:46
  • Copy the library `docx` from `site-packages` and place it in the same directory as your script. – Malik Brahimi Apr 25 '15 at 19:48
  • Or you could force the script to install the package if it doesn't exist as seen [here](http://stackoverflow.com/questions/29852851/packaging-your-python-code) in one of my recent questions. – Malik Brahimi Apr 25 '15 at 19:50
  • Could you show us what you've done with distutils? That's the real problem here. – Makoto Apr 25 '15 at 19:50

3 Answers3

0

You can include the modules for docx in your application. Just distribute them together.

But docx depends on the lmxl operating system package and needs to run setup on that. You can't just copy it to the target machine.

I'm not sure PyInstaller supports docx, especially add it has the non python dependency.

Really using pip or easy_install is the way to go.

Robert Moskal
  • 21,737
  • 8
  • 62
  • 86
0

PyInstaller won't work if you can't make a pyc file and you cannot make pyc file unless your code runs without fatal errors.

You could have the import in a try block that excepts ImportError 's but that will result in NameError 's where the package is referenced. Long story short if the package is integral to the script no amount of avoiding it will fix your problem. You need the dependencies.

You said installing the package is beyond your scope, well then, it is time to expand your scope. Docx is an open source package you can find on github here

You can download that and run setup.py

kpie
  • 9,588
  • 5
  • 28
  • 50
-2

PyInstaller is a program that converts (packages) Python programs into stand-alone executables, under Windows, Linux, Mac OS X, Solaris and AIX.

peterh
  • 11,875
  • 18
  • 85
  • 108