473,378 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Platform independent code?

I have read that Python is a platform independent language. But on this
page:

http://docs.python.org/tut/node4.htm...00000000000000

it seems that making a python script executable is platform dependant:

2.2.2 Executable Python Scripts
On BSD'ish Unix systems, Python scripts can be made directly executable,
like shell scripts, by putting the line
#! /usr/bin/env python
(assuming that the interpreter is on the user's PATH) at the beginning of
the script and giving the file an executable mode. The "#!" must be the
first two characters of the file. On some platforms, this first line must
end with a Unix-style line ending ("\n"), not a Mac OS ("\r") or Windows
("\r\n") line ending. Note that the hash, or pound, character, "#", is used
to start a comment in Python.

The script can be given an executable mode, or permission, using the chmod
command:
$ chmod +x myscript.py

Are there any guidelines (API'S) that gurantees that the python code will be
platform independent?
Jun 27 '08 #1
3 7474
Hi,

Python is a platform independent language, period. You can always
excute a Python script with python script.py. Now, with Windows, you
can execute the script by doucle-clicking on it. With Linux, it's
different, you have to use the shebang line to execute a script with
the correct interpreter. But this has nothing to do with the fact that
Python is a platform independent language.
Some modules may not be available on all platform, for the answer to
this question, see the documentation of the module ;)

Matthieu

2008/6/14 saneman <as*@ad.com>:
I have read that Python is a platform independent language. But on this
page:

http://docs.python.org/tut/node4.htm...00000000000000

it seems that making a python script executable is platform dependant:

2.2.2 Executable Python Scripts
On BSD'ish Unix systems, Python scripts can be made directly executable,
like shell scripts, by putting the line
#! /usr/bin/env python
(assuming that the interpreter is on the user's PATH) at the beginning of
the script and giving the file an executable mode. The "#!" must be the
first two characters of the file. On some platforms, this first line must
end with a Unix-style line ending ("\n"), not a Mac OS ("\r") or Windows
("\r\n") line ending. Note that the hash, or pound, character, "#", is used
to start a comment in Python.

The script can be given an executable mode, or permission, using the chmod
command:
$ chmod +x myscript.py

Are there any guidelines (API'S) that gurantees that the python code will be
platform independent?
--
http://mail.python.org/mailman/listinfo/python-list


--
French PhD student
Website : http://matthieu-brucher.developpez.com/
Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92
LinkedIn : http://www.linkedin.com/in/matthieubrucher
Jun 27 '08 #2
saneman wrote:
I have read that Python is a platform independent language. But on this
page:

http://docs.python.org/tut/node4.htm...00000000000000

it seems that making a python script executable is platform dependant:

2.2.2 Executable Python Scripts
On BSD'ish Unix systems, Python scripts can be made directly executable,
like shell scripts, by putting the line
#! /usr/bin/env python
(assuming that the interpreter is on the user's PATH) at the beginning of
the script and giving the file an executable mode. The "#!" must be the
first two characters of the file. On some platforms, this first line must
end with a Unix-style line ending ("\n"), not a Mac OS ("\r") or Windows
("\r\n") line ending. Note that the hash, or pound, character, "#", is used
to start a comment in Python.

The script can be given an executable mode, or permission, using the chmod
command:
$ chmod +x myscript.py

Are there any guidelines (API'S) that gurantees that the python code will be
platform independent?

Generally you have to stay away from platform "dependent" constructs. Esamples:

1) Use os.path methods everywhere in your code so you won't need to worry about
os.path.sep (slash or backslash).

2) Use lowest common denominator when naming files to make your scripts work
cross platform.

3) Stay away from os-specific calls/interfaces when possible. Manipulating file
permissions, etc is always os-specific.

4) Use config files instead of relying on registry in Windows.

5) Stay away from code that depends on 32/64 bit objects or endian-ness of
storage (struct objects). This is machine specific not OS specific but still
counts.

6) If you have GUI, use something like wxWindows which provides cross platform
GUI support.

7) Be careful using non-Python libraries (C-libraries) should be done with care
to make sure that the library is available for all target operating systems.

8) using os.system or the subsystem module must be done with much care and
probably won't be cross platform compatible.

Hope the suggestions help.

-Larry
Jun 27 '08 #3
On Jun 14, 6:28*am, "saneman" <a...@ad.comwrote:
I have read that Python is a platform independent language. *But on this
page:

http://docs.python.org/tut/node4.htm...00000000000000

it seems that making a python script executable is platform dependant:

2.2.2 Executable Python Scripts
On BSD'ish Unix systems, Python scripts can be made directly executable,
like shell scripts, by putting the line

#! /usr/bin/env python
(assuming that the interpreter is on the user's PATH) at the beginning of
the script and giving the file an executable mode. The "#!" must be the
first two characters of the file. On some platforms, this first line must
end with a Unix-style line ending ("\n"), not a Mac OS ("\r") or Windows
("\r\n") line ending. Note that the hash, or pound, character, "#", is used
to start a comment in Python.

The script can be given an executable mode, or permission, using the chmod
command:

$ chmod +x myscript.py

Are there any guidelines (API'S) that gurantees that the python code will be
platform independent?
The only guarantee is testing it on both yourself. Some modules are
inherently os-dependant (much of the 'os' module for example), and
some constructs just don't work on all platforms (using
os.environ['LOGNAME'] or system('cat file') as examples).

If you stick to python code that doesn't touch the OS directly, you
won't (read: shouldn't) have a problem, but when you start interacting
with the OS directly you have to think hard about what you're doing.

--Buck
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: MK | last post by:
What would be the best platform-independent way to refer to the current (execute) path for a Python script? self.dirname = ??? This variable should contain directory name for the current path....
1
by: Hans Georg Krauthaeuser | last post by:
Hey all, this is probably a FAQ, but I didn't found the answer... I use msvcrt.kbhit() to check for a user keyboard event on windows. But now, I would prefer to make the module independent...
10
by: RA Scheltema | last post by:
hi all, A small question about serializing and deserializing a long in a platform independent manner. Can this be done with the following code ?: char buf; long val = 35456;
0
by: pythonhelpneeded | last post by:
While building Python 2.4.3 on SCO OpenServer 5.0.5, I'm getting the error below. The modules mentioned seem to be available in the Lib subdirectory. I've tried setting PYTHONHOME to the build...
16
by: bobrik | last post by:
Hello, I am using the Python DB API for access to MySQL. But it is not platform-independent - I need a module not included in Python by default - python-mysql, and it uses a compiled binary...
1
by: /M | last post by:
Hi! As you know, one way to seperate platform dependent code from platform _independent_ code is to create an abstract base class which acts as an interface towards all platform independent code...
4
by: archana | last post by:
Hi all, can anyone tell me does dot net platform independent? Why can't i run dot net on unix operating system?. thanks in advance.
12
by: Udhay | last post by:
I am new to vc++. Whether Visual Studio is a platform independent? Is there any chance of using my application created by vc in MAC or Linux. udhay
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.