running python executable from windows command line with arguments - python

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

Related

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.

What happened to my Ubuntu Desktop 16 today - cannot execute Python file as usual

In brief
I can't run a simple Python file with +x permission set and shebang line.
In Details
Let's take a simple Python code in myApp.py file at some $CODE_HOME folder
#!/usr/bin/python
print 122
When cd $CODE_HOME and running this file from console
. ./myApp.py
I got error as
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /usr/bin/print line 528.
Error: no such file "122333"
Though running by python myApp.py will get thing work.
The question
What's wrong is that? How to fix it?
. myApp.py is an instruction to Bash to source the passed file, ie execute it within the current process.
To execute a script or other file, you need to reference it by path:
./myApp.py (or just python myApp.py)
i.e. omitting the starting '.' in your call
To answer your question as is, . is the source command, which just runs each of the commands in the argument script in the context of the calling terminal. In your case, this doesn't do anything for the first line, then tries to call print, as you can see in
at /usr/bin/print line 528
Use ./myApp.py instead.

Running a python script from command prompt

I'm working on a tutorial for a big data class and am having trouble in the command line (Windows 7 Pro). I'm not very familiar with the command line environment so this is probably something simple, but here goes:
I have a python script called mapper.py that is stored in the directory
E:\Documents\School\Math\M 461\MapReduce\PythonScripts
and a file named
Medicare_Provider_Util_Payment_PUF_CY2013.txt
that is stored in
E:\Documents\School\Math\M 461\MapReduce\Data
Python (Anaconda) is installed at C:\Program Files\Anaconda3. I'm trying to feed the file to the script and execute it from the command line using
type Medicare_Provider_Util_Payment_PUF_CY2013.txt
| 'C:\Program Files\Anaconda3\python' mapper.py
I use the apostrophes because otherwise it doesn't like the space in Program Files. However, when I execute this command it says that "The filename, directory name, or volume label syntax is incorrect." I'm not sure where to go from here so any guidance would be appreciated.
The text file and the Python file aren't in the same directory, so your example can't work. Try this series of commands in at your command prompt:
First, make the common parent the current directory
E:
cd E:\Documents\School\Math\M 461\MapReduce
Then run your script, giving the path to the data and python files:
type Data\Medicare_Provider_Util_Payment_PUF_CY2013.txt | 'C:\Program Files\Anaconda3\python' PythonScripts\mapper.py

Why Do I get an Invalid syntax in python terminal when trying to open Django files from Documents folder.

I am running Python 2.7.3 and I just installed django 1.4.3. I am trying to open the file
cd ~/Documents/Projects
but I get an error that reads in total....
cd ~/Documents/Projects
File "", line 1
cd ~/Documents/Projects
^
SyntaxError: invalid syntax
Is it something to do with PATH directories being messed up? I have no idea I am a Newbie. Thanks
cd is a shell command, to change dir in python you need
import os
...
os.chdir('path/to/directory')
...
where ... is your code
You are trying to run shell commands inside Python. Python doesn't know what cd is. You need to do that from inside a plain command prompt, where you have a prompt that looks like C:\whatever\> _.

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

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.

Categories