Can't run a Python file in Windows command prompt [duplicate] - windows

This question already has answers here:
How to run a python script from IDLE interactive shell?
(15 answers)
Closed 5 years ago.
I'm running Windows 7 64-bit with Python 3.4.1, 64-bit
cmd window
I've appended the PATH in system Environment Variable with ;C:\Python34 and saved, then gone into cmd and done the following, yet I keep getting an 'INVALID SYNTAX' message. Yes, I've searched the Q&A here and seen several threads with same problem, but haven't reached success trying their suggestions. As can be seen, I've tried 3 variations of the basic command "python ." Please help.

I think you're misunderstanding the idea.
If you want to execute a script "RUN_ME.py", run python RUN_ME.py directly in the command line. (type cmd into start and then python RUN_ME.py in the window you get)
If you want to type some lines of python and interpret them directly, type python without arguments. (or use the program shortcut you used here)

The mistake made here is the difference between the command line and the python REPL.
When you opened the terminal the way you did, probably by clicking on python terminal or whatever, you opened the python REPL (also available by executing python without arguments in command line), this is a place where you can execute code directly (no scripts). it used to be able to run python scripts as well but the command was phased out in python 3.x. Typically the REPL starts lines with >>>
To run a python script, you have to be in the command line and give the script as parameter to python, aka
python <script_name>. Getting the command line on windows can be done directly by typing cmd, or if you want to open it at a certain path, browse to the path you want in the file explorer and type cmd into the place where the path is displayed.

Okay, I understand now not to use 'python' plus the .py filename when already in the Python '>>>' prompt. Here's a simple try within Windows command prompt at navigating to the directory where the 'RUN_ME.py' file is located and attempting to run:
C:\>cd program files\trader workstation
C:\Program Files\Trader Workstation>cd ibridgepy
C:\Program Files\Trader Workstation\IBridgePy>python
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.getcwd()
'C:\\Program Files\\Trader Workstation\\IBridgePy'
>>> RUN_ME.py
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'RUN_ME' is not defined

Related

Is it possible to execute a '.m' file from windows cmd like python

Please bear with me, I'm neither an Octave developer or Windows cmd developer. However I have found myself in a situation where the path of least resistance would be a cli solution. From Linux or Windows for that matter I can run Python ['test'].py - and expect to see nothing or something depending on what I want returned (e.g. if test.py had a line...
print('this is a test')
I could run and get
>python test.py
this is a test
So my question is if I have a matlab, or '.m' (something recognizable by Octave), is there a analog in octave to running the above as below
>octave test.m
I've tried
>octave --no-gui
That produces a separate command line version of octave - which could work if someone could help me understand how to chain a command such that once ran, the octave cli would execute the assigned octave.m file like
> octave --no-gui & test.m
My apologies if this is something obvious.

`gitk` program found using the `where` command but cannot be executed?

I'm trying to run gitk on Windows but the console gives me:
'gitk' is not recognized as an internal or external command,
operable program or batch file.
But then, I can do
> where gitk
C:\Program Files (x86)\Git\bin\gitk
How comes?
UPDATE: Stealing the OP's comment:
Thanks for a (lengthy) explanation, the easiest fix for me was to move from the old msysGit (1.9.5) version to the newer "Git for Windows" builds which has a proper Windows wrapper for gitk.
gitk, unlike most git commands, is a shell script that invokes Tcl.
If you examine the gitk executable file itself, its first few lines are:
#!/bin/sh
# Tcl ignores the next line -*- tcl -*- \
exec wish "$0" -- "$#"
(At least that's what it looks like on my Linux system; the Windows installer for gitk might change that.)
On a Unix-like system, that first line, known as a "shebang", tells the system to invoke /bin/sh to execute the file rather than executing it directly.
The third line executes the wish command, which executes the script as a Tcl script.
If you have Tcl installed on your Windows system, you should be able to run gitk as a Tcl script using something like this:
C:\> tclsh "C:\Program Files (x86)\Git\bin\gitk"
(Disclaimer: I have not tried this.)
You might also be able to change the command's name from gitk to gitk.tcl and set up a file association in Windows so it will be invoked correctly. (I'm a little surprised that the Windows git installer didn't do this for you.)
More information on running Tcl scripts on Windows can be found here.
And this question discusses invoking gitk on Windows from the context menu rather than from the command line.

Command prompt isn't integrating anything

I installed amazonCLI, as well as Cygwin, and changed the Path env variable to:
%SystemRoot%\System32\Wbem;C:\Program Files\Prio;C:\Program
Files\Diskeeper Corporation\ExpressCache\;C:\Program
Files\Amazon\AWSCLI\;C:\cygwin64\bin
When I open the command prompt, first of all it is directly pointing to
C:\Users\Stephane
(which I think is weird). And then when I input something like 'ls', the return error is:
'ls' is not recognized as internal or external command, operable program or batch file
Can you please help me know what I am doing wrong?
Thanks
From the error message you got, it's evident, that you are trying to execute Cygwin's commands from the Windows Console. That's also the reason, why it's executed in your Windows' profile directory instead of the Cygwin's one.
You should read the basics about using the Cygwin first, because it seems you don't know what Cygwin exactly is and how to use it. Maybe you don't need the Cygwin at all, it depends on what you need to accomplish. The is for example GnuWin tool set or UnxUtils, which are just a sets of standalone GNU tools compiled for Windows. Cygwin is more like system than standalone utilities. You can for example execute shell scripts under the Cygwin.
So it depends on your needs. But I simply can't imagine using Windows without Cygwin yet.
You can execute Cygwin's shell from the Windows Console, but I advice you to use MinTTY (which is in directory $CYGWIN_DIR/bin). MinTTY is a terminal emulator, which executes your Cygwin shell (bash by default).
To execute for example bash directly from the Windows Console, just execute $CYGWIN_DIR/bash.exe --login -i.

Sublime text command line Name error

Good Day,
I am trying to run command line stuff from Sublime text's console. Is that possible? doing stuff like "ls" doesn't work
ls
Traceback (most recent call last):
File "", line 1, in
NameError: name 'ls' is not defined
The Sublime Text console is actually the built-in Python interpreter, so it only accepts Python commands. You can use the os* module to interact with the operating system. The following commands will give you a directory listing:
import os
os.chdir('/path/to/something')
os.listdir('.')
Check out the Files and Directories* section of the docs for more commands.
* Sublime Text 2 uses Python 2.6, while ST3 uses version 3.3. If you're using ST3, just change the 2.6 in the URLs I gave to 3 and you'll get the correct documentation.

Syntax error in setting the current directory of command prompt in Windows 7

I saved a python file to my desktop. In order to test it, I used the command prompt. However, my command prompt is located in
C:\Users\Name\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Accessories
To change the current directory, I wrote
cd C:\Users\Name\Desktop
in the command prompt. However, it keeps telling me that I have an "invalid syntax"
>>>cd C:\Users\Name\Desktop
File "<stdin>", line 1
cd C:\Users\Name\Desktop
^
SyntaxError: invalid syntax
Any suggestion to fix this problem?
the >>> prompt indicates you are already running python interpreter.
You need to enter that command from the shell prompt.
Press Ctrl-D to exit Python to the ordinary shell prompt and try again.
P.S: I'm not related to anyway with python Reference : http://ubuntuforums.org/showthread.php?t=1558728
try this on ms-dos
cd\
cd C:\Users\YourUserName\Desktop
python filename.py

Resources