How to print or log from Python under Powershell - python

I have a simple python script
name = input("Enter your name: ")
print ("Hello, " + name)
saved to test.py
I have a simple powershell script
python test.py
saved to Untitled2.ps1
Running Untitled2.ps1 from either the Powershell IDE or the command line cause no printing of the input request and no output. It hangs and has to be cancelled out. Entering data and pressing Return also cause no output.
Python 3.8.0, Windows 10, Powershell 5.1

Related

Python script errors out with "mv: target is not a directory" but command called in script works from terminal

I'm writing a Python script, here's the part of the script that does not work.
command = "mv " + cwd + sys.argv[2] + " wallpaper.jpg" + " ~/Pictures/Wallpapers/"
print(command)
subprocess.run(command.split(" "))
If I copy and paste the command printed from the script in the terminal it works, but when called from my Python script I get the error:
mv: target '~/Pictures/Wallpapers' is not a directory
What's causing this?
If needed, I'm using Python 3.8.10 and Ubuntu 20.04.3.

How to hide password input with getpass in Python 3 on Visual Studio Code? [duplicate]

When I run this code:
import getpass
p = getpass.getpass(prompt='digite a senha\n')
if p == '12345':
print('YO Paul')
else:
print('BRHHH')
print('O seu input foi:', p) # p = seu input
I got this warning:
Warning (from warnings module):
File "/usr/lib/python3.4/getpass.py", line 63
passwd = fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal. Warning: Password input may be echoed.
Use an actual terminal -- that is, an environment where stdin, stdout and stderr are connected to /dev/tty, or another PTY-compliant device.
The IDLE REPL does not meet this requirement.
Run your code in terminal, instead of the IDE. you will see that there is no more warning there.
To run your code, enter this command in terminal:
python3 your_program.py
Rather than deal with changing the current working directory in a terminal that has not started Python (which would mean you type something like python3 script.py--and it will fail unless the current working directory is already specified), start Python in your terminal and run this one-line command:
exec(open('C:\folder\script.py').read())
where you change the path string 'C:\folder\script.py' to match wherever your file is located on disk (the string does need to be specified with quotes).
use cmd ie. command prompt and then run the file in it.
like:
python abc.py

python 3 - getpass.getpass() is echoing password... nothing seems to work [duplicate]

When I run this code:
import getpass
p = getpass.getpass(prompt='digite a senha\n')
if p == '12345':
print('YO Paul')
else:
print('BRHHH')
print('O seu input foi:', p) # p = seu input
I got this warning:
Warning (from warnings module):
File "/usr/lib/python3.4/getpass.py", line 63
passwd = fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal. Warning: Password input may be echoed.
Use an actual terminal -- that is, an environment where stdin, stdout and stderr are connected to /dev/tty, or another PTY-compliant device.
The IDLE REPL does not meet this requirement.
Run your code in terminal, instead of the IDE. you will see that there is no more warning there.
To run your code, enter this command in terminal:
python3 your_program.py
Rather than deal with changing the current working directory in a terminal that has not started Python (which would mean you type something like python3 script.py--and it will fail unless the current working directory is already specified), start Python in your terminal and run this one-line command:
exec(open('C:\folder\script.py').read())
where you change the path string 'C:\folder\script.py' to match wherever your file is located on disk (the string does need to be specified with quotes).
use cmd ie. command prompt and then run the file in it.
like:
python abc.py

Executing Python commands in Jupyter notebook with prompts

When the function below is entered into a notebook cell, the output will have an input box to enter the details of the prompt.
def a():
val = raw_input("abc")
print "Entered value: %s" % val
I'm trying to achieve the same thing by running the script, but it doesn't work. I've copied the same code for the function above into a script sample.py as below.
# cat sample.py
def a():
val = raw_input("abc")
print "Entered value: %s" % val
a()
Attempts to achieve the same behavior of getting a notebook prompt while running the script from notebook:
1. Using the '!' prefix
This command just keeps running without giving any prompt:
[*] !python sample.py
abc
2. Using %%bash
Gives the following error:
%%bash
python sample.py
EOFError: EOF when reading a line
What is the reason for this to fail and is there a workaround to get it working as expected?
The two command you've used are used for shell command and bash script respectively.
To run Python script as a program you can use Magic command run.

subprocess.call() does not return when using pydev

consider the simple python program below which runs a powershell script. It works fine if I start the python program from the command line, even though I feel it's odd it's printing a blank line for the return value instead of 0. However, if I run this python program from pydev, the script hangs after calling subprocess.call(). I'm using Pydev 2.7.0 under Eclipse SDK 4.2.1 under Windows 7. Python version is 2.7.3.
import subprocess
def run_powershell_script(script):
cmd = ['powershell',
'-ExecutionPolicy',
'RemoteSigned',
'-File',
script]
returncode = subprocess.call(cmd)
print "Done"
return returncode
if __name__ == "__main__":
print run_powershell_script("testscript.ps1")
The powershell script I'm testing with is very simple: it prints the path and then returns 0:
Write-Host "$env:Path"
exit 0
So to recap I guess I have two questions, the most important being why subprocess.call() hangs when I run this program under pydev and the other question is why when I print the returncode I get a blank line instead of 0.
I'm not 100% here, but doesn't exit just kill the script? To return a value when you exit to script, use return.. so Try this PS script instead:
Write-Host "$env:Path"
return 0

Categories

Resources