How do I run a Python script from a C shell without specifying whole path?
I would like to run a Python script from a Hamilton C shell without having to specify the whole path of the script.
The first thing I tried was to add my scripts folder to the PATH environment variable in Windows. This worked, but the C shell cannot simply execute the .py files automatically, so I have to use "python" before the name of the script.
Now the problem is that since my scripts folder is not a Python path it is not recognized by the Python executable. So this does not work either.
I also tried to add my folder to the "PYTHONPATH" environment variable (which I had to create), but this does not work either.
Is there a way that I can run my script by either simply calling its name (myscript.py) or "python myscript.py"?
Solution 1:
Run Python scripts from Windows command line with script name only
Is there any way that I can run my script by either simply calling its name (myscript.py) or "python myscript.py"?
Absolutely, you can do this in Windows with no problem. I tested this and confirmed it works as expected with Windows 10 as a matter of fact by following the below steps.
Configure Windows File Association Pointers
Set the python.exe full explicit path as in the below #3 for the correct value where this is on your system
- Open up an elevated [cmd.exe] command prompt as administrator
- Type in
ASSOC .py=PythonScript
and press Enter - Type in
FTYPE PythonScript="C:\Program Files\Python\python.exe" "%1" %*
and press Enter - Type in
SET PATHEXT=.py;%PATHEXT%
and press Enter
Confirm and Test Windows File Association Pointers
Now from a command prompt you can simply type in the full path to a python script and then press Enter, and the python logic should run accordingly.
You can optionally just type in the full path minus the .py
file extension. You could also just CD
to the directory where a python script is located, and then type in the script name (with or without the extension) and press Enter, and python should run the script logic accordingly.
Examples from CMD (post above changes)
-
"C:\ScriptPath\scriptname.py"
and press Enter -
"C:\ScriptPath\scriptname"
and press Enter -
CD /D "C:\ScriptPath\scriptname.py"
and press Enter -
CD /D "C:\ScriptPath\scriptname.py"
and press Enter
Execute from Shell with Script Name only
If you really need to run the script from the command line without telling the shell the full explicit path to the python script file, then you need to add the path where this script resides to the %PATH%
environmental variable.
- Open up an elevated [cmd.exe] command prompt as administrator
- Type in
SET PATH=%PATH%;C:\Program Files\Python
where theC:\Program Files\Python
is the value where this exist on your system.
Now you can just type in the script name with or without the file extension without doing a CD
to another directory or explicitly specifying the full path to the python script.
Further Resources
Windows Environment Variables
-
FTYPE /?
Displays or modifies file types used in file extension associations FTYPE [fileType[=[openCommandString]]] fileType Specifies the file type to examine or change openCommandString Specifies the open command to use when launching files of this type. Type FTYPE without parameters to display the current file types that have open command strings defined. FTYPE is invoked with just a file type, it displays the current open command string for that file type. Specify nothing for the open command string and the FTYPE command will delete the open command string for the file type. Within an open command string %0 or %1 are substituted with the file name being launched through the assocation. %* gets all the parameters and %2 gets the 1st parameter, %3 the second, etc. %~n gets all the remaining parameters starting with the nth parameter, where n may be between 2 and 9, inclusive. For example: ASSOC .pl=PerlScript FTYPE PerlScript=perl.exe %1 %* would allow you to invoke a Perl script as follows: script.pl 1 2 3 If you want to eliminate the need to type the extensions, then do the following: set PATHEXT=.pl;%PATHEXT% and the script could be invoked as follows: script 1 2 3
-
ASSOC /?
Displays or modifies file extension associations ASSOC [.ext[=[fileType]]] .ext Specifies the file extension to associate the file type with fileType Specifies the file type to associate with the file extension Type ASSOC without parameters to display the current file associations. If ASSOC is invoked with just a file extension, it displays the current file association for that file extension. Specify nothing for the file type and the command will delete the association for the file extension.
Solution 2:
I'm the author of Hamilton C shell. It sounds like you're using Cygwin with my C shell and getting the following error when you try to use python:
1 C% python
csh(C:\cygwin64\bin\python line 1): Couldn't open 'symlink' as a redirected standard input.
> in C:\cygwin64\bin\python
< called from line 1
The problem is that the Cygwin python command is a Cygwin-only symbolic link file rather than an actual executable. They're only supported by Cygwin. (You may have noticed that cmd.exe won't run it either.) Here's what's in it:
2 C% whereis python
C:\cygwin64\bin\python
3 C% cat `!!`
cat `whereis python`
!<symlink>python2.7.exe
Not being able to recognize it as anything else, the C shell tried to interpret it as a script, thought it recognized a !<
C shell i/o redirection operator but could not find a file named symlink
, hence the error message.
But given that all that file is doing is redirecting you to the actual executable, you can do the same thing with a C shell alias which you can save in your startup.csh file:
4 C% alias python python2.7
5 C% python
Python 2.7.10 (default, Jun 1 2015, 18:05:38)
[GCC 4.9.2] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
6 C% cd desktop
7 C% cat MyScript.py
print("Hello")
8 C% python MyScript.py
Hello
If you'd like to run your python scripts directly from the C shell without having to type the python command, the C shell supports the common #!
syntax to tell it to use the python interpreter. But notice it still needs the name of the actual executable. Here's an example:
9 C% cat MyScript2.py
#!python2.7
print("Hello")
10 C% MyScript2.py
Hello
11 C%