Python 3.8.1 win 10 cmd sys args issue [duplicate] - python

This question already has an answer here:
Python Script does not take sys.argv in Windows
(1 answer)
Closed 2 years ago.
Does anyone know why I can't send arguments when executing a script?
I have installed python 3.8.1, Windows 10 x64. I have an environment variable (the folder where my scripts are). I can execute the scripts like this:
nameScript.py
and it works but if I put
nameScript.py 1
and in this script I use that '1', sys.argv[1] give an error: Index out of bounds.
If I execute the script like:
python D/path_to_script/nameScript.py 1
it works.

your editor configuration is different than default command prompt.May be that is not reading your path and other things.
when executing like
nameScript.py 1
it understand that nameScript.py is a program and 1 is argument.
hence
sys.argv[0] = 1
sys.argv[1] = Error

sys.argv is filled with command line arguments. The first one sys.argv[0] is the script name, then sys.argv[1] is the first argument. Like this:
python myscript.py arg1 arg2
Then sys.argv[0] is 'myscript.py' sys.argv[1] is 'arg1' sys.argv[2] is 'arg2'
I think you need to create an enviroment variable that points to your python/bin folder instead your project/script folder.

Related

Run python file from bash, send parameter from bash to py and output the result by echo on bash [duplicate]

This question already has answers here:
How to pass a Bash variable to Python?
(4 answers)
How do I set a variable to the output of a command in Bash?
(15 answers)
Command not found error in Bash variable assignment
(5 answers)
Closed 2 years ago.
The question is related to Linux Debian, bash and python3.
Run python3 file from bash, send parameter from bash to py and output the result by echo on bash.
The follow are a sample which works for me. This one start a python3 script by bash and get the outpup of python3 script on terminal.
bash_script.sh
#!/bin/bash
python3 python_script.py
python_script.py
#!/usr/bin/env python3
import random # import the random module
# determining the values of the parameters
mu = 100
sigma = 25
print(random.normalvariate(mu, sigma))
Whats my question:
How to change the code, for send the parameter for "mu" and "sigma" from bash to python and output the python result on bash by "echo $python_script_output" ?
Follow the not working draft for a solution:
bash_script.sh
#!/bin/bash
mu = 100
sigma = 25
python3 python_script.py
echo $python_script_output
python_script.py
#!/usr/bin/env python3
import random
print(random.normalvariate(mu, sigma))
Is there a particular reason you are looking to run the python script via bash?
If not, you can use in-built python module sys to pass arguments to the python script and print results directly on terminal.
import sys
def func(arg1, arg2):
# do_somehing
arg1, arg2 = sys.argv[1], sys.argv[2]
print(func(arg1, arg2))
Then you can use it directly from bash
python script_name.py arg1 arg1

How to read set environment variable in python [duplicate]

This question already has answers here:
Reading and writing environment variables in Python? [duplicate]
(4 answers)
Closed 7 years ago.
In UNIX from command line, I do
setenv HOME <path to home>
I pass it as argument to my python script
python hello.py HOME
and do
sys.argv[1] = os.environ["HOME"]
still it doesn't read the path.
I am new to python, is os.environ correct for this case?
If your aim is to get the path of the home directory as argument, you can just make your user send it by making shell evaluate the argument before calling the script.
I have a simple script like -
import sys
print(sys.argv[1])
In Windows I call it as -
set HOME=D:\
python script.py %HOME%
The output I get is -
D:\
In Linux -
$ python hello.py $HOME
output -
/home/random/
If you want to get it from the environment variable, and you want to pass the environment variable to use as the first argument to the script, then you should change your script like -
sys.argv[1] = os.environ.get(sys.argv[1],sys.argv[1])
This would
It seems this depends a little on your shell. For example, in Linux using bash 4.3-7ubuntu1.5:
$ export XXX="abc"
$ python
>>> import os
>>> os.environ["XXX"]
'abc'
>>> os.environ["HOME"]
'/home/alan'

Starting a VirtualBox VM from a Python Script

I have this simple script..that does not work
import subprocess
subprocess.call(["C:\Program Files\Oracle\VirtualBox\VBoxManage.exe", "VBoxManage startvm WIN7"])
I have the same thing in a bat file...which works perfectly.
cd C:\Program Files\Oracle\VirtualBox
VBoxManage startvm "WIN7"
I have the VBoxManage.exe in the PATH of Windows 8.1 (My host OS).
The python script understands the VBoxManage executable and spits out it's manual and then this ..
Syntax error: Invalid command 'VBoxManage startvm WIN7'
Could you give me a way to start a VM from inside a python script, either by invoking the .exe directly or by running the .bat file ?
Note: I have searched for the vboxshell.py file but not found it anywhere...:[
subprocess.call() expects a list of arguments, like so:
subprocess.call(['C:\Program Files\Oracle\VirtualBox\VBoxManage.exe',
'startvm',
'WIN7'])
Your code passes 'VBoxManage startvm WIN7' as a single argument to VBoxManage.exe, which expects to find only a command (e.g. 'startvm') there. The subsequent arguments ('WIN7' in this case) need to be passed separately.
In addition, there is no need to repeat the executable name when using subprocess.call(). The example from the Python docs invokes the UNIX command "ls -l" as follows:
subprocess.call(['ls', '-l'])
In other words, you don't need to repeat the 'VBoxManage' part.
The trick is to pass the command as separate arguments
import subprocess
subprocess.call(["C:\Program Files\Oracle\VirtualBox\VBoxManage.exe", "startvm", "WIN7"])

How can you obtain the OS's argv[0] (not sys.argv[0]) in Python?

I want to obtain the true value of the operating system's argv[0] in a Python program. Python's sys.argv[0] is not this value: it is the name of the Python script being executed (with some exceptions). What I want is a foo.py that will print "somestring" when executed as
exec -a "somestring" python foo.py
The trivial program
#! /usr/bin/env python
import sys
print sys.argv[0]
will print "foo.py" instead.
Does anyone know how to obtain this? There are some related functions in the Python C API: e.g. Py_GetProgramName. But this doesn't seem to be exposed to the Python world anywhere. Py_GetProgramFullPath works off of argv[0] but munges it try to obtain a path to a Python interpreter. (This value is propagated to sys.executable, so that variable isn't right either.) Do I really have to write a C module to get this value?
Edit: Also asked (but not helpfully answered) here.
On Linux you can read the contents of /proc/self/cmdline:
#!/usr/bin/env python
import sys
print sys.argv[0]
f = open('/proc/self/cmdline', 'rb')
cmdline = f.read()
f.close()
print repr(cmdline.split('\x00'))
And the output is:
$ bash
$ exec -a "somestring" python foo.py
foo.py
['somestring', 'foo.py', '']
There seems to be a bug in bash The exec command replaces the shell closing the terminal session after the interpreter exits. That's the first bash for.

changing the process name of a python script [duplicate]

This question already has answers here:
Is there a way to change effective process name in Python?
(10 answers)
Closed 7 years ago.
Is there a way to change the name of a process running a python script on Linux?
When I do a ps, all I get are "python" process names.
There is simplier (you don't need import any libs) but maybe not so elegant way. You have to do not use "env" inside the shebang line.
In other words, this will be named as "python" in process list:
#!/usr/bin/env python
But this will be named with your scriptname:
#!/usr/bin/python
So you'll be able to find it with something like pidof -x scriptname or ps -C scriptname
http://code.google.com/p/procname/
Sample usage:
# Lets rename:
>>> procname.setprocname('My super name')
# Lets check. Press Ctrl+Z
user#comp:~/procname$ ps
PID TTY TIME CMD
13016 pts/2 00:00:00 bash
13128 pts/2 00:00:00 My super name <-- it's here
It will only work on systems where prctl system call is present and supports PR_SET_NAME command.
the procname library didn't work for me on ubuntu. I went with setproctitle instead (pip install setproctitle). This is what gunicorn uses and it worked for me.
There is the option of doing the following, though it only works on linux (with the prctl(2) call)
if sys.platform == 'linux2':
import ctypes
libc = ctypes.cdll.LoadLibrary('libc.so.6')
libc.prctl(15, 'My Simple App', 0, 0, 0)

Categories