How to call a shell script from Python in Windows Powershell? - python

I using a Python script to call a .sh script, as shown below
ret = subprocess.call('sh ./myScript.sh '+file_a+' '+file_b+' '+str(self.counter), shell=True)
The first two lines of myScript.sh are
#!/bin/bash
echo "sh script opened"
I can run myScript.sh directly from Windows Powershell, but not from my Python script(run in Powershell). So, I know that myScript.sh is correct; however, I do not get output when run from my Python script On a different computer, this call worked fine, but now it doesn't. Any suggestions?

As #abarnet mentioned in his comment there is a COMSPEC environement variable that you can change to point at powershell rather than cmd:
import os
import subprocess
print os.environ["COMSPEC"]
os.environ["COMSPEC"] = r"C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe"
print os.environ["COMSPEC"]
ret = subprocess.call('sh ./script.sh', shell=True)
print ret
Which on my Windows machine returns:
C:\Windows\system32\cmd.exe
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe
The term 'sh' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included , verify that the path is correct and try again. At line:1 char:3
+ sh <<<< ./script.sh
+ CategoryInfo : ObjectNotFound: (sh:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

Related

Python in vscode

I have downloaded vs code in my windows 10 computer just, I want to learn python in it.
So, I have installed some extensions like "Python" , "Python
for VSCode" ,"code runner".
But when I click on the run button (Ctrl+ALT+N)
I am getting this error in my terminal:
python : The term 'python' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name,
or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ python -u "e:\C File\Source file\practice.py"
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (python:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
But when I type py -u "e:\C File\Source file\practice.py"
instead of python -u "e:\C File\Source file\practice.py"
it is working,
Now what should I do to run this at one mouse click, I don't want to run the code by typing the command.
IMHO, most likely your 'Python' global environment variable is not set correctly.
But, since you mention that py -u [File Path] command seems to give you the output I can suggest that you change how "Code Runner" extension sends a Run command.
Steps:
Click File>Preferences>Settings (shortcut Ctrl+,)
Open "settings.json" file (refer to my attached GIF file for reference)
Paste the following code lines at the end of this file ensuring you don't miss any commas (again refer to the attached GIF):
"code-runner.executorMap": {
"python": "py -u",
},
This should change how the Code-Runner extension sends the Run Code command for Python files. Save the settings.json file and try running your code again using the same shortcut Ctrl+Alt+N".
Does this fix your issue, Pranav? If not, maybe post a clipping of the error message again.
Link to the GIF file showing how to insert the code in settings.json file

hello world python program two version python on same machine

I have installed python2.7 and python3.7.3 on same windows 10 laptop.
Now I am trying to run programmes separately.
On windows cmd I type
cmd>py -2 print 'hello world'
C:\Users\data\installation\python2.7\python.exe: can't open file 'print': [Errno 2] No such file or directory
I tried running on powershell
PS> python2 print 'hello world'
python2 : The term 'python2' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:1
+ python2 print 'hello world'
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (python2:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException`
I try to run on powershell
> py2 print 'hello world'
py2 : The term 'py2' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
At line:1 char:1
+ py2 print 'hello world'
+ ~~~
+ CategoryInfo : ObjectNotFound: (py2:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException`
If I run on powershell just
> py -2 print "hello world'
I get following prompt >>> but nothing else
same is problem with
> py -3 print ('Hello World')
C:\Users\data\installation\python\python.exe: can't open file 'print': [Errno 2] No such file or directory`
my script file (script.py) is:
#! py -3
python --version
I read these questions:
How do I run python 2 and 3 in windows 7?)
How to install both Python 2.x and Python 3.x in Windows 7
I expect hello world to be printed instead of this errors are coming.
You are not using the launcher or commands correctly.
The py launcher for Windows, like the Python command-line binary itself do not take Python code directly. So your first line
py -2 print 'hello world'
doesn't work because you asked the Python executable to find a file named print to run as a Python script. You can instead use the -c command line switch to run a line of Python code:
py -2 -c "print 'hello world'"
That tells the Windows console to start the process py, passing in the arguments -2, -c and print 'hello world', respectively (the " around the last part keep the console from seeing the spaces as delimiters). The py launcher then finds the Python 2.7 binary, and runs it with the remaining arguments.
Switching to the Powershell attempts, the first one doesn't work because Windows doesn't know where to find a python2 exectable:
The term 'python2' is not recognized as the name of a cmdlet, function, script file, or operable program
There is no py2 executable either.
Stick to using the py launcher, as the two questions you link to advice:
py -2 -c "print 'hello world'"
Your script.py file can't work, because everything past the first line, #! py -3, is Python code. python --version is not valid Python. This will work:
#! py -3
import sys
print(sys.version_info)

How do I run pipenv, if I always get the same error?

I'm trying to run 'pipenv' on Windows 10, and it doesn't want to run... The error is receive is below
PS C:\Users\charl\Documents\Python\Face Recognition> pipenv shell
pipenv : The term 'pipenv' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
+ pipenv shell
+ ~~~~~~
+ FullyQualifiedErrorId : CommandNotFoundException```
Use python -m pipenv instead of just pipenv, it should work. Best of luck to you.

trouble creating virtual environment for python 3 in windows 10

I want to create a virtualenv in Windows 10 for python 3.7
I'm trying to follow the instruction from python doc
So first I entered the command
PS T:\python -m venv c:\path\to\myenv
it did create directories and files into that path
then according to the documentation, using PowerShell I entered:
PS T:\path\to\myenv\Scripts\Activate.ps1
but I got the following:
The term
'T:\path\to\myenv\Scripts\Activate.ps1' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At line:1 char:1
+ T:\path\to\myenv\Scripts\Activate.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (T:\path\to\myenv...ts\Activate.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
What am I missing
you should replace
path\to\myenv
to your actual path
so as said in docs
C:\> <venv>\Scripts\Activate.ps1
for example it should be something like
C:\>MyProjectsFolder\first_project\venv\Scripts\Activate.ps1
the other workaround is to manually change directories to Scripts folder and then run
Activate.ps1
if it didnt solve your problem please specify your project folder and structure

Run a powershell script from python that uses Web-Administration module

I have a very long powershell script that I need to execute from a python script.
The powershell script works fine when I run it from command line, but fails when I run it from python.
I've simplified my powershell script to the following code, which reproduces the issue.
Powershell Script:
import-module WebAdministration
Add-Type -Path C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll
Set-WebConfigurationProperty '/system.webServer/proxy' -Name "enabled" -Value "true"
Python Script:
import subprocess
print subprocess.check_output("powershell [pathToMyPowershellScript]");
When I run the powershell script from cmd.exe, it works fine and produces no output.
When I run the python script, it produces the following error:
Set-WebConfigurationProperty : Retrieving the COM class factory for component
with CLSID {688EEEE5-6A7E-422F-B2E1-6AF00DC944A6} failed due to the following
error: 80040154
Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
At [pathToMyPowershellScript]:3 char:1
+ Set-WebConfigurationProperty '/system.webServer/proxy' -Name "enabled" -Value "t ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo
: NotSpecified: (:) [Set-WebConfigurationProperty], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,
Microsoft.IIs.PowerShell.Provider.SetConfigurationPropertyCommand
I'm running IIS 7.5 on Windows 7 Professional (x64), with Python 2.7 and Powershell 1.0
Any ideas?
I was able to fix this by running the powershell script from the "sysnative" version of cmd:
subprocess.check_call("c:\\windows\\sysnative\\cmd.exe /c
powershell pathToScript", shell=True)
This solves the bitness issue.
Your program may be calling the wrong version of powershell, try explictly calling the exe.
%SystemRoot%\SysWoW64\WindowsPowerShell\v1.0\powershell.exe
Heres some related reading you might find interesting.

Categories

Resources