1

I am using Python 2.7.5+ on my Linux Mint to write simple programs as .py files and running them in Konsole Terminal. These work fine on my computer, but I need to share them with a friend using Windows (IDLE I suppose) and wonder if these will work as they are without modification.

The programs start with the usual #!/usr/bin/python, you know.

  • They should work fine as long as you aren't using any platform-specific features. – scai Apr 02 '14 at 08:10
  • Unless your code have Unix specific modules (select, dbus etc), you're fine on the running part. As for how to run the code by just invoking it is a bit different on the two platforms. `#!/python27/python` would need to be placed for the windows system to properly run it, altho that's mostly for IDLE or your text-editors run environment. I'd make sure `C:\Python27` is in your friends `%%PATH%%` on the windows machine and just instruct them to do `python yourcode.py`, that way both you and them can issue the same command. Or simply check out the link above for "double-click" functionality. – Torxed Apr 02 '14 at 08:11
  • @Torxed The select module is not Unix-specific, it works on Windows too. (Although not everything in that module works on all platforms, like with the os module) – Aleksi Torhamo Apr 02 '14 at 09:37
  • Start the script with `#!/usr/bin/env python` is better, as it may run in a virtual environment in Linux/UNIX. – Map X Apr 03 '14 at 06:48

2 Answers2

1

Depends on the script. Unless you use anything os-specific, you are golden.

In the standard library most of the modules are totally os-agnostic, and for the rest the rule of thumb is - "if it is possible to provide the same functionality across *nix and windows, it has probably been done".

Python actually makes it pretty easy to write portable programs. Even file paths manipulation is pretty portable if you do it right - os.path.sep instead of '/', os.path.join instead of string concatenation etc.

Notable exceptions are

  1. sockets - windows sockets are bit different
  2. multiprocessing - windows does not have fork(), that may or may not be a problem.
  3. Needless to say, things related to username, hostname and such.
  4. os and sys are a mixed bag - you should read the compatibility notes in the docs.
  5. Everything packaging and distribution-related.
thule
  • 4,034
  • 21
  • 31
0

Most Python code will work fine, but the shebang (#!) does nothing on Windows - only the filename extension (.py or .pyw) does. If you're handling filenames you'll want to use the functions in os.path. Line endings also differ, which means you'll want to tell open() whether you're reading text or binary data. This also shows up in less adaptive text editors like Windows Notepad (and sadly in Idle when pasting). Notepad++ is one capable text editor for Windows. There are also some restrictions for multiprocessing in Windows.

Yann Vernier
  • 15,414
  • 2
  • 28
  • 26