python command line ok in python shell but not through windows cmd - python

I was about to test the ftpmirror builtin script (python322, winXP 32bits) from the cmd windows default shell and get this :
File "C:\Program Files\python322\Tools\Scripts\ftpmirror.py", line 161
print('Skip pattern', repr(pat), end=' ')
^
SyntaxError: invalid syntax
I tested the print() line directly in the python shell, trough cmd, and with idle (and in blender also) : this work obsiously.
I reproduce the error with a coucou.py file like this :
#! /usr/bin/env python3
pat = 'toto'
print("Skip pattern", repr(pat), end=" ")
when directly called from a cmd prompt :
C:\Program Files\python322\Tools\Scripts>coucou.py
same error than with ftpmirror
but :
C:\Program Files\python322\Tools\Scripts>python coucou.py
is ok
and my environment is ok I can execute py scripts directly from the windows ui by double-clicking a .py file, and I got working scripts working fine when called from .bat
I don't get it, it looks specific to the print() end argument, what did I not read yet about the way to execute python3 from the windows cmd shell ?
thanks,
Jerome

Try checking if you are running the same python interpreter when you double click or you run python from the command-line.
Save this in a .py file with this content and try running it with both methods:
import sys
print sys.version_info
I bet you are using different interpreters in each case.

Related

How do i run a python program just by typing the script name on windows 10 cmd line?

How do i run a python program just by typing the script name on windows 10 cmd line?
Also without having to change directory. I already added my scripts folder and python folder to the path.
tried also tu run assoc py.=PythonScript
ftype PythonScript=python.exe %1 %*
Here's the program's content:
#! python3
# mapIt.py - Launches a map in the browser using an address from the command line or clipboard
import webbrowser, sys, pyperclip
if len(sys.argv) > 1:
address = ' '.join(sys.argv[1:])
else:
address = pyperclip.paste()
webbrowser.open('https://www.google.com/maps/place/' + address)
I added a screenshot with all the commands i tried so far.
I think what you want is to run the file 'mapIt.py' without invoking the keyword python that is:
>mapIt.py
instead of
>python mapIt.py
the way to do that in Linux or macOS is simple enough, you can add
#!/usr/bin/env python
to the top of the file, rename your file from mapIt.py to mapIt
make the script executable:
chmod +x mapIt
But for windows there is no straightforward solution.
One way you can do it is convert the file into an exe or
first add a python.exe association for all '.py' files
> assoc .py=Python
and then
> ftype Python="<path of your python.exe>" "%1" %*
replace the text in angular brackets (<>) with the path of your python.exe file.

Python script doesn't print output in command prompt

I need some advice with a Python script. I'm still new and learned it by myself. I found the script on Google. After I retype it, it doesn't print the result in the console. How can the result of the script be shown in the console? Details as below:
C:\Python27>test1.py af8978b1797b72acfff9595a5a2a373ec3d9106d
C:\Python27>
After I press enter, nothing happens. Should the result be shown or not?
Here's the code that I retyped:
#!/usr/bin/python
#coding: ascii
import requests
import sys
import re
url = 'http://hashtoolkit.com/reverse-hash?hash='
try:
hash = sys.argv[1]
except:
print ("usage: python "+sys.argv[0]+" hash")
sys.exit()
http = request.get(url+hash)
content = http.content
cracked = re.findall("<span title=\*decrypted (md5|sha1|sha384|sha512) hash\*>(.*)</span>", content) # expression regular
print ("\n\tAlgoritmo: "+cracked[0][0])
print ("\tPassword Cracked: "+cracked[0][1])
The first line in your script is called a Shebang line.
A Shebang line tells the script to run the Python interpreter from that location.
The shebang line you provided is a Linux system path, but it looks from the path you are executing Python from, that you are running on Windows.
You can do one of two things here to fix that:
Remove the Shebange Line.
Remove the first line from your script.
Execute the script using python test1.py COMMAND_LINE_ARGUMENTS
Modify Your Shebang line.
Change the first line of your script from !/usr/bin/python to
#!python (This is assuming that python is in your systems PATH variable.)`
Execute the script using test1.py COMMAND_LINE_ARGUMENTS
Also, you are trying to import the requests module that is not installed in the standard library.
If you haven't installed this yet, you can do so by going to your Python install directory and go to the scripts folder.
Hold shift and right click and go Open command window here
Type pip install requests and hit enter.
After that you should be good to go, execute the script by navigating to it and type test.py COMMAND_LINE_ARGUMENT
If a Python script doesn't have the shebang line:
python test.py COMMAND_LINE_ARGUMENT
you need to run your script using python. try:
C:\Python27>python test1.py af8978b1797b72acfff9595a5a2a373ec3d9106d

Executable .py file with shebang path to which python gives error, command not found

I have a self-installed python in my user directory in a corporate UNIX SUSE computer (no sudo privilege):
which python
<user>/bin/python/Python-3.6.1/python
I have an executable (chmod 777) sample.py file with this line at the top of the file:
#!<user>/bin/python/Python-3.6.1/python
I can execute the file like this:
python sample.py
But when I run it by itself I get an error:
/full/path/sample.py
/full/path/sample.py: Command not found
I have no idea why it's not working. I'm discombobulated as what might be going wrong since the file is executable, the python path is correct, and the file executes if I put a python command in the front. What am I missing?
EDIT:
I tried putting this on top of the file:
#!/usr/bin/env python
Now, I get this error:
: No such file or directory
I tried this to make sure my env is correct
which env
/usr/bin/env
EDIT2:
Yes, I can run the script fine using the shebang command like this:
<user>/bin/python/Python-3.6.1/python /full/path/sample.py
Your file has DOS line endings (CR+LF). It works if you run python sample.py but doesn't work if you run ./sample.py. Recode the file so it has Unix line endings (pure LF at the end of every line).
Try using #!/usr/bin/env python as described in this post. Let the OS do the work.

Access Denied When Running .bat file, Windows 10

Windows 10 Home Version 1607. Python 3.5.2
I'm new to python, I've tried to run python script using a batch file. I've added my folder contain batch files to the PATH Environment Variables. Have tried to run my python script by typing in 'HelloWorld' on Win+R
However a error message pops saying 'This app can't run on your pc' and the cmd keeps on returning the current message
Access Denied. Press any key to continue.....
Python Script. Saved as HelloWorld.py
#! python3
import sys
print('Hello World')
print(sys.argv)
Batch File. Saved as HelloWorld.bat
#py C:\Users\Anthony\MyPythonScripts\HelloWorld.py %*
#pause
You are mistakenly using py instead of python to execute your script.
This causes the messages you are getting.
If you use python 3.5.2 or python 2.7 in a project or in a script to run it you should use the command
d:\Users\nameofuser>python nameofmyscript.
For exemple i have a script in python2:
import os
print "Hello wolrd! I'm a rabbit!"
i will run it with python rabbit.py
answer: Hello wolrd! I'm a rabbit!
if i have my script in python 3
import os
print("Hello wolrd! I'm a rabbit!")
this time i can use python3 rabbit.py or python rabbit.py
The only thing you can't do is to use python3 for a python 2.7 script you will have a SyntaxError

running python executable from windows command line with arguments

I am using the windows7 command prompt and have opened the python interpreter and changed to the directory where the file is located. The instructions I have say to get into the directory and type
./keyboardControl.py 192.168.1.108
where keyboardControl.py is the name of the file and the ip address is for a robot.
I get the error:
File "", line 1
.\keyboardControl.py 192.168.1.108
SyntaxError: invalid syntax
(with the carrot under the . before )
I have also tried:
python keyboardControl.py 192.168.1.108
I get the same error with the carrot now under the l in Control.
Any help would be greatly appreciated.
It sounds like you've launched the Python interpreter and are typing these commands into the REPL. This is not what you should be doing. The commands should be run directly at the cmd prompt, e.g.:
C:\Users\me>keyboardControl.py 192.168.1.108
If that does not work (file associations might not be set correctly - Windows does not handle the #! "shebang") the form would be.
C:\Users\me>python keyboardControl.py 192.168.1.108

Categories