On 22.6.2014. 4:51, Nicholas Cannon wrote:
I have a simple program that is ran in the console with
2 modules and i was wondering how i could like export it
so i could give it to someone to use as like a utlitie
in the console?
* You have one script - script.py, using two additional module -
module1.py & module2.py.
* You want to hold the script in some generic Utility folder (on
your system path or wherever). Let's refer to that folder as
'D:\Utility', just to make it seem more realistic.
* Target machine already has the desired Python environment installed.
The simplest 'all in one' solution would be to simply copy script.py,
module1.py & module2.py into the 'D:\Utility' folder. Then you can run
script.py as a script (using whatever Python environment you prefer) and
its folder (i.e. 'D:\Utility') will automatically be added to the Python
path, ergo module1.py & module2.py can be easily imported using 'import
module1' & 'import module2' respectively.
One possible bad side to this organization is that the user does not
necessarily know what module1.py & module2.py files are - they are
stored together with other utility scripts but need not be runnable
scripts by themselves. If they can be run as standalone scripts then
that is all fine and well but if they are not - user does now know that
they should not be and possibly what they are related to.
A slight variation making it clear which scripts should be runnable
directly and which should not would be to move module1.py & module.py
under some 'D:\Utility\script_details' folder and add an empty
__init__.py file to that folder as well.
Then module1.py & module2.py can be imported as:
Another addition is to prepare a packaged installer that installs your
files in one of the aforementioned structures as any other application,
e.g. under '/usr/bin', 'C:\Program Files' or whatever, but that's
strictly an addition to what was described earlier.
If the script and the modules you mention are self-contained and are
not intended to be reused elsewhere, then I don't think you need
anything more complex than that. If they are not then you have an option
to place the implementation module in some shared Python environment
folder, e.g. a specific Python environment's 'site-packages' folder.
That would allow you to easily reuse those modules from other scripts
located in other folders, but it would also introduce additional
complications - with it you need to make sure the folder you placed them
on has indeed been configured to be located on the used Python
environment's Python path.
Another variation is to package an installer that basically installs
a stand-alone Python distribution together with your script, e.g. like
something done by cx_Freeze or similar projects. Then your target
machine does not need to have Python installed separately, but on the
other hand, the installation is much larger and you risk getting
multiple Python installations all over the same machine. :-)
Hope this helps.