Multi Platform Portable Python

Cross-platform deployment and easy installation

I've got a project that sounds vaguely similar to what you're trying to do and I've seen some of the same problems since I usually develop on Linux and port to Windows. It's a Python + wxPython + NumPy + SciPy + matplotlib + assorted other packages, and what I've found to work best is to use PyInstaller. PyInstaller does an excellent job of handling third party Python packages and creates an EXE pretty painlessly.

I think if you're using py2exe or PyInstaller it more or less has to be done on Windows since IIRC there are a few Windows libs that have to come along for the ride. Maybe you could run a Windows EC2 instance? Depending on how complicated your application is, you might instead be able to make something work with Portable Python or PyPy.

I have tried downloading Python and required packages before for Windows boxes - it worked but it was always a little fragile. If you can find an installer builder that lets you specify dependencies (e.g. Advanced Installer-not free but works well), I'd try that first as it seems to be a little more robust.

how to make a python or perl script portable to both linux and windows?

Windows will just ignore the shebang (which is, after all, a comment); in Windows you need to associate the .py extension to the Python executable in the registry, but you can perfectly well leave the shebang on, it will be perfectly innocuous there.

There are many bits and pieces which are platform-specific (many only exist on Unix, msvcrt only on Windows) so if you want to be portable you should abstain from those; some are subtly different (such as the detailed precise behavior of subprocess.Popen or mmap) -- it's all pretty advanced stuff and the docs will guide you there. If you're executing (via subprocess or otherwise) external commands you'd better make sure they exist on both platforms, of course, or check what platform you're in and use different external commands in each case.

Remember to always use /, not \, as path separator (forward slash works in both platforms, backwards slash is windows-only), and be meticulous as to whether each file you're opening is binary or text.

I think that's about it...

Is python's hash() portable?

No, hash() is not guaranteed to be portable.

Python 3.3 also uses hash randomisation by default, where certain types are hashed with a hash seed picked at start-up. Hash values then differ between Python interpreter invocations.

From the object.__hash__() documenation:

By default, the __hash__() values of str, bytes and datetime objects are “salted” with an unpredictable random value. Although they remain constant within an individual Python process, they are not predictable between repeated invocations of Python.

This is intended to provide protection against a denial-of-service caused by carefully-chosen inputs that exploit the worst case performance of a dict insertion, O(n^2) complexity. See http://www.ocert.org/advisories/ocert-2011-003.html for details.

Changing hash values affects the iteration order of dicts, sets and other mappings. Python has never made guarantees about this ordering (and it typically varies between 32-bit and 64-bit builds).

See also PYTHONHASHSEED.

Python 2.6.8 and 3.2.3 and newer support the same feature but have it normally disabled.

Python 3.2 introduced a sys.hash_info named tuple that gives you details about the hash implementation for the current interpreter.

If you need a portable hash, there are plenty of implementations. The standard library includes a cryptographic hash library called hashlib; these implementations are definitely portable. Another option would be the mm3 package which provides Murmur3 non-cryptographic hash function implementations.

Common data structures would need to be converted to bytes first; you could use serialisation for that, like the json or pickle modules.



Related Topics



Leave a reply



Submit