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)
Related
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.
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
I want run this simple python code int terminal:
#!/usr/bin/env python3
print('Hello world')
I saved this script as hello.py
I go to terminal to write down:
$ chmod +x hello.py
then I click enter. This is to allow permission to be granted. Terminal then showed me this:
-bash: $: command not found
Ok...I then write down the path to hello.py:
$ /Users/myname/Documents/MyPythonScripts/hello.py
I press enter. I was expecting terminal to print out hello world but to my horror, terminal show this:
/Users/myname/Documents/MyPythonScript/hello.py: line 3: syntax error near unexpected token `'Hello world''
/Users/hadi/Documents/MyPythonScript/hello.py: line 3: `print('Hello world')'
What's wrong here?
Btw, Running on macOS 10.13.3 and Python 3.6.3
First of all, this line:
-bash: $: command not found
tells that you've executed the "$" which is not right.
Your command to set permissions should look like this:
chmod +x hello.py
Another comment is that the shebang line should be without space:
#!/usr/bin/env python3
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
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.