|
INTERVIEW QUESTIONS
WEB
PYTHON
DETAILS
Question: How do I make a Python script executable on Unix?
Answer: You need to do two things: the script file's mode must be executable and the first line must begin with #! followed by the path of the Python interpreter.
The first is done by executing chmod +x scriptfile or perhaps chmod 755 scriptfile. The second can be done in a number of ways. The most straightforward way is to write
#!/usr/local/bin/python
as the very first line of your file, using the pathname for where the Python interpreter is installed on your platform.
If you would like the script to be independent of where the Python interpreter lives, you can use the "env" program. Almost all Unix variants support the following, assuming the python interpreter is in a directory on the user's $PATH:
#! /usr/bin/env python
Don't do this for CGI scripts. The $PATH variable for CGI scripts is often very minimal, so you need to use the actual absolute pathname of the interpreter.
Occasionally, a user's environment is so full that the /usr/bin/env program fails; or there's no env program at all. In that case, you can try the following hack (due to Alex Rezinsky):
#! /bin/sh """:" exec python $0 ${1+"$@"} """
The minor disadvantage is that this defines the script's __doc__ string. However, you can fix that by adding
__doc__ = """...Whatever..."""
|
|
|
Category |
Python Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.2) By 9511 users |
Added on |
9/23/2014 |
Views |
67479 |
Rate it! |
|
|
Question:
How do I make a Python script executable on Unix?
Answer:
You need to do two things: the script file's mode must be executable and the first line must begin with #! followed by the path of the Python interpreter.
The first is done by executing chmod +x scriptfile or perhaps chmod 755 scriptfile. The second can be done in a number of ways. The most straightforward way is to write
#!/usr/local/bin/python
as the very first line of your file, using the pathname for where the Python interpreter is installed on your platform.
If you would like the script to be independent of where the Python interpreter lives, you can use the "env" program. Almost all Unix variants support the following, assuming the python interpreter is in a directory on the user's $PATH:
#! /usr/bin/env python
Don't do this for CGI scripts. The $PATH variable for CGI scripts is often very minimal, so you need to use the actual absolute pathname of the interpreter.
Occasionally, a user's environment is so full that the /usr/bin/env program fails; or there's no env program at all. In that case, you can try the following hack (due to Alex Rezinsky):
#! /bin/sh """:" exec python $0 ${1+"$@"} """
The minor disadvantage is that this defines the script's __doc__ string. However, you can fix that by adding
__doc__ = """...Whatever...""" Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
Where is the math.py (socket.py, regex.py, etc.) source file?
|
View Answer
|
|
When I edit an imported module and reimport it, the changes don't show up. Why does this happen?
|
View Answer
|
|
How do I find the current module name?
|
View Answer
|
|
How can I overload constructors (or methods) in Python?
|
View Answer
|
|
How do I create static class data and static class methods?
|
View Answer
|
|
How can I organize my code to make it easier to change the base class?
|
View Answer
|
|
How do I call a method defined in a base class from a derived class that overrides it?
|
View Answer
|
|
I want to do a complicated sort: can you do a Schwartzman Transform in Python?
|
View Answer
|
|
How do I apply a method to a sequence of objects?
|
View Answer
|
|
How do I apply a method to a sequence of objects?
|
View Answer
|
|
How do I create a multidimensional list?
|
View Answer
|
|
How do you remove duplicates from a list?
|
View Answer
|
|
How do I iterate over a sequence in reverse order?
|
View Answer
|
|
What's a negative index?
|
View Answer
|
|
How do I convert between tuples and lists?
|
View Answer
|
|
Is there a scanf() or sscanf() equivalent?
|
View Answer
|
|
Is there an equivalent to Perl's chomp() for removing trailing newlines from strings?
|
View Answer
|
|
How do I use strings to call functions/methods?
|
View Answer
|
|
How do I modify a string in place?
|
View Answer
|
|
How do I convert a number to a string?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All Python Interview Questions & Answers - Exam Mode /
Learning Mode
|