Martin
Martin

Reputation: 165

Making a Python Script usable without import

I have a Python Script that uses several imports (for example gdal, numpy, pygtk). I would like this script to run on other PCs without having to say something like "Before using this Programm you have to install gdal...."

What i have done is, whenever i use an imported method i rightclick, click show definition and then just copy paste all necessary modules directly into my script.

That works, but what I would like to know is: Is there an easier or more elegant way to do this?

Thanks

Upvotes: 2

Views: 1575

Answers (3)

Costa Halicea
Costa Halicea

Reputation: 160

The preffered way of packaging your apps is using a setup.py script (using distutils) Have a look at http://docs.python.org/2/distutils/setupscript.html.

In the setup.py script you will need to provide all the dependencies and when the users install your app (using python setup.py install) it will check the dependencies and install them for you.

Additionaly if you would like to ship a single executable please have a look at :

  1. http://www.pyinstaller.org/ to make an exe or a linux executable
  2. http://pythonhosted.org/py2app/ to make a mac os executable.

In both cases having a proper setup.py script is required

Upvotes: 5

furins
furins

Reputation: 5048

Since the dependencies you mention are not pure-python and supposing you are working/distribute on windows only, you may consider using py2exe to create an executable (in this way you may eventually avoid people to install python as well).

Upvotes: 1

Preet Kukreti
Preet Kukreti

Reputation: 8607

You can simply copy the relevant python module directories into source-relative path.

However the main problem remains the same: You do not get the benefit of updates and bugfixes without significant work each time something is updated. One benefit of this and your current approach is that you can lock down behaviour to be less prone to versioning side effects on the client machine.

However the "correct" way to do this is to turn your python script into a module with a setup script, and specify module dependencies via distutils.

If the dependencies are not pure-python, you may run into problems with client-side compilation, in which case, you would probably need to package pre-built module distributions.

Upvotes: 2

Related Questions