Execution of Python Code with -M Option or Not

Run a Python script from another Python script, passing in arguments

Try using os.system:

os.system("script2.py 1")

execfile is different because it is designed to run a sequence of Python statements in the current execution context. That's why sys.argv didn't change for you.

Python: significance of -u option?

From python --help:

-u     : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x
see man page for details on internal buffering relating to '-u'

The manpage states:

-u     Force  stdin,  stdout and stderr to be totally unbuffered.  On systems where it matters, also put stdin,
stdout and stderr in binary mode. Note that there is internal buffering in xreadlines(), readlines()
and file-object iterators ("for line in sys.stdin") which is not influenced by this option. To work
around this, you will want to use "sys.stdin.readline()" inside a "while 1:" loop.

Python opens the stdin, -out and -error streams in a buffered mode; it'll read or write in larger chunks, keeping data in memory until a threshold is reached. -u disables those buffers.

Also, python can interpret newlines on open files and translate them from and to the native platform newlines (text mode). The -u option disables this translation, allowing you to process binary data without having to worry about what might happen to \r\n combinations. It is the equivalent of using rb or wb modes when opening files with the open() function.

how to run python script without typing 'python ...'

You've got to add the shebang:

#!/usr/bin/env python

Then make the script executable:

chmod +x foo

Then you can run it like any other executable:

./foo

And a note from Homer6: if you're editing the file from windows and invoking it on linux, you may run into the cryptic "No such file or directory" error. It's due to the line endings of the lines being CRLF instead of LF. If you convert them to LF, the script will execute as expected. Notepad++ > View > Show Symbols > Show End of Line to show the EOL characters. And Notepad++ > Edit > EOL Conversion > Unix Format to convert all line endings to use LF. Alternatively, you can use the dos2unix tool (dos2unix foo.py), which is present on most Linux systems.

How to continue execution of Python script after evaluating a Click cli function?

Actually the Click documentation provides a good explanation for why this occurs, and how to change this behavior.

By default all commands inherit from BaseCommand, which defines a main method that by default exits upon successful completion with sys.exit(), which emits the SystemExit exception.

To change this behavior, one may disable what they term standalone_mode as documented here.

So for the example provided in my question, changing the line containing get_inputs() call in test_cli.py from print(get_inputs()) into print(get_inputs.main(standalone_mode=False)), then invoking the script from the command line gives the desired behavior, like so:

$ python test_cli.py --test-option test123
before calling get_inputs
test123
after calling get_inputs

Is there a way to stop a program from running if an input is incorrect? Python

First import these :-

import sys

Then add this after that "sorry we don't allow under 18" line

input('Press Enter to Continue...')
sys.exit()

Plus use if instead of while

Your code can be simplified as :-

import sys

first_name=input('PLEASE ENTER YOUR FIRST NAME: ')
last_name=input('PLEASE ENTER YOUR SURNAME: ')
print(f'Hello {first_name} {last_name}, before you enter.')

def age_det():
age = int(input(f'How old are you?'))
if num>=18:
print(f'Awesome, {first_name}, welcome to Blablabla')
else :
print(f'Sorry, {first_name}, but we require you to be at least 18 to enter Blablabla.')
input('Press Enter to Continue...')
sys.exit()

age_det()

How do I execute a program from Python? os.system fails due to spaces in path

subprocess.call will avoid problems with having to deal with quoting conventions of various shells. It accepts a list, rather than a string, so arguments are more easily delimited. i.e.

import subprocess
subprocess.call(['C:\\Temp\\a b c\\Notepad.exe', 'C:\\test.txt'])

How to execute .py file with double-click

Doing the following should fix it:

  1. Right click on the .py file you want to open;
  2. Open with -> Choose default program -> More options;
  3. Select the python.exe file.

Explanation:

Your Python scripts have to be processed by another program called the Python interpreter. The interpreter reads your script, compiles it into bytecodes, and then executes the bytecodes to run your program.

Installing a new version might have messed the path to the Python interpreter. The steps listed above will tell Windows to associate .py files with your Python interpreter, thus fixing the issue.

This link with Python on Windows FAQ might also be of help.

How to execute Python scripts in Windows?

When you execute a script without typing "python" in front, you need to know two things about how Windows invokes the program. First is to find out what kind of file Windows thinks it is:


C:\>assoc .py
.py=Python.File

Next, you need to know how Windows is executing things with that extension. It's associated with the file type "Python.File", so this command shows what it will be doing:


C:\>ftype Python.File
Python.File="c:\python26\python.exe" "%1" %*

So on my machine, when I type "blah.py foo", it will execute this exact command, with no difference in results than if I had typed the full thing myself:


"c:\python26\python.exe" "blah.py" foo

If you type the same thing, including the quotation marks, then you'll get results identical to when you just type "blah.py foo". Now you're in a position to figure out the rest of your problem for yourself.

(Or post more helpful information in your question, like actual cut-and-paste copies of what you see in the console. Note that people who do that type of thing get their questions voted up, and they get reputation points, and more people are likely to help them with good answers.)

Brought In From Comments:

Even if assoc and ftype display the correct information, it may happen that the arguments are stripped off. What may help in that case is directly fixing the relevant registry keys for Python. Set the

HKEY_CLASSES_ROOT\Applications\python26.exe\shell\open\command

key to:

"C:\Python26\python26.exe" "%1" %*

Likely, previously, %* was missing. Similarly, set

 HKEY_CLASSES_ROOT\py_auto_file\shell\open\command

to the same value. See http://eli.thegreenplace.net/2010/12/14/problem-passing-arguments-to-python-scripts-on-windows/

example registry setting for python.exe
HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command The registry path may vary, use python26.exe or python.exe or whichever is already in the registry.

Sample Image
HKEY_CLASSES_ROOT\py_auto_file\shell\open\command



Related Topics



Leave a reply



Submit